Debian Codegraph @

pkg/twcache/lru_bench_test.go

raw
1 package twcache
2
3 import (
4 "strconv"
5 "testing"
6 )
7
8 func BenchmarkLRU_GetHit(b *testing.B) {
9 cache := NewLRU[string, int](100)
10 for i := 0; i < 100; i++ {
11 cache.Add(strconv.Itoa(i), i)
12 }
13 b.ResetTimer()
14 b.RunParallel(func(pb *testing.PB) {
15 i := 0
16 for pb.Next() {
17 cache.Get(strconv.Itoa(i % 100))
18 i++
19 }
20 })
21 }
22
23 func BenchmarkLRU_GetMiss(b *testing.B) {
24 cache := NewLRU[string, int](100)
25 for i := 0; i < 50; i++ {
26 cache.Add(strconv.Itoa(i), i)
27 }
28 b.ResetTimer()
29 b.RunParallel(func(pb *testing.PB) {
30 i := 0
31 for pb.Next() {
32 cache.Get("miss_" + strconv.Itoa(i))
33 i++
34 }
35 })
36 }
37
38 func BenchmarkLRU_AddNew(b *testing.B) {
39 cache := NewLRU[string, int](1000)
40 b.ResetTimer()
41 b.RunParallel(func(pb *testing.PB) {
42 i := 0
43 for pb.Next() {
44 cache.Add(strconv.Itoa(i), i)
45 i++
46 }
47 })
48 }
49
50 func BenchmarkLRU_AddUpdate(b *testing.B) {
51 cache := NewLRU[string, int](100)
52 for i := 0; i < 100; i++ {
53 cache.Add(strconv.Itoa(i), i)
54 }
55 b.ResetTimer()
56 b.RunParallel(func(pb *testing.PB) {
57 i := 0
58 for pb.Next() {
59 cache.Add(strconv.Itoa(i%100), i)
60 i++
61 }
62 })
63 }
64
65 func BenchmarkLRU_GetOrCompute_Hit(b *testing.B) {
66 cache := NewLRU[string, int](100)
67 for i := 0; i < 100; i++ {
68 cache.Add(strconv.Itoa(i), i)
69 }
70 compute := func() int { return 42 }
71 b.ResetTimer()
72 b.RunParallel(func(pb *testing.PB) {
73 i := 0
74 for pb.Next() {
75 cache.GetOrCompute(strconv.Itoa(i%100), compute)
76 i++
77 }
78 })
79 }
80
81 func BenchmarkLRU_GetOrCompute_Miss(b *testing.B) {
82 cache := NewLRU[string, int](1000)
83 compute := func() int { return 42 }
84 b.ResetTimer()
85 b.RunParallel(func(pb *testing.PB) {
86 i := 0
87 for pb.Next() {
88 cache.GetOrCompute("key_"+strconv.Itoa(i), compute)
89 i++
90 }
91 })
92 }