Skip to content

Commit

Permalink
add benchmark tests for package protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyenought committed Nov 25, 2023
1 parent 28b4051 commit 8adbe49
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pkg/protocol/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@
package protocol

import (
"github.com/cloudwego/hertz/pkg/common/test/assert"
"testing"

"github.com/cloudwego/hertz/pkg/common/test/assert"
)

func TestArgsDeleteAll(t *testing.T) {
Expand Down
23 changes: 22 additions & 1 deletion pkg/protocol/header_timing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@
package protocol

import (
"github.com/cloudwego/hertz/pkg/common/test/assert"
"net/http"
"strconv"
"strings"
"testing"

"github.com/cloudwego/hertz/pkg/common/test/assert"
"github.com/cloudwego/hertz/pkg/protocol/consts"
)

Expand Down Expand Up @@ -162,3 +162,24 @@ func BenchmarkResponseHeader_SetContentLength(b *testing.B) {
rh.Reset()
}
}

func BenchmarkRequestHeaderVisitAll(b *testing.B) {
h := RequestHeader{}
h.Set("xxx", "yyy")
h.Set("xxx2", "yyy2")

b.ResetTimer()
h.VisitAll(func(k, v []byte) {
key := string(k)
value := string(v)
if key != "Xxx" && key != "Xxx2" {
b.Fatalf("Unexpected %v. Expected %v", key, "xxx or yyy")
}
if key == "Xxx" && value != "yyy" {
b.Fatalf("Unexpected %v. Expected %v", value, "yyy")
}
if key == "Xxx2" && value != "yyy2" {
b.Fatalf("Unexpected %v. Expected %v", value, "yyy2")
}
})
}
36 changes: 36 additions & 0 deletions pkg/protocol/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,25 @@ func TestResponseBodyStreamMultipleBodyCalls(t *testing.T) {
}
}

func BenchmarkName(b *testing.B) {
var r Response

s := "foobar baz abc"
if r.IsBodyStream() {
b.Fatalf("IsBodyStream must return false")
}
r.SetBodyStream(bytes.NewBufferString(s), len(s))
if !r.IsBodyStream() {
b.Fatalf("IsBodyStream must return true")
}
for i := 0; i < b.N; i++ {
body := r.Body()
if string(body) != s {
b.Fatalf("unexpected body %q. Expecting %q. iteration %d", body, s, i)
}
}
}

func TestResponseBodyWriteToPlain(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -294,3 +313,20 @@ func TestResponse_HijackWriter(t *testing.T) {
resp.GetHijackWriter().Finalize()
assert.True(t, isFinal)
}

func BenchmarkResponse_HijackWriter(b *testing.B) {
buf := new(bytes.Buffer)
isFinal := false
for i := 0; i < b.N; i++ {
resp := AcquireResponse()
resp.HijackWriter(&mock.ExtWriter{Buf: buf, IsFinal: &isFinal})
resp.AppendBody([]byte("hello"))
resp.GetHijackWriter().Flush()
resp.AppendBodyString(", world")
resp.GetHijackWriter().Flush()
resp.GetHijackWriter().Flush()
resp.GetHijackWriter().Finalize()
resp.Reset()
buf.Reset()
}
}

0 comments on commit 8adbe49

Please sign in to comment.