-
Notifications
You must be signed in to change notification settings - Fork 105
/
benchmark.go
440 lines (339 loc) · 8.17 KB
/
benchmark.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// Package benchmark provides internally used benchmark support
package benchmark
import (
"math/rand"
"sort"
"strings"
"testing"
"time"
"github.com/google/btree"
"github.com/openacid/low/mathext/zipf"
"github.com/openacid/low/size"
"github.com/openacid/slim/benchhelper"
"github.com/openacid/slim/encode"
"github.com/openacid/slim/trie"
)
// Config defines the variable inputs struct in one benchmark.
type Config struct {
KeyCnt int
KeyLen int
ValLen int
}
// SearchResult show the key search result with a constructed data.
// Used to transfer benchmark result currently.
// SearchResult also defines the column titles when output to a chart.
type SearchResult struct {
KeyCnt int
KeyLen int
ExistingKeyNsPerOp int64
NonexistentKeyNsPerOp int64
}
// GetResult represent the ns/Get() for various key count and several predefined
// key length = 64, 128, 256
type GetResult struct {
KeyCount int `tw-title:"key-count"`
K64 int `tw-title:"k=64"`
K128 int `tw-title:"k=128"`
K256 int `tw-title:"k=256"`
}
// MSABResult defines the ns/Get() for Map, SlimTrie, Array and Btree.
type MSABResult struct {
KeyCount int `tw-title:"key-count"`
Map int `tw-title:"map"`
Slim int `tw-title:"SlimTrie"`
Array int `tw-title:"array"`
Btree int `tw-title:"Btree"`
}
// FPRResult represent the false positive rate.
type FPRResult struct {
KeyCount int `tw-title:"key-count"`
FPR float64 `tw-title:"fpr" tw-fmt:"%.3f%%"`
}
// MemResult is a alias of GetResult
type MemResult GetResult
var Rec int32
// BenchGet benchmark the Get().
func BenchGet(keyCounts []int, typ, workload string) []GetResult {
var rst = make([]GetResult, 0, len(keyCounts))
for _, n := range keyCounts {
r := GetResult{
KeyCount: n,
K64: benchGet(NewGetSetting(n, 64), typ, workload),
K128: benchGet(NewGetSetting(n, 128), typ, workload),
K256: benchGet(NewGetSetting(n, 256), typ, workload),
}
rst = append(rst, r)
}
return rst
}
// GetFPR estimate false positive rate(FPR) for Get.
func GetFPR(keyCounts []int) []FPRResult {
var rst = make([]FPRResult, 0, len(keyCounts))
keyLen := 64
r := 100
for _, n := range keyCounts {
keys := benchhelper.RandSortedStrings(n, keyLen, nil)
nAbsent := n * r
present := map[string]bool{}
for _, k := range keys {
present[k] = true
}
vals := make([]uint16, n)
st, err := trie.NewSlimTrie(encode.U16{}, keys, vals)
if err != nil {
panic(err)
}
fp := float64(0)
for i := 0; i < nAbsent; {
k := benchhelper.RandString(keyLen, nil)
if _, ok := present[k]; ok {
continue
}
_, found := st.Get(k)
if found {
fp++
}
i++
}
fpr := fp / float64(nAbsent)
r := FPRResult{
KeyCount: n,
FPR: fpr,
}
rst = append(rst, r)
}
return rst
}
func Mem(keyCounts []int) []MemResult {
rst := make([]MemResult, 0)
for _, n := range keyCounts {
r := MemResult{
KeyCount: n,
K64: int(slimtrieMem(n, 64)),
K128: int(slimtrieMem(n, 128)),
K256: int(slimtrieMem(n, 256)),
}
rst = append(rst, r)
}
return rst
}
func slimtrieMem(keyCnt, keyLen int) int64 {
keys := benchhelper.RandSortedStrings(keyCnt, keyLen, nil)
t, err := trie.NewSlimTrie(encode.U16{}, keys, nil)
if err != nil {
panic(err)
}
sz := size.Of(t)
return int64(sz) * 8 / int64(keyCnt)
}
func benchGet(setting *GetSetting, typ string, workload string) int {
var keys []string
if typ == "present" {
keys = setting.Keys
} else {
keys = setting.AbsentKeys
}
st := setting.SlimKV
n := len(keys)
mask := maxMask(n)
accesses := newWorkLoad(workload, n)
var rec int32
rst := testing.Benchmark(
func(b *testing.B) {
for i := 0; i < b.N; i++ {
v := st.Get(keys[accesses[i&mask]])
rec += v
}
})
Rec = rec
return int(rst.NsPerOp())
}
func GetMapSlimArrayBtree(keyCounts []int, workload string) []MSABResult {
var rst = make([]MSABResult, 0, len(keyCounts))
for _, n := range keyCounts {
mp := benchGet_map_slim_array_btree(NewGetSetting(n, 64), "present", workload)
r := MSABResult{
KeyCount: n,
Map: mp["map"],
Slim: mp["slim"],
Array: mp["array"],
Btree: mp["btree"],
}
rst = append(rst, r)
}
return rst
}
var OutputMSAB int32 = 0
func benchGet_map_slim_array_btree(setting *GetSetting, typ string, workload string) map[string]int {
gst := setting
var keys []string
nsops := make(map[string]int)
if typ == "present" {
keys = setting.Keys
} else {
keys = setting.AbsentKeys
}
n := len(keys)
mask := maxMask(n)
accesses := newWorkLoad(workload, n)
v := int32(0)
rst := testing.Benchmark(
func(b *testing.B) {
for i := 0; i < b.N; i++ {
v += gst.SlimKV.Get(keys[accesses[i&mask]])
}
})
nsops["slim"] = int(rst.NsPerOp())
rst = testing.Benchmark(
func(b *testing.B) {
for i := 0; i < b.N; i++ {
v += gst.Map[keys[accesses[i&mask]]]
}
})
nsops["map"] = int(rst.NsPerOp())
rst = testing.Benchmark(
func(b *testing.B) {
for i := 0; i < b.N; i++ {
itm := &KVElt{Key: keys[accesses[i&mask]], Val: gst.Values[i&mask]}
ee := gst.Btree.Get(itm)
v += ee.(*KVElt).Val
}
})
nsops["btree"] = int(rst.NsPerOp())
rst = testing.Benchmark(
func(b *testing.B) {
for i := 0; i < b.N; i++ {
v += sortedArraySearch(keys, gst.Values, keys[accesses[i&mask]])
}
})
nsops["array"] = int(rst.NsPerOp())
OutputMSAB += v
return nsops
}
func sortedArraySearch(keys []string, values []int32, searchKey string) int32 {
n := len(keys)
idx := sort.Search(
n,
func(i int) bool {
return strings.Compare(keys[i], searchKey) >= 0
},
)
if idx < n && strings.Compare(keys[idx], searchKey) == 0 {
return values[idx]
}
return -1
}
func NewGetSetting(cnt int, keyLen int) *GetSetting {
ks := benchhelper.RandSortedStrings(cnt*2, keyLen, nil)
keys := make([]string, cnt)
absentKeys := make([]string, cnt)
for i := 0; i < cnt; i++ {
keys[i] = ks[i*2]
absentKeys[i] = ks[i*2+1]
}
vals := make([]int32, cnt)
for i := 0; i < cnt; i++ {
vals[i] = int32(i)
}
elts := makeKVElts(keys, vals)
st, err := trie.NewSlimTrie(encode.I32{}, keys, vals)
if err != nil {
panic(err)
}
// make test map
m := make(map[string]int32, cnt)
for i := 0; i < len(keys); i++ {
m[keys[i]] = vals[i]
}
// make test btree
bt := btree.New(32)
for _, v := range elts {
bt.ReplaceOrInsert(v)
}
// get search key
r := rand.New(rand.NewSource(time.Now().Unix()))
idx := r.Int63n(int64(cnt))
searchKey := keys[idx]
searchVal := vals[idx]
return &GetSetting{
Keys: keys,
Values: vals,
AbsentKeys: absentKeys,
SlimKV: &slimKV{Elts: elts, slim: st},
Map: m,
Btree: bt,
SearchKey: searchKey,
SearchValue: searchVal,
}
}
// GetSetting defines benchmark data source.
type GetSetting struct {
Keys []string
Values []int32
AbsentKeys []string
SlimKV *slimKV
Map map[string]int32
Btree *btree.BTree
SearchKey string
SearchValue int32
}
type slimKV struct {
// SlimTrie as an index
slim *trie.SlimTrie
// full key-values
Elts []*KVElt
}
func (s *slimKV) Get(key string) int32 {
idx, found := s.slim.GetI32(key)
if !found {
return -1
}
elt := s.Elts[idx]
if elt.Key != key {
return -1
}
return elt.Val
}
func maxMask(n int) int {
mask := 1
for ; (mask<<1 | 1) <= n; mask = mask<<1 | 1 {
}
return mask
}
func newWorkLoad(workload string, n int) []int {
if workload == "zipf" {
return accessesZipf(n)
} else if workload == "scan" {
return accessesScan(n)
}
panic("unknown workload:" + workload)
}
func accessesZipf(n int) []int {
times := maxMask(n) + 1
return zipf.Accesses(1, 1.5, n, times, nil)
}
func accessesScan(n int) []int {
times := maxMask(n) + 1
a := make([]int, times)
for i := 0; i < times; i++ {
a[i] = i
}
return a
}
// KVElt defines a key-value struct to be used as a value in SlimTrie in test.
type KVElt struct {
Key string
Val int32
}
// Less is used to implements google/btree.Item
func (kv *KVElt) Less(than btree.Item) bool {
o := than.(*KVElt)
return kv.Key < o.Key
}
func makeKVElts(srcKeys []string, srcVals []int32) []*KVElt {
elts := make([]*KVElt, len(srcKeys))
for i, k := range srcKeys {
elts[i] = &KVElt{Key: k, Val: srcVals[i]}
}
return elts
}