-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_benchmark_test.go
More file actions
91 lines (79 loc) · 1.64 KB
/
cache_benchmark_test.go
File metadata and controls
91 lines (79 loc) · 1.64 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
package loadingcache_test
import (
"context"
"testing"
"time"
"github.com/devzero-inc/loadingcache"
)
func BenchmarkGetMiss(b *testing.B) {
type K int
type V int
matrixBenchmark(b,
nil,
noopBenchmarkSetupFunc[K, V](),
func(b *testing.B, cache loadingcache.Cache[K, V]) {
for i := 0; i < b.N; i++ {
_, _ = cache.Get(context.TODO(), K(i))
}
})
}
func BenchmarkGetHit(b *testing.B) {
type K int
type V string
matrixBenchmark(b,
nil,
func(b *testing.B, cache loadingcache.Cache[K, V]) {
cache.Put(1, "a")
},
func(b *testing.B, cache loadingcache.Cache[K, V]) {
for i := 0; i < b.N; i++ {
_, err := cache.Get(context.TODO(), 1)
if err != nil {
panic(err)
}
}
})
}
func BenchmarkPutNew(b *testing.B) {
type K int
type V int
matrixBenchmark(b,
nil,
noopBenchmarkSetupFunc[K, V](),
func(b *testing.B, cache loadingcache.Cache[K, V]) {
for i := 0; i < b.N; i++ {
cache.Put(K(i), 1)
}
})
}
func BenchmarkPutNewNoPreWrite(b *testing.B) {
type K int
type V int
matrixBenchmark(b,
[]loadingcache.CacheOption[K, V]{
loadingcache.WithBackgroundEvictFrequency[K, V](time.Second),
},
noopBenchmarkSetupFunc[K, V](),
func(b *testing.B, cache loadingcache.Cache[K, V]) {
for i := 0; i < b.N; i++ {
cache.Put(K(i), 1)
}
})
}
func BenchmarkPutReplace(b *testing.B) {
cache := loadingcache.New[string, int]()
cache.Put("a", 1)
b.ResetTimer()
for i := 0; i < b.N; i++ {
cache.Put("a", 1)
}
}
func BenchmarkPutAtMaxSize(b *testing.B) {
cache := loadingcache.New(
loadingcache.WithMaxSize[int, int](1),
)
b.ResetTimer()
for i := 0; i < b.N; i++ {
cache.Put(i, 1)
}
}