-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmap_benchmark_test.go
More file actions
239 lines (217 loc) · 5.69 KB
/
cmap_benchmark_test.go
File metadata and controls
239 lines (217 loc) · 5.69 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package cmap
import (
"fmt"
"math/rand"
"runtime"
"strconv"
"sync/atomic"
"testing"
"time"
)
// GOMAXPROCS=4 go clean -testcache && go test -bench=BenchmarkCMap -benchtime=10s -benchmem .
const benchItems = 1 << 16
// BenchmarkCMapGetOnlyHits simulates a workload where the
// lookups are for keys that always exist in the map.
func BenchmarkCMapGetOnlyHits(b *testing.B) {
c, err := New()
if err != nil {
b.Fatal(err)
}
defer c.Clear()
// Pre-populate the map with a fixed set of keys.
const keyspace = 1024
v := []byte("dag")
for i := range keyspace {
c.Insert([]byte(strconv.Itoa(i)), v)
}
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
// Each goroutine gets its own random number source to avoid lock contention.
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
for pb.Next() {
// Look up a random, pre-existing key. This guarantees a cache hit.
k := []byte(strconv.Itoa(rng.Intn(keyspace)))
if _, ok, err := c.Get(k); err != nil || !ok {
panic(fmt.Errorf("failed to get key %q: %w", k, err))
}
}
})
}
// BenchmarkCMapGetBalancedHits simulates a workload where lookups are
// with a 50/50 mix of hits and misses.
func BenchmarkCMapGetBalanced(b *testing.B) {
c, err := New()
if err != nil {
b.Fatal(err)
}
defer c.Clear()
// Pre-populate the map with keys from 0 to keyspace-1.
const keyspace = 1024
v := []byte("dag")
for i := range keyspace {
c.Insert([]byte(strconv.Itoa(i)), v)
}
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
for pb.Next() {
// Look up a random key in the range [keyspace, 2*keyspace-1], (50/50).
k := []byte(strconv.Itoa(rng.Intn(keyspace) + keyspace))
if _, _, err := c.Get(k); err != nil {
panic(fmt.Errorf("failed to get key %q: %w", k, err))
}
}
})
}
// BenchmarkCMapGetOrInsertBalanced simulates a high-churn workload
// with a 50/50 mix of reads and writes.
func BenchmarkCMapGetOrInsertBalanced(b *testing.B) {
c, err := New()
if err != nil {
b.Fatal(err)
}
defer c.Clear()
const keyspace = 1024
v := []byte("dag")
for i := range keyspace {
c.Insert([]byte(strconv.Itoa(i)), v)
}
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
for pb.Next() {
k := []byte(strconv.Itoa(rng.Intn(keyspace)))
if rng.Intn(2) == 0 {
if _, ok, err := c.Get(k); err != nil || !ok {
panic(fmt.Errorf("failed to get key %q: %w", k, err))
}
} else {
// Some inserts will be updates.
if _, err := c.Insert(k, v); err != nil {
panic(fmt.Errorf("failed to insert key %q: %w", k, err))
}
}
}
})
s := &Stats{}
c.UpdateStats(s)
b.ReportMetric(float64(s.Compactions), "compactions")
}
// threadCounter is a helper for the adversarial benchmark to assign a unique-ish
// ID to each parallel goroutine.
var threadCounter int64
// BenchmarkCMapAdversarial simulates a worst-case scenario with high contention. Many
// goroutines repeatedly update and delete a small set of hot keys.
func BenchmarkCMapAdversarial(b *testing.B) {
c, err := New()
if err != nil {
b.Fatal(err)
}
defer c.Clear()
// Create a small number of hot keys, one for each CPU core,
// to ensures maximum contention.
numHotKeys := runtime.GOMAXPROCS(0)
hotKeys := make([][]byte, numHotKeys)
for i := range numHotKeys {
hotKeys[i] = []byte(strconv.Itoa(i))
}
v := []byte("dag")
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
// Simple thread counter assigns a unique index to each goroutine.
gID := atomic.AddInt64(&threadCounter, 1) - 1
k := hotKeys[int(gID)%numHotKeys]
var err error
for pb.Next() {
if _, err = c.Insert(k, v); err != nil {
panic(fmt.Errorf("failed to insert key %q: %w", k, err))
}
// Immediately delete to create churn.
if _, err = c.Delete(k); err != nil {
panic(fmt.Errorf("failed to delete key %q: %w", k, err))
}
}
})
s := &Stats{}
c.UpdateStats(s)
b.ReportMetric(float64(s.Compactions), "compactions")
}
// BenchmarkCMapInsertThroughput measures Insert throughput in a
// high conention, update-heavy workload.
func BenchmarkCMapInsertThroughput(b *testing.B) {
c, err := New()
if err != nil {
b.Fatal(err)
}
defer c.Clear()
// Normalized for operation throughput, million of ops.
b.SetBytes(benchItems)
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
k := []byte("\x00\x00\x00\x00")
v := []byte("dag")
var err error
for pb.Next() {
for range benchItems {
k[0]++
if k[0] == 0 {
k[1]++
}
_, err = c.Insert(k, v)
if err != nil {
panic(fmt.Errorf("failed to insert %q: %w", k, err))
}
}
}
})
b.ReportMetric(float64(benchItems), "items/op")
s := &Stats{}
c.UpdateStats(s)
b.ReportMetric(float64(s.Compactions/uint64(b.N)), "compactions/op")
}
// BenchmarkCMapGetThroughput measures the throughput of Get
// operations under high contention.
func BenchmarkCMapGetThroughput(b *testing.B) {
c, err := New()
if err != nil {
b.Fatal(err)
}
defer c.Clear()
// Pre-populate the map with keys.
k := []byte("\x00\x00\x00\x00")
v := []byte("dag")
for range benchItems {
k[0]++
if k[0] == 0 {
k[1]++
}
_, err := c.Insert(k, v)
if err != nil {
panic(fmt.Errorf("failed to insert: %w", err))
}
}
b.ReportAllocs()
b.SetBytes(benchItems)
b.RunParallel(func(pb *testing.PB) {
k := []byte("\x00\x00\x00\x00")
var err error
var ok bool
for pb.Next() {
for range benchItems {
k[0]++
if k[0] == 0 {
k[1]++
}
_, ok, err = c.Get(k)
if err != nil || !ok {
panic(fmt.Errorf("failed to get key %q: %w", k, err))
}
}
}
})
b.ReportMetric(float64(benchItems), "items/op")
}