-
Notifications
You must be signed in to change notification settings - Fork 13
/
benchmark_test.go
99 lines (87 loc) · 2.16 KB
/
benchmark_test.go
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
package sturdyc_test
import (
"testing"
"time"
"github.com/creativecreature/sturdyc"
)
type benchmarkMetric[T any] struct {
getOps int
setOps int
hits int
evictions int
}
func (b *benchmarkMetric[T]) recordGet(c *sturdyc.Client[T], key string) {
b.getOps++
_, ok := c.Get(key)
if ok {
b.hits++
}
}
func (b *benchmarkMetric[T]) recordSet(c *sturdyc.Client[T], key string, value T) {
b.setOps++
evict := c.Set(key, value)
if evict {
b.evictions++
}
}
type benchmarkMetrics[T any] []benchmarkMetric[T]
func (metrics benchmarkMetrics[T]) hitRate() (float64, string) {
var ops, hits int
for _, metrics := range metrics {
ops += metrics.getOps
hits += metrics.hits
}
return float64(hits) / float64(ops), "hits/op"
}
func (metrics benchmarkMetrics[T]) evictions() (float64, string) {
var ops, evictions int
for _, metrics := range metrics {
ops += metrics.setOps
evictions += metrics.evictions
}
return float64(evictions) / float64(ops), "evictions/op"
}
func BenchmarkGetConcurrent(b *testing.B) {
cacheKey := "key"
capacity := 1_000_000
numShards := 100
ttl := time.Hour
evictionPercentage := 5
c := sturdyc.New[string](capacity, numShards, ttl, evictionPercentage,
sturdyc.WithNoContinuousEvictions(),
)
c.Set(cacheKey, "value")
metrics := make(benchmarkMetrics[string], 0)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
var metric benchmarkMetric[string]
for pb.Next() {
metric.recordGet(c, cacheKey)
}
metrics = append(metrics, metric)
})
b.StopTimer()
b.ReportMetric(metrics.hitRate())
}
func BenchmarkSetConcurrent(b *testing.B) {
capacity := 10_000_000
numShards := 10_000
ttl := time.Hour
evictionPercentage := 5
c := sturdyc.New[string](capacity, numShards, ttl, evictionPercentage,
sturdyc.WithNoContinuousEvictions(),
)
metrics := make(benchmarkMetrics[string], 0)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
var metric benchmarkMetric[string]
for pb.Next() {
// NOTE: The benchmark includes the time for generating random keys.
key := randKey(16)
metric.recordSet(c, key, "value")
}
metrics = append(metrics, metric)
})
b.StopTimer()
b.ReportMetric(metrics.evictions())
}