forked from monzo/typhon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_bench_test.go
76 lines (64 loc) · 1.77 KB
/
response_bench_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package typhon
import (
"bytes"
"io/ioutil"
"strings"
"testing"
"google.golang.org/protobuf/proto"
"github.com/monzo/typhon/prototest"
)
func BenchmarkResponseDecode(b *testing.B) {
b.ReportAllocs()
rsp := NewResponse(NewRequest(nil, "GET", "/", nil))
b.ResetTimer()
for i := 0; i < b.N; i++ {
rsp.Body = &rc{*strings.NewReader(`{"foo":"bar"}` + "\n"), 0}
v := map[string]string{}
rsp.Decode(&v)
}
}
func BenchmarkResponseProtobufDecode(b *testing.B) {
b.ReportAllocs()
g := &prototest.Greeting{Message: "Hello world!", Priority: 1}
out, _ := proto.Marshal(g)
rsp := NewResponse(NewRequest(nil, "GET", "/", nil))
rsp.Header.Set("Content-Type", "application/protobuf")
b.ResetTimer()
for i := 0; i < b.N; i++ {
rsp.Body = ioutil.NopCloser(bytes.NewReader(out))
g := &prototest.Greeting{}
rsp.Decode(g)
}
}
func BenchmarkRepeatedResponseDecode(b *testing.B) {
b.ReportAllocs()
rsp := NewResponse(NewRequest(nil, "GET", "/", nil))
rsp.Body = &rc{*strings.NewReader(`{"foo":"bar"}` + "\n"), 0}
b.ResetTimer()
for i := 0; i < b.N; i++ {
v := map[string]string{}
rsp.Decode(&v)
}
}
func BenchmarkRepeatedProtobufDecode(b *testing.B) {
b.ReportAllocs()
g := &prototest.Greeting{Message: "Hello world!", Priority: 1}
out, _ := proto.Marshal(g)
rsp := NewResponse(NewRequest(nil, "GET", "/", nil))
rsp.Header.Set("Content-Type", "application/protobuf")
rsp.Body = &rc{*strings.NewReader(string(out)), 0}
b.ResetTimer()
for i := 0; i < b.N; i++ {
gout := &prototest.Greeting{}
rsp.Decode(gout)
}
}
func BenchmarkRepeatedResponseBodyBytes(b *testing.B) {
b.ReportAllocs()
rsp := NewResponse(NewRequest(nil, "GET", "/", nil))
rsp.Body = &rc{*strings.NewReader(`{"foo":"bar"}` + "\n"), 0}
b.ResetTimer()
for i := 0; i < b.N; i++ {
rsp.BodyBytes(false)
}
}