-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathring_bench_test.go
More file actions
509 lines (438 loc) · 12.1 KB
/
ring_bench_test.go
File metadata and controls
509 lines (438 loc) · 12.1 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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
package ring
import (
"sync"
"sync/atomic"
"testing"
"time"
)
// =============================================================================
// Strategy Benchmarks
// =============================================================================
// BenchmarkWriterStrategy benchmarks each strategy
func BenchmarkWriterStrategy(b *testing.B) {
strategies := []RetryStrategy{
SleepBackoff,
NextShard,
RandomShard,
SpinThenYield,
AutoAdaptive,
}
for _, strategy := range strategies {
b.Run(strategy.String(), func(b *testing.B) {
ring, _ := NewShardedRing(1000000, 8)
config := WriteConfig{
Strategy: strategy,
MaxRetries: 10,
BackoffDuration: 100 * time.Microsecond,
}
writer := NewWriter(ring, 0, config)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
writer.Write(i)
// Drain frequently to prevent filling (every 100 writes)
if i%100 == 99 {
for j := 0; j < 100; j++ {
ring.TryRead()
}
}
}
})
}
}
// BenchmarkWriterVsWriteWithBackoff compares Writer to WriteWithBackoff
func BenchmarkWriterVsWriteWithBackoff(b *testing.B) {
b.Run("WriteWithBackoff", func(b *testing.B) {
ring, _ := NewShardedRing(1000000, 8)
config := WriteConfig{
MaxRetries: 10,
BackoffDuration: 100 * time.Microsecond,
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
ring.WriteWithBackoff(0, i, config)
if i%100 == 99 {
for j := 0; j < 100; j++ {
ring.TryRead()
}
}
}
})
b.Run("Writer_SleepBackoff", func(b *testing.B) {
ring, _ := NewShardedRing(1000000, 8)
config := WriteConfig{
Strategy: SleepBackoff,
MaxRetries: 10,
BackoffDuration: 100 * time.Microsecond,
}
writer := NewWriter(ring, 0, config)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
writer.Write(i)
if i%100 == 99 {
for j := 0; j < 100; j++ {
ring.TryRead()
}
}
}
})
}
// =============================================================================
// Core Benchmarks
// =============================================================================
// BenchmarkWrite benchmarks single-threaded write performance
func BenchmarkWrite(b *testing.B) {
ring, _ := NewShardedRing(1000000, 8)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
ring.Write(uint64(i), i)
// Read to prevent ring from filling
if i%100 == 99 {
for j := 0; j < 100; j++ {
ring.TryRead()
}
}
}
}
// BenchmarkTryRead benchmarks single-threaded read performance
func BenchmarkTryRead(b *testing.B) {
ring, _ := NewShardedRing(1000000, 8)
// Pre-fill ring
for i := 0; i < 500000; i++ {
ring.Write(uint64(i), i)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, ok := ring.TryRead(); !ok {
// Refill if empty
for j := 0; j < 10000; j++ {
ring.Write(uint64(j), j)
}
}
}
}
// BenchmarkReadBatch benchmarks batch read performance
func BenchmarkReadBatch(b *testing.B) {
b.Run("batch_10", func(b *testing.B) {
benchmarkReadBatchSize(b, 10)
})
b.Run("batch_100", func(b *testing.B) {
benchmarkReadBatchSize(b, 100)
})
b.Run("batch_1000", func(b *testing.B) {
benchmarkReadBatchSize(b, 1000)
})
}
func benchmarkReadBatchSize(b *testing.B, batchSize int) {
ring, _ := NewShardedRing(1000000, 8)
// Pre-fill ring
for i := 0; i < 500000; i++ {
ring.Write(uint64(i), i)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
batch := ring.ReadBatch(batchSize)
if len(batch) == 0 {
// Refill if empty
for j := 0; j < batchSize*10; j++ {
ring.Write(uint64(j), j)
}
}
}
}
// BenchmarkReadBatchIntoPool benchmarks zero-allocation batch read using sync.Pool
func BenchmarkReadBatchIntoPool(b *testing.B) {
b.Run("batch_10", func(b *testing.B) {
benchmarkReadBatchIntoPoolSize(b, 10)
})
b.Run("batch_100", func(b *testing.B) {
benchmarkReadBatchIntoPoolSize(b, 100)
})
b.Run("batch_1000", func(b *testing.B) {
benchmarkReadBatchIntoPoolSize(b, 1000)
})
}
func benchmarkReadBatchIntoPoolSize(b *testing.B, batchSize int) {
ring, _ := NewShardedRing(1000000, 8)
// Use pointer type to avoid int->any boxing allocations
type item struct{ val int }
// Pre-allocate items to reuse (simulating real usage with pooled objects)
items := make([]*item, 500000)
for i := range items {
items[i] = &item{val: i}
}
// Pre-fill ring with pointers (no boxing allocation)
for i := 0; i < 500000; i++ {
ring.Write(uint64(i), items[i])
}
// Create pool for batch slices
pool := sync.Pool{
New: func() any {
return make([]any, 0, batchSize)
},
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
// Get buffer from pool
buf := pool.Get().([]any)
// Read into pooled buffer
buf = ring.ReadBatchInto(buf, batchSize)
if len(buf) == 0 {
// Refill if empty (reuse same items)
for j := 0; j < batchSize*10 && j < len(items); j++ {
ring.Write(uint64(j), items[j])
}
}
// Return buffer to pool
pool.Put(buf[:0])
}
}
// BenchmarkConcurrentWrite benchmarks concurrent write performance
func BenchmarkConcurrentWrite(b *testing.B) {
b.Run("1_producer", func(b *testing.B) {
benchmarkConcurrentWriteN(b, 1)
})
b.Run("2_producers", func(b *testing.B) {
benchmarkConcurrentWriteN(b, 2)
})
b.Run("4_producers", func(b *testing.B) {
benchmarkConcurrentWriteN(b, 4)
})
b.Run("8_producers", func(b *testing.B) {
benchmarkConcurrentWriteN(b, 8)
})
}
func benchmarkConcurrentWriteN(b *testing.B, numProducers int) {
ring, _ := NewShardedRing(10000000, 8)
b.ResetTimer()
b.ReportAllocs()
b.SetParallelism(numProducers)
b.RunParallel(func(pb *testing.PB) {
producerID := uint64(0)
i := 0
for pb.Next() {
ring.Write(producerID, i)
i++
// Prevent filling by periodically reading
if i%1000 == 0 {
for j := 0; j < 100; j++ {
ring.TryRead()
}
}
}
})
}
// BenchmarkProducerConsumer benchmarks write-then-read cycle
func BenchmarkProducerConsumer(b *testing.B) {
ring, _ := NewShardedRing(10000, 8)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
// Write
ring.Write(uint64(i%8), i)
// Read periodically to prevent filling
if i%100 == 99 {
for j := 0; j < 100; j++ {
ring.TryRead()
}
}
}
}
// BenchmarkWriteContention benchmarks write contention with many producers on same shard
func BenchmarkWriteContention(b *testing.B) {
ring, _ := NewShardedRing(10000000, 1) // Single shard = maximum contention
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
ring.Write(0, i) // All write to same shard
i++
if i%1000 == 0 {
for j := 0; j < 100; j++ {
ring.TryRead()
}
}
}
})
}
// BenchmarkWriteNoContention benchmarks write with no contention (each goroutine has own shard)
func BenchmarkWriteNoContention(b *testing.B) {
ring, _ := NewShardedRing(10000000, 64) // Many shards
b.ResetTimer()
b.ReportAllocs()
var producerCounter atomic.Uint64
b.RunParallel(func(pb *testing.PB) {
producerID := producerCounter.Add(1) - 1
i := 0
for pb.Next() {
ring.Write(producerID, i) // Each producer to own shard
i++
if i%1000 == 0 {
for j := 0; j < 100; j++ {
ring.TryRead()
}
}
}
})
}
// BenchmarkShardCount benchmarks impact of shard count on performance
func BenchmarkShardCount(b *testing.B) {
b.Run("01_shards", func(b *testing.B) {
benchmarkShardCountN(b, 1)
})
b.Run("02_shards", func(b *testing.B) {
benchmarkShardCountN(b, 2)
})
b.Run("04_shards", func(b *testing.B) {
benchmarkShardCountN(b, 4)
})
b.Run("08_shards", func(b *testing.B) {
benchmarkShardCountN(b, 8)
})
b.Run("16_shards", func(b *testing.B) {
benchmarkShardCountN(b, 16)
})
b.Run("32_shards", func(b *testing.B) {
benchmarkShardCountN(b, 32)
})
}
func benchmarkShardCountN(b *testing.B, numShards uint64) {
ring, _ := NewShardedRing(1000000, numShards)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
ring.Write(uint64(i), i)
if i%100 == 99 {
for j := 0; j < 100; j++ {
ring.TryRead()
}
}
}
}
// BenchmarkThroughput measures sustained throughput with concurrent producers
func BenchmarkThroughput(b *testing.B) {
ring, _ := NewShardedRing(10000000, 8)
b.ResetTimer()
b.ReportAllocs()
var counter atomic.Uint64
b.RunParallel(func(pb *testing.PB) {
producerID := counter.Add(1) - 1
i := 0
for pb.Next() {
ring.Write(producerID, i)
i++
// Periodic drain to prevent filling
if i%1000 == 0 {
for j := 0; j < 100; j++ {
ring.TryRead()
}
}
}
})
}
// BenchmarkTryReadRotation benchmarks TryRead with rotating shard start.
//
// Background: TryRead uses a rotating start shard (readStartShard) to ensure
// fair distribution across shards. Without rotation, shard 0 would always be
// checked first, leading to uneven draining.
//
// Key finding: We tested atomic vs non-atomic for readStartShard counter.
// Since this is MPSC (single consumer), atomic is unnecessary. Results:
//
// | Shards | Atomic (ns/op) | Non-Atomic (ns/op) | Improvement |
// |--------|----------------|-------------------|-------------|
// | 4 | ~27.6 | ~23.7 | ~14% faster |
// | 8 | ~33.5 | ~28.9 | ~14% faster |
// | 16 | ~35.2 | ~31.5 | ~11% faster |
//
// Note: The B/op shown here comes from boxing int→any during Write refills,
// NOT from TryRead. See BenchmarkTryReadRotationZeroAlloc for proof.
func BenchmarkTryReadRotation(b *testing.B) {
b.Run("4_shards", func(b *testing.B) {
benchmarkTryReadRotationN(b, 4)
})
b.Run("8_shards", func(b *testing.B) {
benchmarkTryReadRotationN(b, 8)
})
b.Run("16_shards", func(b *testing.B) {
benchmarkTryReadRotationN(b, 16)
})
}
func benchmarkTryReadRotationN(b *testing.B, numShards uint64) {
ring, _ := NewShardedRing(numShards*10000, numShards)
// Pre-fill ring evenly across shards
for i := uint64(0); i < numShards*5000; i++ {
ring.Write(i%numShards, int(i))
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, ok := ring.TryRead(); !ok {
// Refill if empty
for j := uint64(0); j < numShards*100; j++ {
ring.Write(j%numShards, int(j))
}
}
}
}
// BenchmarkTryReadRotationZeroAlloc proves TryRead is zero-allocation when using
// pointer types (the recommended production pattern).
//
// Background: BenchmarkTryReadRotation shows 2-6 B/op, which might suggest
// TryRead allocates. This benchmark proves those allocations come from boxing
// value types (int→any) during Write, NOT from TryRead itself.
//
// Key finding: Using pointer types (*item) instead of value types (int):
//
// | Shards | int boxing (B/op) | *item pointers (B/op) |
// |--------|-------------------|----------------------|
// | 4 | 2 | 0 |
// | 8 | 5 | 0 |
//
// Bonus: Pointer types are also faster (~38% for 4 shards) because no boxing
// overhead is incurred during Write operations.
//
// Production recommendation: Always store pointer types (e.g., *Packet) in the
// ring, typically obtained from sync.Pool. This achieves true zero-allocation
// steady-state operation as documented in the README.
func BenchmarkTryReadRotationZeroAlloc(b *testing.B) {
b.Run("4_shards", func(b *testing.B) {
benchmarkTryReadRotationZeroAllocN(b, 4)
})
b.Run("8_shards", func(b *testing.B) {
benchmarkTryReadRotationZeroAllocN(b, 8)
})
}
func benchmarkTryReadRotationZeroAllocN(b *testing.B, numShards uint64) {
ring, _ := NewShardedRing(numShards*10000, numShards)
// Use pointer type to avoid boxing allocations
type item struct{ val int }
// Pre-allocate items (simulating real usage with pooled objects)
items := make([]*item, numShards*5000)
for i := range items {
items[i] = &item{val: i}
}
// Pre-fill ring with pointers (no boxing allocation)
for i := uint64(0); i < numShards*5000; i++ {
ring.Write(i%numShards, items[i])
}
b.ResetTimer()
b.ReportAllocs()
refillIdx := 0
for i := 0; i < b.N; i++ {
if _, ok := ring.TryRead(); !ok {
// Refill with pre-allocated pointers (no allocation)
for j := 0; j < int(numShards*100) && refillIdx < len(items); j++ {
ring.Write(uint64(j)%numShards, items[refillIdx%len(items)])
refillIdx++
}
}
}
}