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 Dec 7, 2023
1 parent 64bdd12 commit 0122976
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 11 deletions.
27 changes: 27 additions & 0 deletions pkg/app/server/binding/reflect_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
23 changes: 23 additions & 0 deletions pkg/protocol/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down
36 changes: 34 additions & 2 deletions pkg/protocol/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()))
Expand Down Expand Up @@ -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()

Expand Down
51 changes: 42 additions & 9 deletions pkg/protocol/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -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{}
Expand All @@ -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()
Expand Down
44 changes: 44 additions & 0 deletions pkg/protocol/trailer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package protocol

import (
"fmt"
"strings"
"testing"

Expand All @@ -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{}

Expand Down Expand Up @@ -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"))
Expand All @@ -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"))
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions pkg/protocol/uri_timing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 0122976

Please sign in to comment.