-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
105 lines (89 loc) · 1.77 KB
/
main.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
101
102
103
104
105
package main
import (
"runtime"
"time"
)
type Benchmark struct {
Generator
N int
}
func main() {
cacheSize := []int{1e3, 10e3, 100e3, 1e6}
multiplier := []int{10, 100, 1000}
newCache := []NewCacheFunc{
NewTinyLFU,
NewClockPro,
NewARC,
NewRistretto,
NewDirectCache,
NewTwoQueue,
NewGroupCacheLRU,
NewHashicorpLRU,
NewS4LRU,
NewSLRU,
NewWTFCache,
}
newGen := []NewGeneratorFunc{
NewScrambledZipfian,
// NewHotspot,
// NewUniform,
}
var results []*BenchmarkResult
for _, newGen := range newGen {
for _, cacheSize := range cacheSize {
for _, multiplier := range multiplier {
numKey := cacheSize * multiplier
for _, newCache := range newCache {
result := run(newGen, cacheSize, numKey, newCache)
results = append(results, result)
}
if len(results) > 0 {
printResults(results)
results = results[:0]
}
}
}
}
}
func run(newGen NewGeneratorFunc, cacheSize, numKey int, newCache NewCacheFunc) *BenchmarkResult {
gen := newGen(numKey)
b := &Benchmark{
Generator: gen,
N: 1e6,
}
alloc1 := memAlloc()
cache := newCache(cacheSize)
defer cache.Close()
start := time.Now()
hits, misses := bench(b, cache)
dur := time.Since(start)
alloc2 := memAlloc()
return &BenchmarkResult{
GenName: gen.Name(),
CacheName: cache.Name(),
CacheSize: cacheSize,
NumKey: numKey,
Hits: hits,
Misses: misses,
Duration: dur,
Bytes: int64(alloc2) - int64(alloc1),
}
}
func bench(b *Benchmark, cache Cache) (hits, misses int) {
for i := 0; i < b.N; i++ {
value := b.Next()
if cache.Get(value) {
hits++
} else {
misses++
cache.Set(value)
}
}
return hits, misses
}
func memAlloc() uint64 {
runtime.GC()
var m runtime.MemStats
runtime.ReadMemStats(&m)
return m.Alloc
}