-
Notifications
You must be signed in to change notification settings - Fork 2
/
benchmark_test.go
100 lines (81 loc) · 1.8 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
100
package cache
import (
"testing"
"time"
"github.com/Comcast/goburrow-cache/synthetic"
)
const (
testMaxSize = 512
benchmarkThreshold = 100
)
type sameGenerator int
func (g sameGenerator) Int() int {
return int(g)
}
func BenchmarkSame(b *testing.B) {
g := sameGenerator(1)
benchmarkCache(b, g)
}
func BenchmarkUniform(b *testing.B) {
distintKeys := testMaxSize * 2
g := synthetic.Uniform(0, distintKeys)
benchmarkCache(b, g)
}
func BenchmarkUniformLess(b *testing.B) {
distintKeys := testMaxSize
g := synthetic.Uniform(0, distintKeys)
benchmarkCache(b, g)
}
func BenchmarkCounter(b *testing.B) {
g := synthetic.Counter(0)
benchmarkCache(b, g)
}
func BenchmarkExponential(b *testing.B) {
g := synthetic.Exponential(1.0)
benchmarkCache(b, g)
}
func BenchmarkZipf(b *testing.B) {
items := testMaxSize * 10
g := synthetic.Zipf(0, items, 1.01)
benchmarkCache(b, g)
}
func BenchmarkHotspot(b *testing.B) {
items := testMaxSize * 2
g := synthetic.Hotspot(0, items, 0.25)
benchmarkCache(b, g)
}
func benchmarkCache(b *testing.B, g synthetic.Generator) {
c := New(WithMaximumSize(testMaxSize))
defer c.Close()
intCh := make(chan int, 100)
go func(n int) {
for i := 0; i < n; i++ {
intCh <- g.Int()
}
}(b.N)
defer close(intCh)
if b.N > benchmarkThreshold {
defer printStats(b, c, time.Now())
}
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
k := <-intCh
_, ok := c.GetIfPresent(k)
if !ok {
c.Put(k, k)
}
}
})
}
func printStats(b *testing.B, c Cache, start time.Time) {
dur := time.Since(start)
var st Stats
c.Stats(&st)
b.Logf("total: %d (%s), hits: %d (%.2f%%), misses: %d (%.2f%%), evictions: %d\n",
st.RequestCount(), dur,
st.HitCount, st.HitRate()*100.0,
st.MissCount, st.MissRate()*100.0,
st.EvictionCount)
}