-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmarks_test.go
172 lines (148 loc) · 4.15 KB
/
benchmarks_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package peanats
import (
"context"
"fmt"
"github.com/mikluko/peanats/examples/protojson/api"
"github.com/mikluko/peanats/testutil"
"testing"
"github.com/nats-io/nats.go"
)
func BenchmarkServer(b *testing.B) {
ns := testutil.NatsServer(b)
sc, err := nats.Connect(ns.Addr().String())
if err != nil {
b.Fatal(err)
}
defer sc.Close()
srv := Server{
Conn: NATS(sc),
ListenSubjects: []string{"test.simple"},
Handler: ChainMiddleware(
HandlerFunc(func(pub Publisher, req Request) (err error) {
err = pub.Publish(req.Data())
if err != nil {
return err
}
return nil
}),
),
}
err = srv.Start()
if err != nil {
b.Fatal(err)
}
cc, err := nats.Connect(ns.Addr().String())
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
b.ResetTimer()
for i := 0; i < b.N; i++ {
msg, err := cc.RequestWithContext(ctx, "test.simple", []byte("the parson had a dog"))
if err != nil {
b.Fatal(err)
}
if string(msg.Data) != "the parson had a dog" {
b.Fatalf("unexpected response: %s", msg.Data)
}
}
}
func BenchmarkRaw(b *testing.B) {
type argument struct {
Arg string `json:"arg"`
}
type result struct {
Res string `json:"res"`
}
handler := Typed[argument, result](&JsonCodec{}, TypedHandlerFunc[argument, result](
func(pub TypedPublisher[result], req TypedRequest[argument]) error {
res := result{Res: req.Payload().Arg}
return pub.Publish(&res)
},
))
req := new(requestMock)
req.On("Subject").Return("foo").Times(b.N)
req.On("Data").Return([]byte("{\"arg\":\"the parson had a dog\"}")).Times(b.N)
pub := new(publisherMock)
pub.On("Publish", []byte("{\"res\":\"the parson had a dog\"}")).Return(nil).Times(b.N)
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
err := handler.Serve(pub, req)
if err != nil {
b.Fatal(err)
}
}
})
}
func benchmarkTyped[RQ, RS any](b *testing.B, codec Codec, rq *RQ, rs *RS) {
handler := Typed[RQ, RS](codec, TypedHandlerFunc[RQ, RS](
func(pub TypedPublisher[RS], req TypedRequest[RQ]) error {
return pub.Publish(rs)
},
))
rqp, err := codec.Encode(rq)
if err != nil {
b.Fatalf("request encode failed: %s", err)
}
req := new(requestMock)
req.On("Subject").Return("foo").Times(b.N)
req.On("Data").Return(rqp).Times(b.N)
rsp, err := codec.Encode(rs)
if err != nil {
b.Fatalf("response encode failed: %s", err)
}
pub := new(publisherMock)
pub.On("Publish", rsp).Return(nil).Times(b.N)
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
err := handler.Serve(pub, req)
if err != nil {
b.Fatal(err)
}
}
})
}
func BenchmarkTyped(b *testing.B) {
b.Run("json", func(b *testing.B) {
benchmarkTyped(b, &JsonCodec{}, &api.Argument{Arg: "the parson had a dog"}, &api.Result{Res: "the parson had a dog"})
})
b.Run("proto", func(b *testing.B) {
benchmarkTyped(b, &ProtoCodec{}, &api.Argument{Arg: "the parson had a dog"}, &api.Result{Res: "the parson had a dog"})
})
b.Run("protojson", func(b *testing.B) {
benchmarkTyped(b, &ProtojsonCodec{}, &api.Argument{Arg: "the parson had a dog"}, &api.Result{Res: "the parson had a dog"})
})
b.Run("prototext", func(b *testing.B) {
benchmarkTyped(b, &PrototextCodec{}, &api.Argument{Arg: "the parson had a dog"}, &api.Result{Res: "the parson had a dog"})
})
}
func BenchmarkServeMux(b *testing.B) {
var benchmark = func(b *testing.B, num int) {
mux := ServeMux{}
_ = mux.HandleFunc(func(pub Publisher, req Request) error {
return pub.Publish([]byte("hello"))
}, "foo")
for i := 0; i < num; i++ {
_ = mux.HandleFunc(func(pub Publisher, req Request) error {
b.Fatal("should not be called")
return nil
}, fmt.Sprintf("bar-%07d", i))
}
req := new(requestMock)
req.On("Subject").Return("foo").Times(b.N)
pub := new(publisherMock)
pub.On("Publish", []byte("hello")).Return(nil).Times(b.N)
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = mux.Serve(pub, req)
}
})
}
b.Run("1", func(b *testing.B) { benchmark(b, 1) })
b.Run("10", func(b *testing.B) { benchmark(b, 10) })
b.Run("100", func(b *testing.B) { benchmark(b, 100) })
}