diff --git a/pkg/app/server/binding/reflect_internal_test.go b/pkg/app/server/binding/reflect_internal_test.go index 65dc68fc8..847cbe436 100644 --- a/pkg/app/server/binding/reflect_internal_test.go +++ b/pkg/app/server/binding/reflect_internal_test.go @@ -49,6 +49,17 @@ func Test_ReferenceValue(t *testing.T) { assert.DeepEqual(t, "f1", deFoo1PointerVal.Field(0).Interface().(string)) } +func BenchmarkReferenceValue(b *testing.B) { + b.ResetTimer() + b.ReportAllocs() + + for i := 0; i < b.N; i++ { + foo1 := foo2{F1: "f1"} + foo1Val := reflect.ValueOf(foo1) + decoder.ReferenceValue(foo1Val, 5) + } +} + func Test_GetNonNilReferenceValue(t *testing.T) { foo1 := (****foo)(nil) foo1Val := reflect.ValueOf(foo1) @@ -88,3 +99,19 @@ func Test_GetFieldValue(t *testing.T) { t.Errorf("expect can set value, but not") } } + +func BenchmarkGetFieldValue(b *testing.B) { + type bar struct { + B1 **fooq + } + + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + bar1 := (***bar)(nil) + parentIdx := []int{0} + + bar1Val := reflect.ValueOf(bar1) + decoder.GetFieldValue(bar1Val, parentIdx) + } +} diff --git a/pkg/protocol/args_test.go b/pkg/protocol/args_test.go index 59cc176f8..a8c6e54b4 100644 --- a/pkg/protocol/args_test.go +++ b/pkg/protocol/args_test.go @@ -61,6 +61,16 @@ func TestArgsDeleteAll(t *testing.T) { } } +func BenchmarkArgs_Add(b *testing.B) { + var a Args + + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + a.Add("q1", "foo") + } +} + func TestArgsBytesOperation(t *testing.T) { var a Args a.Add("q1", "foo") @@ -71,6 +81,19 @@ func TestArgsBytesOperation(t *testing.T) { assert.DeepEqual(t, []byte(""), peekArgBytes(a.args, []byte("q2"))) } +func BenchmarkArgs_setArgBytes(b *testing.B) { + var a Args + a.Add("q1", "foo") + a.Add("q2", "bar") + + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + setArgBytes(a.args, a.args[0].key, a.args[0].value, false) + setArgBytes(a.args, a.args[1].key, a.args[1].value, true) + } +} + func TestArgsPeekExists(t *testing.T) { var a Args a.Add("q1", "foo") diff --git a/pkg/protocol/request_test.go b/pkg/protocol/request_test.go index fda96cd47..3a285aa4d 100644 --- a/pkg/protocol/request_test.go +++ b/pkg/protocol/request_test.go @@ -67,9 +67,18 @@ func (er errorReader) Read(p []byte) (int, error) { func TestMultiForm(t *testing.T) { var r Request - // r.Header.Set() _, err := r.MultipartForm() - fmt.Println(err) + assert.NotNil(t, err) +} + +func BenchmarkMultipartForm(b *testing.B) { + var r Request + + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + r.MultipartForm() + } } func TestRequestBodyWriterWrite(t *testing.T) { @@ -78,6 +87,16 @@ func TestRequestBodyWriterWrite(t *testing.T) { assert.DeepEqual(t, "test", string(w.r.body.B)) } +func Benchmark_RequestBodyWriterWrite(b *testing.B) { + w := requestBodyWriter{&Request{}} + + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + w.Write([]byte("test")) + } +} + func TestRequestScheme(t *testing.T) { req := NewRequest("", "ptth://127.0.0.1:8080", nil) assert.DeepEqual(t, "ptth", string(req.Scheme())) @@ -117,6 +136,19 @@ func TestRequestSwapBody(t *testing.T) { assert.DeepEqual(t, "testB", string(body)) } +func BenchmarkSwapRequestBody(b *testing.B) { + reqA := &Request{} + reqB := &Request{} + reqB.SetBodyRaw([]byte("testB")) + reqA.SetBodyRaw([]byte("testA")) + + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + SwapRequestBody(reqA, reqB) + } +} + func TestRequestKnownSizeStreamMultipartFormWithFile(t *testing.T) { t.Parallel() diff --git a/pkg/protocol/response_test.go b/pkg/protocol/response_test.go index 8e8ac36d3..d20609e80 100644 --- a/pkg/protocol/response_test.go +++ b/pkg/protocol/response_test.go @@ -92,22 +92,19 @@ func TestResponseBodyStreamMultipleBodyCalls(t *testing.T) { } } -func BenchmarkName(b *testing.B) { +func BenchmarkResponseBodyStreamMultipleBodyCalls(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") - } + + b.ResetTimer() + b.ReportAllocs() for i := 0; i < b.N; i++ { + r.SetBodyStream(bytes.NewBufferString(s), len(s)) body := r.Body() if string(body) != s { b.Fatalf("unexpected body %q. Expecting %q. iteration %d", body, s, i) } + r.Reset() } } @@ -221,6 +218,20 @@ func TestResponseBodyGunzip(t *testing.T) { assert.DeepEqual(t, zipData, src1) } +func BenchmarkResponse_BodyGunzip(b *testing.B) { + dst1 := []byte("") + src1 := []byte("hello") + res1 := compress.AppendGzipBytes(dst1, src1) + resp := Response{} + resp.SetBody(res1) + + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + resp.BodyGunzip() + } +} + func TestResponseSwapResponseBody(t *testing.T) { t.Parallel() resp1 := Response{} @@ -241,6 +252,28 @@ func TestResponseSwapResponseBody(t *testing.T) { assert.DeepEqual(t, resp2.BodyStream(), bytes.NewBufferString(str1)) } +func BenchmarkSwapResponseBody(b *testing.B) { + str1 := "resp1" + str2 := "resp2" + + byteBuffer1 := &bytebufferpool.ByteBuffer{} + byteBuffer2 := &bytebufferpool.ByteBuffer{} + resp1 := Response{} + resp2 := Response{} + + byteBuffer1.Set([]byte(str1)) + resp1.ConstructBodyStream(byteBuffer1, bytes.NewBufferString(str1)) + + byteBuffer2.Set([]byte(str2)) + resp2.ConstructBodyStream(byteBuffer2, bytes.NewBufferString(str2)) + + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + SwapResponseBody(&resp1, &resp2) + } +} + func TestResponseAcquireResponse(t *testing.T) { t.Parallel() resp1 := AcquireResponse() diff --git a/pkg/protocol/trailer_test.go b/pkg/protocol/trailer_test.go index ced1cc26f..0d5cd27db 100644 --- a/pkg/protocol/trailer_test.go +++ b/pkg/protocol/trailer_test.go @@ -17,6 +17,7 @@ package protocol import ( + "fmt" "strings" "testing" @@ -36,6 +37,16 @@ func TestTrailerAdd(t *testing.T) { assert.True(t, strings.Contains(string(tr.Header()), "Bar: value3")) } +func BenchmarkTrailer_Add(b *testing.B) { + var tr Trailer + + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + tr.Add("bar", "value3") + } +} + func TestHeaderTrailerSet(t *testing.T) { h := &RequestHeader{} @@ -89,6 +100,18 @@ func TestTrailerDel(t *testing.T) { assert.True(t, strings.Contains(string(tr.Header()), "Bar: value3")) } +func BenchmarkTrailer_Del(b *testing.B) { + var tr Trailer + + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + tr.Add("foo", "value3") + tr.Del("foo") + tr.Reset() + } +} + func TestTrailerSet(t *testing.T) { var tr Trailer assert.Nil(t, tr.Set("foo", "value1")) @@ -99,6 +122,16 @@ func TestTrailerSet(t *testing.T) { assert.True(t, strings.Contains(string(tr.Header()), "Bar: value3")) } +func BenchmarkTrailer_Set(b *testing.B) { + var tr Trailer + + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + tr.Set(fmt.Sprintf("foo%v", i), "value1") + } +} + func TestTrailerGet(t *testing.T) { var tr Trailer assert.Nil(t, tr.Add("foo", "value1")) @@ -118,6 +151,17 @@ func TestTrailerUpdateArgBytes(t *testing.T) { assert.False(t, strings.Contains(string(tr.Header()), "Bar: value3")) } +func BenchmarkUpdateArgBytes(b *testing.B) { + var tr Trailer + tr.addArgBytes([]byte("Foo"), []byte("value0"), argsNoValue) + + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + tr.UpdateArgBytes([]byte("Foo"), []byte("value1")) + } +} + func TestTrailerEmpty(t *testing.T) { var tr Trailer assert.DeepEqual(t, tr.Empty(), true) diff --git a/pkg/protocol/uri_timing_test.go b/pkg/protocol/uri_timing_test.go index 950117a2d..5abab2290 100644 --- a/pkg/protocol/uri_timing_test.go +++ b/pkg/protocol/uri_timing_test.go @@ -81,6 +81,8 @@ func BenchmarkURIFullURI(b *testing.B) { func benchmarkURIParse(b *testing.B, host, uri string) { strHost, strURI := []byte(host), []byte(uri) + b.ResetTimer() + b.ReportAllocs() b.RunParallel(func(pb *testing.PB) { var u URI for pb.Next() {