diff --git a/pkg/protocol/args_test.go b/pkg/protocol/args_test.go index a49d6a8ea..59cc176f8 100644 --- a/pkg/protocol/args_test.go +++ b/pkg/protocol/args_test.go @@ -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) { diff --git a/pkg/protocol/header_timing_test.go b/pkg/protocol/header_timing_test.go index b50d64c58..8d0f8567e 100644 --- a/pkg/protocol/header_timing_test.go +++ b/pkg/protocol/header_timing_test.go @@ -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" ) @@ -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") + } + }) +} diff --git a/pkg/protocol/response_test.go b/pkg/protocol/response_test.go index 20a18ffce..8e8ac36d3 100644 --- a/pkg/protocol/response_test.go +++ b/pkg/protocol/response_test.go @@ -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() @@ -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() + } +}