-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbench_test.go
More file actions
488 lines (451 loc) · 12.8 KB
/
Copy pathbench_test.go
File metadata and controls
488 lines (451 loc) · 12.8 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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// Copyright 2026 The Go Language Server Authors. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
package jsonrpc2
import (
"context"
"net"
"strconv"
"sync"
"testing"
)
var (
benchmarkDecodeMinimal = []byte(`{"jsonrpc":"2.0","method":"void","id":1}`)
benchmarkDecodeMedium = []byte(`{"jsonrpc":"2.0","method":"textDocument/hover","params":{"textDocument":{"uri":"file:///tmp/example.go"},"position":{"line":123,"character":45}},"id":99}`)
benchmarkDecodeLarge64KiB = makeBenchmarkLargeCall(64 << 10)
benchmarkDecodeLarge1MiB = makeBenchmarkLargeCall(1 << 20)
benchmarkMessage Message
benchmarkRequests []*ParsedMessage
benchmarkViews []ParsedMessageView
benchmarkView MessageView
benchmarkFrame FrameView
benchmarkEncoded []byte
)
type benchmarkDecodeEnvelopeCase struct {
name string
input []byte
batch bool
wantErr bool
}
var benchmarkDecodeEnvelopeCases = []benchmarkDecodeEnvelopeCase{
{"Call", benchmarkDecodeMedium, false, false},
{"StringID", []byte(`{"jsonrpc":"2.0","method":"textDocument/hover","params":{"line":1},"id":"abc"}`), false, false},
{"Notification", []byte(`{"jsonrpc":"2.0","method":"window/logMessage","params":{"type":3,"message":"hello"}}`), false, false},
{"Response", []byte(`{"jsonrpc":"2.0","id":99,"result":{"contents":{"kind":"markdown","value":"hello"}}}`), false, false},
{"ErrorResponse", []byte(`{"jsonrpc":"2.0","id":99,"error":{"code":-32602,"message":"invalid params","data":{"field":"position"}}}`), false, false},
{"EscapedMethodAndID", []byte(`{"jsonrpc":"2.0","method":"textDocument\/hover\n","params":{"method":"nested"},"id":"a\u002fb"}`), false, false},
{"NestedMethodInParams", []byte(`{"jsonrpc":"2.0","method":"outer","params":{"method":"inner","id":7},"id":1}`), false, false},
{"DuplicateTopLevelFields", []byte(`{"jsonrpc":"2.0","method":"first","method":"second","params":null,"id":1}`), false, false},
{"LargeParams64KiB", benchmarkDecodeLarge64KiB, false, false},
{"LargeParams1MiB", benchmarkDecodeLarge1MiB, false, false},
{"InvalidJSON", []byte(`{"jsonrpc":"2.0","method":`), false, true},
{"InvalidJSONRPC", []byte(`{"jsonrpc":"1.0","method":"m","id":1}`), false, true},
{"BatchRequests", []byte(`[{"jsonrpc":"2.0","method":"one","id":1},{"jsonrpc":"2.0","method":"two","params":{"x":2},"id":2},{"jsonrpc":"2.0","method":"note","params":[1,2,3]}]`), true, false},
{"MixedBatchRequests", []byte(`[{"jsonrpc":"2.0","method":"one","id":1},{"jsonrpc":"2.0","method":1,"id":2},{"jsonrpc":"2.0","id":3,"result":true},{"jsonrpc":"2.0","method":"note"}]`), true, false},
}
func makeBenchmarkLargeCall(n int) []byte {
const prefix = `{"jsonrpc":"2.0","method":"large","params":{"text":"`
const suffix = `"},"id":1}`
out := make([]byte, 0, len(prefix)+n+len(suffix))
out = append(out, prefix...)
for range n {
out = append(out, 'a')
}
out = append(out, suffix...)
return out
}
// BenchmarkVoidRoundTrip measures the headline hot path: a call with nil params to
// a handler that replies with a nil result, in the default synchronous dispatch
// mode, over an in-memory net.Pipe. It reports ns/op and allocs/op.
func BenchmarkVoidRoundTrip(b *testing.B) {
ctx := b.Context()
ca, cb := net.Pipe()
client := NewConn(NewNDJSONStream(ca))
server := NewConn(NewNDJSONStream(cb))
client.Go(ctx, MethodNotFoundHandler)
server.Go(ctx, func(ctx context.Context, req *Request) (any, error) {
return nil, nil
})
defer func() {
_ = client.Close()
<-client.Done()
_ = server.Close()
<-server.Done()
}()
// Warm up one round-trip before timing.
if _, err := client.Call(ctx, "void", nil, nil); err != nil {
b.Fatalf("warmup: %v", err)
}
b.ReportAllocs()
for b.Loop() {
if _, err := client.Call(ctx, "void", nil, nil); err != nil {
b.Fatalf("Call: %v", err)
}
}
}
func BenchmarkDecodeMinimalLegacy(b *testing.B) {
msg := benchmarkDecodeMinimal
b.ReportAllocs()
for b.Loop() {
got, err := DecodeMessage(msg)
if err != nil {
b.Fatalf("DecodeMessage: %v", err)
}
benchmarkMessage = got
}
}
func BenchmarkDecodeMediumLegacy(b *testing.B) {
msg := benchmarkDecodeMedium
b.ReportAllocs()
for b.Loop() {
got, err := DecodeMessage(msg)
if err != nil {
b.Fatalf("DecodeMessage: %v", err)
}
benchmarkMessage = got
}
}
func BenchmarkDecodeEnvelope(b *testing.B) {
for _, tc := range benchmarkDecodeEnvelopeCases {
b.Run(tc.name, func(b *testing.B) {
b.ReportAllocs()
b.SetBytes(int64(len(tc.input)))
if tc.batch {
for b.Loop() {
got, err := ParseRequests(tc.input)
if tc.wantErr {
if err == nil {
b.Fatalf("ParseRequests succeeded, want error")
}
continue
}
if err != nil {
b.Fatalf("ParseRequests: %v", err)
}
benchmarkRequests = got
}
return
}
for b.Loop() {
got, err := DecodeMessage(tc.input)
if tc.wantErr {
if err == nil {
b.Fatalf("DecodeMessage succeeded, want error")
}
benchmarkMessage = nil
continue
}
if err != nil {
b.Fatalf("DecodeMessage: %v", err)
}
benchmarkMessage = got
}
})
}
}
func BenchmarkDecodeViewEnvelope(b *testing.B) {
for _, tc := range benchmarkDecodeEnvelopeCases {
if tc.batch {
continue
}
b.Run(tc.name, func(b *testing.B) {
b.ReportAllocs()
b.SetBytes(int64(len(tc.input)))
for b.Loop() {
got, err := ScanMessageView(tc.input)
if tc.wantErr {
if err == nil {
b.Fatalf("ScanMessageView succeeded, want error")
}
benchmarkView = MessageView{}
continue
}
if err != nil {
b.Fatalf("ScanMessageView: %v", err)
}
benchmarkView = got
}
})
}
}
func BenchmarkDecodeRequestViewsEnvelope(b *testing.B) {
for _, tc := range benchmarkDecodeEnvelopeCases {
b.Run(tc.name, func(b *testing.B) {
b.ReportAllocs()
b.SetBytes(int64(len(tc.input)))
for b.Loop() {
got, err := ScanRequestViews(tc.input)
if err != nil {
b.Fatalf("ScanRequestViews: %v", err)
}
if tc.wantErr {
if len(got) == 0 || got[0].Err == nil {
b.Fatalf("ScanRequestViews succeeded, want per-entry error")
}
benchmarkViews = got
continue
}
benchmarkViews = got
}
})
}
}
func BenchmarkDecodeAppendRequestViewsEnvelope(b *testing.B) {
for _, tc := range benchmarkDecodeEnvelopeCases {
b.Run(tc.name, func(b *testing.B) {
var views []ParsedMessageView
if tc.batch {
views = make([]ParsedMessageView, 0, 8)
} else {
views = make([]ParsedMessageView, 0, 1)
}
b.ReportAllocs()
b.SetBytes(int64(len(tc.input)))
for b.Loop() {
got, err := AppendRequestViews(views[:0], tc.input)
if err != nil {
b.Fatalf("AppendRequestViews: %v", err)
}
if tc.wantErr {
if len(got) == 0 || got[0].Err == nil {
b.Fatalf("AppendRequestViews succeeded, want per-entry error")
}
benchmarkViews = got
continue
}
benchmarkViews = got
}
})
}
}
func BenchmarkDecodeMinimalFastView(b *testing.B) {
msg := benchmarkDecodeMinimal
b.ReportAllocs()
for b.Loop() {
got, err := ScanMessageView(msg)
if err != nil {
b.Fatalf("ScanMessageView: %v", err)
}
benchmarkView = got
}
}
func BenchmarkAppendEnvelope(b *testing.B) {
call := NewCall(NewNumberID(1), "textDocument/hover", RawMessage(`{"line":1}`))
batch := []Message{
call,
NewNotification("$/progress", RawMessage(`{"token":1}`)),
NewResponse(NewNumberID(1), RawMessage(`{"ok":true}`), nil),
}
b.Run("EncodeMessage/Call", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
got, err := EncodeMessage(call)
if err != nil {
b.Fatalf("EncodeMessage: %v", err)
}
benchmarkEncoded = got
}
})
b.Run("AppendMessage/Call", func(b *testing.B) {
buf := make([]byte, 0, 128)
b.ReportAllocs()
for b.Loop() {
benchmarkEncoded = AppendMessage(buf[:0], call)
}
})
b.Run("AppendCall/Call", func(b *testing.B) {
buf := make([]byte, 0, 128)
b.ReportAllocs()
for b.Loop() {
benchmarkEncoded = AppendCall(buf[:0], call.ID(), call.Method(), call.Params())
}
})
b.Run("AppendResponse/Result", func(b *testing.B) {
buf := make([]byte, 0, 128)
b.ReportAllocs()
for b.Loop() {
benchmarkEncoded = AppendResponse(buf[:0], NewNumberID(1), RawMessage(`{"ok":true}`), nil)
}
})
b.Run("AppendBatch/Mixed", func(b *testing.B) {
buf := make([]byte, 0, 384)
b.ReportAllocs()
for b.Loop() {
benchmarkEncoded = AppendBatch(buf[:0], batch)
}
})
}
func BenchmarkDecodeMediumFastView(b *testing.B) {
msg := benchmarkDecodeMedium
b.ReportAllocs()
for b.Loop() {
got, err := ScanMessageView(msg)
if err != nil {
b.Fatalf("ScanMessageView: %v", err)
}
benchmarkView = got
}
}
func BenchmarkDecodeMinimalView(b *testing.B) {
msg := benchmarkDecodeMinimal
b.ReportAllocs()
for b.Loop() {
got, err := ScanFrameView(msg)
if err != nil {
b.Fatalf("ScanFrameView: %v", err)
}
benchmarkFrame = got
}
}
func BenchmarkDecodeMediumView(b *testing.B) {
msg := benchmarkDecodeMedium
b.ReportAllocs()
for b.Loop() {
got, err := ScanFrameView(msg)
if err != nil {
b.Fatalf("ScanFrameView: %v", err)
}
benchmarkFrame = got
}
}
// BenchmarkSyncClientVoidRoundTrip measures the A1c synchronous-client mode: the
// caller owns the read loop (no background reader goroutine on the client side),
// so a void round trip collapses the dedicated-reader-to-Call hand-off that
// BenchmarkVoidRoundTrip pays. The server is an ordinary Conn with a background
// reader, so this is a real RPC over a real net.Pipe transport. It is a distinct
// mode (no concurrent calls, no server-initiated requests), reported separately
// from the concurrent Conn round trip.
func BenchmarkSyncClientVoidRoundTrip(b *testing.B) {
ctx := b.Context()
ca, cb := net.Pipe()
client, err := NewSyncClient(NewNDJSONStream(ca))
if err != nil {
b.Fatalf("NewSyncClient: %v", err)
}
server := NewConn(NewNDJSONStream(cb))
server.Go(ctx, func(ctx context.Context, req *Request) (any, error) {
return nil, nil
})
defer func() {
_ = client.Close()
_ = server.Close()
<-server.Done()
}()
if _, err := client.Call(ctx, "void", nil, nil); err != nil {
b.Fatalf("warmup: %v", err)
}
b.ReportAllocs()
for b.Loop() {
if _, err := client.Call(ctx, "void", nil, nil); err != nil {
b.Fatalf("Call: %v", err)
}
}
}
func BenchmarkPipelineClientVoidRoundTrip(b *testing.B) {
for _, inflight := range []int{1, 8, 64, 256} {
b.Run("Inflight"+strconv.Itoa(inflight), func(b *testing.B) {
benchmarkPipelinedVoidRoundTrip(b, inflight, func(stream Stream) Conn {
return NewPipelineClient(stream)
})
})
}
}
func BenchmarkConnPipelinedVoidRoundTrip(b *testing.B) {
for _, inflight := range []int{1, 8, 64, 256} {
b.Run("Inflight"+strconv.Itoa(inflight), func(b *testing.B) {
benchmarkPipelinedVoidRoundTrip(b, inflight, func(stream Stream) Conn {
return NewConn(stream)
})
})
}
}
func benchmarkPipelinedVoidRoundTrip(b *testing.B, inflight int, newClient func(Stream) Conn) {
ctx := b.Context()
ca, cb := net.Pipe()
client := newClient(NewNDJSONStream(ca))
server := NewServer(NewNDJSONStream(cb))
client.Go(ctx, MethodNotFoundHandler)
server.Go(ctx, func(ctx context.Context, req *Request) (any, error) {
return nil, nil
})
defer func() {
_ = client.Close()
<-client.Done()
_ = server.Close()
<-server.Done()
}()
if _, err := client.Call(ctx, "void", nil, nil); err != nil {
b.Fatalf("warmup: %v", err)
}
var firstErr error
var firstErrMu sync.Mutex
recordErr := func(err error) {
if err == nil {
return
}
firstErrMu.Lock()
if firstErr == nil {
firstErr = err
}
firstErrMu.Unlock()
}
b.ReportAllocs()
b.ReportMetric(float64(inflight), "calls/op")
for b.Loop() {
if inflight == 1 {
if _, err := client.Call(ctx, "void", nil, nil); err != nil {
b.Fatalf("Call: %v", err)
}
continue
}
var wg sync.WaitGroup
wg.Add(inflight)
for range inflight {
go func() {
defer wg.Done()
_, err := client.Call(ctx, "void", nil, nil)
recordErr(err)
}()
}
wg.Wait()
if firstErr != nil {
b.Fatalf("Call: %v", firstErr)
}
}
}
func BenchmarkOutgoingCallSlotsAddTake(b *testing.B) {
waiters := makeBenchmarkWaiters(256)
b.ReportAllocs()
for b.Loop() {
var slots outgoingCallSlots
for i, w := range waiters {
slots.Add(NewNumberID(int64(i+1)), w)
}
for i := range waiters {
if _, ok := slots.Take(NewNumberID(int64(i + 1))); !ok {
b.Fatalf("Take(%d) failed", i+1)
}
}
}
}
func BenchmarkDenseCallSlotsAddTake(b *testing.B) {
waiters := makeBenchmarkWaiters(256)
b.ReportAllocs()
for b.Loop() {
var slots denseCallSlots
for i, w := range waiters {
slots.Add(NewNumberID(int64(i+1)), w)
}
for i := range waiters {
if _, ok := slots.Take(NewNumberID(int64(i + 1))); !ok {
b.Fatalf("Take(%d) failed", i+1)
}
}
}
}
func makeBenchmarkWaiters(n int) []*waiter {
waiters := make([]*waiter, n)
for i := range waiters {
waiters[i] = &waiter{ready: make(chan struct{}, 1)}
}
return waiters
}