-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_test.go
162 lines (151 loc) · 3.21 KB
/
bench_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package cache_bench
import (
"context"
"math"
"math/rand"
"runtime"
"testing"
"time"
"github.com/c-pro/geche"
"github.com/erni27/imcache"
"github.com/google/uuid"
)
const keyCardinality = 1000000
var numShards = 1 << (int(math.Log2(float64(runtime.NumCPU()))) + 1)
type testCase struct {
key string
op int
}
const (
OPGet = iota
OPSet
OPDel
)
func genTestData(N int) []testCase {
keys := make([]string, keyCardinality)
for i := range keys {
keys[i] = uuid.NewString()
}
d := make([]testCase, N)
for i := range d {
d[i].key = keys[rand.Intn(keyCardinality)]
r := rand.Float64()
switch {
case r < 0.9:
d[i].op = OPGet
case r >= 0.9 && r < 0.95:
d[i].op = OPSet
case r >= 0.95:
d[i].op = OPDel
}
}
return d
}
func benchmarkFuzzParallel(
c geche.Geche[string, string],
testData []testCase,
pb *testing.PB,
) {
i := 0
for pb.Next() {
switch testData[i].op {
case OPGet:
_, _ = c.Get(testData[i].key)
case OPSet:
c.Set(testData[i].key, "value")
case OPDel:
_ = c.Del(testData[i].key)
}
i = (i + 1) % len(testData)
}
}
// BenchmarkEverything performs different operations randomly.
// Ratio for get/set/del is 90/5/5
func BenchmarkEverythingParallel(b *testing.B) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
tab := []struct {
name string
imp geche.Geche[string, string]
}{
{
"MapCache",
geche.NewMapCache[string, string](),
},
{
"MapTTLCache",
geche.NewMapTTLCache[string, string](ctx, time.Second, time.Second),
},
{
"RingBuffer",
geche.NewRingBuffer[string, string](1000000),
},
{
"ShardedMapCache",
geche.NewSharded[string](
func() geche.Geche[string, string] { return geche.NewMapCache[string, string]() },
numShards,
&geche.StringMapper{},
),
},
{
"ShardedMapTTLCache",
geche.NewSharded[string](
func() geche.Geche[string, string] {
return geche.NewMapTTLCache[string, string](ctx, time.Second, time.Second)
},
numShards,
&geche.StringMapper{},
),
},
{
"ShardedRingBuffer",
geche.NewSharded[string](
func() geche.Geche[string, string] { return geche.NewRingBuffer[string, string](1000000/numShards + 1) },
numShards,
&geche.StringMapper{},
),
},
{
"github.com/Code-Hex/go-generics-cache",
NewGogLRU[string, string](ctx, time.Second, time.Second),
},
{
"github.com/Yiling-J/theine-go",
NewTheine[string, string](1000000, time.Second),
},
{
"github.com/jellydator/ttlcache",
NewTTLCache[string, string](ctx, 1000000, time.Second),
},
{
"github.com/erni27/imcache",
NewIMCache[string, string](time.Second, imcache.DefaultStringHasher64{}, numShards),
},
{
"github.com/dgraph-io/ristretto",
NewRistretto[string, string](1000000, time.Second),
},
{
"github.com/hashicorp/golang-lru/v2",
NewGLRU[string, string](1000000),
},
{
"github.com/egregors/kesh",
NewKesh[string, string](1000000),
},
{
"KVMapCache",
geche.NewKV[string](geche.NewMapCache[string, string]()),
},
}
data := genTestData(10_000_000)
b.ResetTimer()
for _, c := range tab {
b.Run(c.name, func(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
benchmarkFuzzParallel(c.imp, data, pb)
})
})
}
}