Debian Codegraph @

bench/bench_test.go

raw
1 //go:build !trace
2
3 package bench_test
4
5 import (
6 "fmt"
7 "math/rand"
8 "runtime"
9 "testing"
10
11 sieve "github.com/opencoff/go-sieve"
12
13 arc "github.com/hashicorp/golang-lru/arc/v2"
14 lru "github.com/hashicorp/golang-lru/v2"
15 )
16
17 // BenchmarkGet_Parallel measures concurrent read throughput.
18 func BenchmarkGet_Parallel(b *testing.B) {
19 const cacheSize = 8192
20
21 b.Run("Sieve", func(b *testing.B) {
22 c := sieve.Must(sieve.New[int, int](cacheSize))
23 for i := 0; i < cacheSize; i++ {
24 c.Add(i, i)
25 }
26 b.ResetTimer()
27 b.RunParallel(func(pb *testing.PB) {
28 r := rand.New(rand.NewSource(rand.Int63()))
29 for pb.Next() {
30 c.Get(r.Intn(cacheSize))
31 }
32 })
33 })
34
35 b.Run("LRU", func(b *testing.B) {
36 c, _ := lru.New[int, int](cacheSize)
37 for i := 0; i < cacheSize; i++ {
38 c.Add(i, i)
39 }
40 b.ResetTimer()
41 b.RunParallel(func(pb *testing.PB) {
42 r := rand.New(rand.NewSource(rand.Int63()))
43 for pb.Next() {
44 c.Get(r.Intn(cacheSize))
45 }
46 })
47 })
48
49 b.Run("ARC", func(b *testing.B) {
50 c, _ := arc.NewARC[int, int](cacheSize)
51 for i := 0; i < cacheSize; i++ {
52 c.Add(i, i)
53 }
54 b.ResetTimer()
55 b.RunParallel(func(pb *testing.PB) {
56 r := rand.New(rand.NewSource(rand.Int63()))
57 for pb.Next() {
58 c.Get(r.Intn(cacheSize))
59 }
60 })
61 })
62 }
63
64 // BenchmarkAdd_Parallel measures concurrent write throughput with eviction.
65 func BenchmarkAdd_Parallel(b *testing.B) {
66 const cacheSize = 8192
67 const keyRange = cacheSize * 2
68
69 b.Run("Sieve", func(b *testing.B) {
70 c := sieve.Must(sieve.New[int, int](cacheSize))
71 b.ResetTimer()
72 b.RunParallel(func(pb *testing.PB) {
73 r := rand.New(rand.NewSource(rand.Int63()))
74 for pb.Next() {
75 k := r.Intn(keyRange)
76 c.Add(k, k)
77 }
78 })
79 })
80
81 b.Run("LRU", func(b *testing.B) {
82 c, _ := lru.New[int, int](cacheSize)
83 b.ResetTimer()
84 b.RunParallel(func(pb *testing.PB) {
85 r := rand.New(rand.NewSource(rand.Int63()))
86 for pb.Next() {
87 k := r.Intn(keyRange)
88 c.Add(k, k)
89 }
90 })
91 })
92
93 b.Run("ARC", func(b *testing.B) {
94 c, _ := arc.NewARC[int, int](cacheSize)
95 b.ResetTimer()
96 b.RunParallel(func(pb *testing.PB) {
97 r := rand.New(rand.NewSource(rand.Int63()))
98 for pb.Next() {
99 k := r.Intn(keyRange)
100 c.Add(k, k)
101 }
102 })
103 })
104 }
105
106 // BenchmarkProbe_Parallel measures concurrent Probe (get-or-insert)
107 // throughput. SIEVE only: LRU and ARC have no semantically-equivalent
108 // method — their PeekOrAdd/ContainsOrAdd skip recency promotion, which
109 // would degrade eviction quality. See bench/README.md for details.
110 func BenchmarkProbe_Parallel(b *testing.B) {
111 const cacheSize = 8192
112 const keyRange = cacheSize * 2
113
114 b.Run("Sieve", func(b *testing.B) {
115 c := sieve.Must(sieve.New[int, int](cacheSize))
116 b.ResetTimer()
117 b.RunParallel(func(pb *testing.PB) {
118 r := rand.New(rand.NewSource(rand.Int63()))
119 for pb.Next() {
120 k := r.Intn(keyRange)
121 c.Probe(k, k)
122 }
123 })
124 })
125 }
126
127 // BenchmarkDelete_Parallel measures concurrent Delete/Remove throughput.
128 // The cache is pre-filled with keyRange entries (keyRange > cacheSize so
129 // eviction fires during pre-fill). Goroutines then delete random keys
130 // from [0, keyRange); the hit/miss mix shifts toward "not present" as
131 // the cache drains, which is representative of delete traffic under
132 // churn.
133 func BenchmarkDelete_Parallel(b *testing.B) {
134 const cacheSize = 8192
135 const keyRange = cacheSize * 2
136
137 b.Run("Sieve", func(b *testing.B) {
138 c := sieve.Must(sieve.New[int, int](cacheSize))
139 for i := 0; i < keyRange; i++ {
140 c.Add(i, i)
141 }
142 b.ResetTimer()
143 b.RunParallel(func(pb *testing.PB) {
144 r := rand.New(rand.NewSource(rand.Int63()))
145 for pb.Next() {
146 c.Delete(r.Intn(keyRange))
147 }
148 })
149 })
150
151 b.Run("LRU", func(b *testing.B) {
152 c, _ := lru.New[int, int](cacheSize)
153 for i := 0; i < keyRange; i++ {
154 c.Add(i, i)
155 }
156 b.ResetTimer()
157 b.RunParallel(func(pb *testing.PB) {
158 r := rand.New(rand.NewSource(rand.Int63()))
159 for pb.Next() {
160 c.Remove(r.Intn(keyRange))
161 }
162 })
163 })
164
165 b.Run("ARC", func(b *testing.B) {
166 c, _ := arc.NewARC[int, int](cacheSize)
167 for i := 0; i < keyRange; i++ {
168 c.Add(i, i)
169 }
170 b.ResetTimer()
171 b.RunParallel(func(pb *testing.PB) {
172 r := rand.New(rand.NewSource(rand.Int63()))
173 for pb.Next() {
174 c.Remove(r.Intn(keyRange))
175 }
176 })
177 })
178 }
179
180 // BenchmarkMixed_Parallel measures 60% Get / 30% Add / 10% Delete.
181 func BenchmarkMixed_Parallel(b *testing.B) {
182 const cacheSize = 8192
183 const keyRange = cacheSize * 2
184
185 b.Run("Sieve", func(b *testing.B) {
186 c := sieve.Must(sieve.New[int, int](cacheSize))
187 for i := 0; i < cacheSize; i++ {
188 c.Add(i, i)
189 }
190 b.ResetTimer()
191 b.RunParallel(func(pb *testing.PB) {
192 r := rand.New(rand.NewSource(rand.Int63()))
193 for pb.Next() {
194 k := r.Intn(keyRange)
195 op := r.Intn(10)
196 switch {
197 case op < 6: // 60% Get
198 c.Get(k)
199 case op < 9: // 30% Add
200 c.Add(k, k)
201 default: // 10% Delete
202 c.Delete(k)
203 }
204 }
205 })
206 })
207
208 b.Run("LRU", func(b *testing.B) {
209 c, _ := lru.New[int, int](cacheSize)
210 for i := 0; i < cacheSize; i++ {
211 c.Add(i, i)
212 }
213 b.ResetTimer()
214 b.RunParallel(func(pb *testing.PB) {
215 r := rand.New(rand.NewSource(rand.Int63()))
216 for pb.Next() {
217 k := r.Intn(keyRange)
218 op := r.Intn(10)
219 switch {
220 case op < 6:
221 c.Get(k)
222 case op < 9:
223 c.Add(k, k)
224 default:
225 c.Remove(k)
226 }
227 }
228 })
229 })
230
231 b.Run("ARC", func(b *testing.B) {
232 c, _ := arc.NewARC[int, int](cacheSize)
233 for i := 0; i < cacheSize; i++ {
234 c.Add(i, i)
235 }
236 b.ResetTimer()
237 b.RunParallel(func(pb *testing.PB) {
238 r := rand.New(rand.NewSource(rand.Int63()))
239 for pb.Next() {
240 k := r.Intn(keyRange)
241 op := r.Intn(10)
242 switch {
243 case op < 6:
244 c.Get(k)
245 case op < 9:
246 c.Add(k, k)
247 default:
248 c.Remove(k)
249 }
250 }
251 })
252 })
253 }
254
255 // BenchmarkMemoryFootprint measures heap allocation delta at various cache sizes.
256 //
257 // The heap-bytes/heap-objects metrics are the point of this benchmark;
258 // the ns/op column is construction+fill time and should be ignored.
259 // (The timer must keep running — with a stopped timer the framework
260 // would scale b.N toward huge iteration counts.)
261 func BenchmarkMemoryFootprint(b *testing.B) {
262 for _, size := range []int{100_000, 500_000, 1_000_000} {
263 name := formatSize(size)
264
265 b.Run(name+"/Sieve", func(b *testing.B) {
266 for range b.N {
267 var before, after runtime.MemStats
268 runtime.GC()
269 runtime.ReadMemStats(&before)
270
271 c := sieve.Must(sieve.New[int, int](size))
272 for i := 0; i < size; i++ {
273 c.Add(i, i)
274 }
275
276 runtime.GC()
277 runtime.ReadMemStats(&after)
278 b.ReportMetric(float64(after.HeapAlloc-before.HeapAlloc), "heap-bytes")
279 b.ReportMetric(float64(after.HeapObjects-before.HeapObjects), "heap-objects")
280 runtime.KeepAlive(c)
281 }
282 })
283
284 b.Run(name+"/LRU", func(b *testing.B) {
285 for range b.N {
286 var before, after runtime.MemStats
287 runtime.GC()
288 runtime.ReadMemStats(&before)
289
290 c, _ := lru.New[int, int](size)
291 for i := 0; i < size; i++ {
292 c.Add(i, i)
293 }
294
295 runtime.GC()
296 runtime.ReadMemStats(&after)
297 b.ReportMetric(float64(after.HeapAlloc-before.HeapAlloc), "heap-bytes")
298 b.ReportMetric(float64(after.HeapObjects-before.HeapObjects), "heap-objects")
299 runtime.KeepAlive(c)
300 }
301 })
302
303 b.Run(name+"/ARC", func(b *testing.B) {
304 for range b.N {
305 var before, after runtime.MemStats
306 runtime.GC()
307 runtime.ReadMemStats(&before)
308
309 c, _ := arc.NewARC[int, int](size)
310 for i := 0; i < size; i++ {
311 c.Add(i, i)
312 }
313
314 runtime.GC()
315 runtime.ReadMemStats(&after)
316 b.ReportMetric(float64(after.HeapAlloc-before.HeapAlloc), "heap-bytes")
317 b.ReportMetric(float64(after.HeapObjects-before.HeapObjects), "heap-objects")
318 runtime.KeepAlive(c)
319 }
320 })
321 }
322 }
323
324 // BenchmarkGCImpact measures GC pause times at 1M entries under mixed workload.
325 func BenchmarkGCImpact(b *testing.B) {
326 const size = 1_000_000
327 const keyRange = size * 2
328
329 b.Run("Sieve", func(b *testing.B) {
330 c := sieve.Must(sieve.New[int, int](size))
331 for i := 0; i < size; i++ {
332 c.Add(i, i)
333 }
334
335 // Snapshot before the timed loop: PauseTotalNs/NumGC are
336 // process-cumulative, so reporting them directly would fold in
337 // warmup fill and earlier sub-benchmarks. Report the delta.
338 var s0 runtime.MemStats
339 runtime.ReadMemStats(&s0)
340 b.ResetTimer()
341 for range b.N {
342 r := rand.New(rand.NewSource(rand.Int63()))
343 // Do some mixed ops to keep the cache active
344 for j := 0; j < 1000; j++ {
345 k := r.Intn(keyRange)
346 if r.Intn(2) == 0 {
347 c.Get(k)
348 } else {
349 c.Add(k, k)
350 }
351 }
352 runtime.GC()
353 }
354 b.StopTimer()
355 var s1 runtime.MemStats
356 runtime.ReadMemStats(&s1)
357 if ngc := s1.NumGC - s0.NumGC; ngc > 0 {
358 b.ReportMetric(float64(s1.PauseTotalNs-s0.PauseTotalNs)/float64(ngc), "avg-gc-pause-ns")
359 }
360 })
361
362 b.Run("LRU", func(b *testing.B) {
363 c, _ := lru.New[int, int](size)
364 for i := 0; i < size; i++ {
365 c.Add(i, i)
366 }
367
368 var s0 runtime.MemStats
369 runtime.ReadMemStats(&s0)
370 b.ResetTimer()
371 for range b.N {
372 r := rand.New(rand.NewSource(rand.Int63()))
373 for j := 0; j < 1000; j++ {
374 k := r.Intn(keyRange)
375 if r.Intn(2) == 0 {
376 c.Get(k)
377 } else {
378 c.Add(k, k)
379 }
380 }
381 runtime.GC()
382 }
383 b.StopTimer()
384 var s1 runtime.MemStats
385 runtime.ReadMemStats(&s1)
386 if ngc := s1.NumGC - s0.NumGC; ngc > 0 {
387 b.ReportMetric(float64(s1.PauseTotalNs-s0.PauseTotalNs)/float64(ngc), "avg-gc-pause-ns")
388 }
389 })
390
391 b.Run("ARC", func(b *testing.B) {
392 c, _ := arc.NewARC[int, int](size)
393 for i := 0; i < size; i++ {
394 c.Add(i, i)
395 }
396
397 var s0 runtime.MemStats
398 runtime.ReadMemStats(&s0)
399 b.ResetTimer()
400 for range b.N {
401 r := rand.New(rand.NewSource(rand.Int63()))
402 for j := 0; j < 1000; j++ {
403 k := r.Intn(keyRange)
404 if r.Intn(2) == 0 {
405 c.Get(k)
406 } else {
407 c.Add(k, k)
408 }
409 }
410 runtime.GC()
411 }
412 b.StopTimer()
413 var s1 runtime.MemStats
414 runtime.ReadMemStats(&s1)
415 if ngc := s1.NumGC - s0.NumGC; ngc > 0 {
416 b.ReportMetric(float64(s1.PauseTotalNs-s0.PauseTotalNs)/float64(ngc), "avg-gc-pause-ns")
417 }
418 })
419 }
420
421 // BenchmarkZipf_Get_Parallel measures concurrent read throughput under Zipfian
422 // access distribution, comparing Sieve vs LRU vs ARC.
423 func BenchmarkZipf_Get_Parallel(b *testing.B) {
424 const cacheSize = 8192
425 const keyRange = cacheSize * 2
426 const seqLen = 256 << 10 // 256K samples
427
428 for _, skew := range []float64{1.01, 1.20, 1.50} {
429 name := fmt.Sprintf("s=%.2f", skew)
430
431 b.Run(name+"/Sieve", func(b *testing.B) {
432 c := sieve.Must(sieve.New[int, int](cacheSize))
433 seq := zipfSequence(seqLen, keyRange, skew, 42)
434 for _, k := range seq {
435 if _, ok := c.Get(k); !ok {
436 c.Add(k, k)
437 }
438 }
439 b.ResetTimer()
440 b.RunParallel(func(pb *testing.PB) {
441 local := shuffled(seq, rand.Int63())
442 n := len(local)
443 i := 0
444 for pb.Next() {
445 c.Get(local[i%n])
446 i++
447 }
448 })
449 })
450
451 b.Run(name+"/LRU", func(b *testing.B) {
452 c, _ := lru.New[int, int](cacheSize)
453 seq := zipfSequence(seqLen, keyRange, skew, 42)
454 for _, k := range seq {
455 if _, ok := c.Get(k); !ok {
456 c.Add(k, k)
457 }
458 }
459 b.ResetTimer()
460 b.RunParallel(func(pb *testing.PB) {
461 local := shuffled(seq, rand.Int63())
462 n := len(local)
463 i := 0
464 for pb.Next() {
465 c.Get(local[i%n])
466 i++
467 }
468 })
469 })
470
471 b.Run(name+"/ARC", func(b *testing.B) {
472 c, _ := arc.NewARC[int, int](cacheSize)
473 seq := zipfSequence(seqLen, keyRange, skew, 42)
474 for _, k := range seq {
475 if _, ok := c.Get(k); !ok {
476 c.Add(k, k)
477 }
478 }
479 b.ResetTimer()
480 b.RunParallel(func(pb *testing.PB) {
481 local := shuffled(seq, rand.Int63())
482 n := len(local)
483 i := 0
484 for pb.Next() {
485 c.Get(local[i%n])
486 i++
487 }
488 })
489 })
490 }
491 }
492
493 func zipfSequence(n, keySpace int, s float64, seed int64) []int {
494 r := rand.New(rand.NewSource(seed))
495 z := rand.NewZipf(r, s, 1.0, uint64(keySpace-1))
496 out := make([]int, n)
497 for i := range out {
498 out[i] = int(z.Uint64())
499 }
500 return out
501 }
502
503 func shuffled(src []int, seed int64) []int {
504 dst := make([]int, len(src))
505 copy(dst, src)
506 r := rand.New(rand.NewSource(seed))
507 r.Shuffle(len(dst), func(i, j int) {
508 dst[i], dst[j] = dst[j], dst[i]
509 })
510 return dst
511 }
512
513 func formatSize(n int) string {
514 switch {
515 case n >= 1_000_000:
516 return fmt.Sprintf("%dM", n/1_000_000)
517 case n >= 1_000:
518 return fmt.Sprintf("%dK", n/1_000)
519 default:
520 return fmt.Sprintf("%d", n)
521 }
522 }