-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
447 lines (362 loc) · 9.86 KB
/
benchmark_test.go
File metadata and controls
447 lines (362 loc) · 9.86 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// Copyright (c) Bartłomiej Płotka @bwplotka
// Licensed under the Apache License 2.0.
package localgrpc
import (
"context"
"fmt"
"io"
"iter"
"log"
"net"
"os"
"testing"
"time"
listv0 "github.com/bwplotka/benchmarks/benchmarks/local-grpc/dev/bwplotka/list/v0"
"github.com/efficientgo/core/errors"
"github.com/efficientgo/core/testutil"
"github.com/fullstorydev/grpchan/inprocgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
var _ listv0.ListStringsServer = &List{}
type List struct {
listv0.UnimplementedListStringsServer
resps []*listv0.ListResponse
}
func (l *List) List(_ *listv0.ListRequest, srv grpc.ServerStreamingServer[listv0.ListResponse]) error {
for _, r := range l.resps {
if err := srv.Send(r); err != nil {
return err
}
}
return nil
}
/*
export bench=bench01-2025 && go test \
-run '^$' -bench '^BenchmarkLocal' \
-benchtime 5s -count 6 -cpu 2 -timeout 999m \
| tee ${bench}.txt
benchstat -col /impl bench12-2024.txt
*/
func BenchmarkLocal(b *testing.B) {
benchmarkLocal(testutil.NewTB(b))
}
func TestBenchmarkLocal(t *testing.T) {
benchmarkLocal(testutil.NewTB(t))
}
const testString = "1pjpwqp23-wqwpqj--jwfewpjfpwjfq0jJWQIFnI230NInp@#(lfpsakon[]la,','-K-21J1J3RJFmx/,mx?wpjfjpw1-)DSPL!"
func benchmarkLocal(b testutil.TB) {
// Always send ~10MB worth data, so 10k x 100B strings.
list := make([]string, 1e4)
for i := range list {
list[i] = testString
}
for _, respSize := range []int{1, 10, 100} {
for _, implCase := range []struct {
name string
clientFn func(b testutil.TB, srv listv0.ListStringsServer) listv0.ListStringsClient
}{
{name: "localhost", clientFn: newLocalhostClient},
{name: "unixsocket", clientFn: newUnixSocketClient},
{name: "grpchannel", clientFn: newGRPCChannelClient},
{name: "grpchannel-nocpy", clientFn: newGRPCChannelClientNoCpy},
{name: "chan", clientFn: newGoChanClient},
{name: "buffer", clientFn: newBufferedClient},
{name: "iter", clientFn: newIterClient},
} {
b.Run(fmt.Sprintf("respSize=%v/impl=%v", respSize, implCase.name), func(b testutil.TB) {
testSrv := &List{resps: make([]*listv0.ListResponse, 0, len(list)/respSize)}
for i := 0; i < len(list); i += respSize {
testSrv.resps = append(testSrv.resps, &listv0.ListResponse{Strings: list[i : i+respSize]})
}
client := implCase.clientFn(b, testSrv)
var (
ctx = context.Background()
req = &listv0.ListRequest{}
res = &listv0.ListResponse{}
gotStrings []string
)
b.ReportAllocs()
b.ResetTimer()
for range b.N() {
stream, err := client.List(ctx, req)
testutil.Ok(b, err)
for {
err := stream.RecvMsg(res)
if err == io.EOF {
break
}
testutil.Ok(b, err)
if !b.IsBenchmark() {
testutil.Assert(b, respSize == len(res.Strings))
gotStrings = append(gotStrings, res.GetStrings()...)
}
}
}
if !b.IsBenchmark() {
testutil.Equals(b, list, gotStrings)
}
})
}
}
}
func newLocalhostClient(b testutil.TB, srv listv0.ListStringsServer) listv0.ListStringsClient {
l, err := net.Listen("tcp", "localhost:0")
testutil.Ok(b, err)
insec := insecure.NewCredentials()
grpcSrv := grpc.NewServer(grpc.Creds(insec))
listv0.RegisterListStringsServer(grpcSrv, srv)
go func() { _ = grpcSrv.Serve(l) }()
b.Cleanup(grpcSrv.Stop)
// Purposefully using grpc.WithBlock despite deprecation, for the best effort "warm up".
cc, err := grpc.NewClient(l.Addr().String(), grpc.WithTransportCredentials(insec), grpc.WithBlock())
testutil.Ok(b, err)
return listv0.NewListStringsClient(cc)
}
func newUnixSocketClient(b testutil.TB, srv listv0.ListStringsServer) listv0.ListStringsClient {
const sockAddr = "/tmp/grpc.sock"
if _, err := os.Stat(sockAddr); !os.IsNotExist(err) {
if err := os.RemoveAll(sockAddr); err != nil {
log.Fatal(err)
}
}
l, err := net.Listen("unix", sockAddr)
testutil.Ok(b, err)
insec := insecure.NewCredentials()
grpcSrv := grpc.NewServer(grpc.Creds(insec))
listv0.RegisterListStringsServer(grpcSrv, srv)
go func() { _ = grpcSrv.Serve(l) }()
b.Cleanup(grpcSrv.Stop)
// Purposefully using grpc.WithBlock despite deprecation, for the best effort "warm up".
cc, err := grpc.NewClient(
"unix:"+sockAddr,
grpc.WithTransportCredentials(insec),
grpc.WithBlock(),
)
testutil.Ok(b, err)
return listv0.NewListStringsClient(cc)
}
func newGRPCChannelClient(_ testutil.TB, srv listv0.ListStringsServer) listv0.ListStringsClient {
ch := &inprocgrpc.Channel{}
listv0.RegisterListStringsServer(ch, srv)
return listv0.NewListStringsClient(ch)
}
type nopCloner struct{}
func (nopCloner) Copy(out, in interface{}) error {
out = in
return nil
}
func (nopCloner) Clone(in interface{}) (out interface{}, _ error) {
return in, nil
}
func newGRPCChannelClientNoCpy(_ testutil.TB, srv listv0.ListStringsServer) listv0.ListStringsClient {
ch := &inprocgrpc.Channel{}
ch = ch.WithCloner(nopCloner{})
listv0.RegisterListStringsServer(ch, srv)
return listv0.NewListStringsClient(ch)
}
func newBufferedClient(_ testutil.TB, srv listv0.ListStringsServer) listv0.ListStringsClient {
return &bufferedClient{srv: srv}
}
type bufferedClient struct {
srv listv0.ListStringsServer
}
func (b *bufferedClient) List(ctx context.Context, in *listv0.ListRequest, _ ...grpc.CallOption) (listv0.ListStrings_ListClient, error) {
buf := &buffer{ctx: ctx} // buf: make([]*listv0.ListResponse, 0, 1e4)} This makes perResponse=1 much better, but =100 much worse (:
return buf, b.srv.List(in, buf)
}
type buffer struct {
listv0.ListStrings_ListServer
listv0.ListStrings_ListClient
ctx context.Context
buf []*listv0.ListResponse
received int
}
func (b *buffer) Context() context.Context {
return b.ctx
}
func (b *buffer) SendMsg(m any) error {
return b.Send(m.(*listv0.ListResponse))
}
func (b *buffer) Send(resp *listv0.ListResponse) error {
b.buf = append(b.buf, resp)
return nil
}
func (b *buffer) RecvMsg(m any) error {
r, err := b.Recv()
if err != nil {
return err
}
m.(*listv0.ListResponse).Strings = r.Strings
return nil
}
func (b *buffer) Recv() (*listv0.ListResponse, error) {
if b.received >= len(b.buf) {
return nil, io.EOF
}
r := b.buf[b.received]
b.received++
return r, nil
}
func newGoChanClient(_ testutil.TB, srv listv0.ListStringsServer) listv0.ListStringsClient {
return &chanClient{srv: srv}
}
type chanClient struct {
srv listv0.ListStringsServer
}
func (c *chanClient) List(ctx context.Context, in *listv0.ListRequest, _ ...grpc.CallOption) (listv0.ListStrings_ListClient, error) {
buf := &channel{ctx: ctx, ch: make(chan *listv0.ListResponse), errCh: make(chan error)}
go func() {
if err := c.srv.List(in, buf); err != nil {
buf.errCh <- err
}
close(buf.ch)
close(buf.errCh)
}()
return buf, nil
}
type channel struct {
listv0.ListStrings_ListServer
listv0.ListStrings_ListClient
ctx context.Context
ch chan *listv0.ListResponse
errCh chan error
}
func (c *channel) Context() context.Context {
return c.ctx
}
func (c *channel) SendMsg(m any) error {
return c.Send(m.(*listv0.ListResponse))
}
func (c *channel) Send(resp *listv0.ListResponse) error {
c.ch <- resp
return nil
}
func (c *channel) RecvMsg(m any) error {
r, err := c.Recv()
if err != nil {
return err
}
m.(*listv0.ListResponse).Strings = r.Strings
return nil
}
func (c *channel) Recv() (*listv0.ListResponse, error) {
select {
case r, ok := <-c.ch:
if !ok {
return nil, io.EOF
}
return r, nil
case err, ok := <-c.errCh:
if !ok {
return nil, io.EOF
}
return nil, err
}
}
func newIterClient(_ testutil.TB, srv listv0.ListStringsServer) listv0.ListStringsClient {
return &iterClient{srv: srv}
}
type iterClient struct {
srv listv0.ListStringsServer
}
func (c *iterClient) List(ctx context.Context, in *listv0.ListRequest, _ ...grpc.CallOption) (listv0.ListStrings_ListClient, error) {
y := &yielder{ctx: ctx}
// Pull from iter.Seq2[*listv0.ListResponse, error].
y.recv, y.stop = iter.Pull2(func(yield func(*listv0.ListResponse, error) bool) {
y.send = yield
if err := c.srv.List(in, y); err != nil {
yield(nil, err)
return
}
})
return y, nil
}
type yielder struct {
listv0.ListStrings_ListServer
listv0.ListStrings_ListClient
ctx context.Context
send func(*listv0.ListResponse, error) bool
recv func() (*listv0.ListResponse, error, bool)
stop func()
}
func (y *yielder) Context() context.Context {
return y.ctx
}
func (y *yielder) SendMsg(m any) error {
return y.Send(m.(*listv0.ListResponse))
}
func (y *yielder) RecvMsg(m any) error {
r, err := y.Recv()
if err != nil {
return err
}
m.(*listv0.ListResponse).Strings = r.Strings
return nil
}
func (y *yielder) Send(resp *listv0.ListResponse) error {
if !y.send(resp, nil) {
return errors.New("iterator stopped receiving")
}
return nil
}
func (y *yielder) Recv() (*listv0.ListResponse, error) {
r, err, ok := y.recv()
if err != nil {
y.stop()
return nil, err
}
if !ok {
return nil, io.EOF
}
return r, nil
}
type server func(send func(any))
func serve(send func(any)) {
send("yo")
time.Sleep(1 * time.Second) // Imagine a processing time.
send("are you there?")
}
func call(serve server) {
serve(func(s any) {
fmt.Println(time.Now().Second(), s)
})
}
func TestCall(t *testing.T) {
call(serve)
}
type pullClient func(recv func() (any, bool))
var _ pullClient = pullCall
func pullCall(recv func() (any, bool)) {
for {
m, ok := recv()
if !ok {
return
}
fmt.Println(time.Now().Second(), m)
}
}
func TestPullCall_Buffer(t *testing.T) {
// We have to allow pushing somewhere...
var medium []any
serve(func(m any) {
medium = append(medium, m)
})
var i int
pullCall(func() (m any, ok bool) {
if i < len(medium) {
i++
return medium[i-1], true
}
return nil, false
})
}
func TestPullCall_Iter(t *testing.T) {
var iterator iter.Seq[any] = func(yield func(any) bool) {
serve(func(a any) {
yield(a)
})
}
recv, _ := iter.Pull(iterator)
pullCall(recv)
}