-
Notifications
You must be signed in to change notification settings - Fork 58
/
benchmark_store_test.go
568 lines (456 loc) · 13.8 KB
/
benchmark_store_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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// Copyright 2016-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package moss
import (
"bytes"
"encoding/binary"
"fmt"
"io/ioutil"
"os"
"strings"
"sync"
"testing"
"time"
)
const LargePrime = int64(9576890767)
type benchStoreSpec struct {
numItems, keySize, valSize, batchSize int
randomLoad bool // When true, use repeatably random-like keys; otherwise, sequential load.
noCopyValue bool
accesses []benchStoreSpecAccess
compactionPercentage float64 // when set to a non-zero value triggers compactions
}
type benchStoreSpecAccess struct {
after string // Run this access test after this given phase.
kind string // The kind of access: "w" (writes), "r" (reads), "" (neither).
domainFrom int // The domain of the test, from keys numbered domainFrom to domainTo.
domainTo int
ops int // The number of ops for the access test.
random bool // Whether to use repeatably random-like keys for the ops.
pctGet float32 // The pecentage of ops that should be GET's.
batchSize int
}
func BenchmarkStore_numItems1M_keySize20_valSize100_batchSize100(b *testing.B) {
benchmarkStore(b, benchStoreSpec{
numItems: 1000000, keySize: 20, valSize: 100, batchSize: 100,
})
}
func BenchmarkStore_numItems1M_keySize20_valSize100_batchSize100_randomLoad(b *testing.B) {
benchmarkStore(b, benchStoreSpec{
numItems: 1000000, keySize: 20, valSize: 100, batchSize: 100, randomLoad: true,
})
}
func BenchmarkStore_numItems1M_keySize20_valSize1000_batchSize100_randomLoad(b *testing.B) {
benchmarkStore(b, benchStoreSpec{
numItems: 1000000, keySize: 20, valSize: 1000, batchSize: 100, randomLoad: true,
})
}
func BenchmarkStore_numItems1M_keySize20_valSize100_batchSize100_ACCESSES_afterLoad_domainTo100K_ops200K_batchSize100(b *testing.B) {
benchmarkStore(b, benchStoreSpec{
numItems: 1000000, keySize: 20, valSize: 100, batchSize: 100,
noCopyValue: true,
accesses: []benchStoreSpecAccess{
{after: "load", kind: "w", domainTo: 100000, ops: 200000, random: true, batchSize: 100},
{after: "iter", kind: "r", domainTo: 100000, ops: 200000, random: true, pctGet: 1.0},
},
})
}
func BenchmarkStore_numItems1M_keySize20_valSize100_batchSize10000(b *testing.B) {
benchmarkStore(b, benchStoreSpec{
numItems: 1000000, keySize: 20, valSize: 100, batchSize: 10000,
})
}
func BenchmarkStore_numItems10M_keySize20_valSize100_batchSize1000(b *testing.B) {
benchmarkStore(b, benchStoreSpec{
numItems: 10000000, keySize: 20, valSize: 100, batchSize: 1000,
})
}
func BenchmarkStore_numItems10M_keySize20_valSize100_batchSize10000(b *testing.B) {
benchmarkStore(b, benchStoreSpec{
numItems: 10000000, keySize: 20, valSize: 100, batchSize: 10000,
})
}
func BenchmarkStore_numItems20M_keySize16_valSize0_batchSize10000_randomLoad(b *testing.B) { // Similar to Nitro VLDB test.
benchmarkStore(b, benchStoreSpec{
numItems: 20000000, keySize: 16, valSize: 0, batchSize: 10000, randomLoad: true,
accesses: []benchStoreSpecAccess{
{after: "iter", kind: "r", domainTo: 20000000, ops: 1000000, random: true, pctGet: 1.0},
},
})
}
func BenchmarkStore_numItems20M_keySize32_valSize0_batchSize10000_randomLoad(b *testing.B) { // Similar to Nitro VLDB test.
benchmarkStore(b, benchStoreSpec{
numItems: 20000000, keySize: 32, valSize: 0, batchSize: 10000, randomLoad: true,
accesses: []benchStoreSpecAccess{
{after: "iter", kind: "r", domainTo: 20000000, ops: 1000000, random: true, pctGet: 1.0},
},
})
}
func BenchmarkStore_numItems20M_keySize64_valSize0_batchSize10000_randomLoad(b *testing.B) { // Similar to Nitro VLDB test.
benchmarkStore(b, benchStoreSpec{
numItems: 20000000, keySize: 64, valSize: 0, batchSize: 10000, randomLoad: true,
accesses: []benchStoreSpecAccess{
{after: "iter", kind: "r", domainTo: 20000000, ops: 1000000, random: true, pctGet: 1.0},
},
})
}
func BenchmarkStore_numItems20M_keySize128_valSize0_batchSize10000_randomLoad(b *testing.B) { // Similar to Nitro VLDB test.
benchmarkStore(b, benchStoreSpec{
numItems: 20000000, keySize: 128, valSize: 0, batchSize: 10000, randomLoad: true,
accesses: []benchStoreSpecAccess{
{after: "iter", kind: "r", domainTo: 20000000, ops: 1000000, random: true, pctGet: 1.0},
},
})
}
func BenchmarkStore_numItems50M_keySize20_valSize100_batchSize10000(b *testing.B) {
benchmarkStore(b, benchStoreSpec{
numItems: 50000000, keySize: 20, valSize: 100, batchSize: 10000,
})
}
func BenchmarkStore_numItems50M_keySize20_valSize100_batchSize10000_ACCESSES_domainTo100K_ops1M_batchSize10K(b *testing.B) {
benchmarkStore(b, benchStoreSpec{
numItems: 50000000, keySize: 20, valSize: 100, batchSize: 10000,
accesses: []benchStoreSpecAccess{
{after: "load", kind: "w", domainTo: 100000, ops: 1000000, batchSize: 10000},
},
})
}
func BenchmarkStore_numItems100M_keySize20_valSize100_batchSize10000(b *testing.B) {
benchmarkStore(b, benchStoreSpec{
numItems: 100000000, keySize: 20, valSize: 100, batchSize: 10000,
accesses: []benchStoreSpecAccess{
{after: "iter", kind: "r", domainTo: 20000000, ops: 1000000, random: true, pctGet: 1.0},
},
})
}
func BenchmarkStore_numItems1M_keySize20_valSize100_batchSize10_compact(b *testing.B) {
benchmarkStore(b, benchStoreSpec{
numItems: 1000000, keySize: 20, valSize: 100, batchSize: 100,
compactionPercentage: 0.2,
})
}
func benchmarkStore(b *testing.B, spec benchStoreSpec) {
bufSize := spec.valSize
if bufSize < spec.keySize {
bufSize = spec.keySize
}
buf := make([]byte, bufSize)
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchmarkStoreDo(b, spec, buf)
}
}
func benchmarkStoreDo(b *testing.B, spec benchStoreSpec, buf []byte) {
tmpDir, _ := ioutil.TempDir("", "mossStoreBenchmark")
defer os.RemoveAll(tmpDir)
fmt.Printf("\n")
for i := 0; i < 180; i++ {
fmt.Printf("-")
}
fmt.Printf("\nspec: %+v\n", spec)
readOptions := ReadOptions{NoCopyValue: spec.noCopyValue}
var mu sync.Mutex
counts := map[EventKind]int{}
eventWaiters := map[EventKind]chan struct{}{}
co := CollectionOptions{
OnEvent: func(event Event) {
mu.Lock()
counts[event.Kind]++
eventWaiter := eventWaiters[event.Kind]
eventWaiters[event.Kind] = nil
mu.Unlock()
if eventWaiter != nil {
close(eventWaiter)
}
},
}
so := StoreOptions{CollectionOptions: co}
spo := StorePersistOptions{CompactionConcern: CompactionAllow}
if spec.compactionPercentage > 0 {
so.CompactionPercentage = spec.compactionPercentage
so.CompactionSync = true
spo.CompactionConcern = CompactionAllow
}
var store *Store
var coll Collection
var err error
var cumMSecs int64
var cumWMSecs int64
var cumRMSecs int64
var cumWOps int64
var cumROps int64
var cumWBytes int64
var cumRBytes int64
var phaseWOps int64
var phaseROps int64
var phaseRBytes int64
var phaseWBytes int64
phaseDo := func(phaseName, phaseKind string, addToCumulative bool, f func()) {
phaseWOps = 0
phaseROps = 0
phaseWBytes = 0
phaseRBytes = 0
phaseBegTime := time.Now()
f()
phaseEndTime := time.Now()
phaseMSecs := phaseEndTime.Sub(phaseBegTime).Nanoseconds() / 1000000.0
if addToCumulative {
cumMSecs += phaseMSecs
if strings.Index(phaseKind, "w") >= 0 {
cumWMSecs += phaseMSecs
}
if strings.Index(phaseKind, "r") >= 0 {
cumRMSecs += phaseMSecs
}
cumWOps += phaseWOps
cumROps += phaseROps
cumWBytes += phaseWBytes
cumRBytes += phaseRBytes
}
var phaseWOpsPerSec int64
var phaseROpsPerSec int64
var phaseWKBPerSec int64
var phaseRKBPerSec int64
if phaseMSecs > 0 {
phaseWOpsPerSec = 1000 * phaseWOps / phaseMSecs
phaseROpsPerSec = 1000 * phaseROps / phaseMSecs
phaseWKBPerSec = 1000 * phaseWBytes / phaseMSecs / 1024
phaseRKBPerSec = 1000 * phaseRBytes / phaseMSecs / 1024
}
var cumWOpsPerSec int64
var cumROpsPerSec int64
var cumWKBPerSec int64
var cumRKBPerSec int64
if cumWMSecs > 0 {
cumWOpsPerSec = 1000 * cumWOps / cumWMSecs
cumWKBPerSec = 1000 * cumWBytes / cumWMSecs / 1024
}
if cumRMSecs > 0 {
cumROpsPerSec = 1000 * cumROps / cumRMSecs
cumRKBPerSec = 1000 * cumRBytes / cumRMSecs / 1024
}
fmt.Printf(" %6s || time: %5d (ms) | %8d wop/s | %8d wkb/s | %8d rop/s | %8d rkb/s"+
" || cumulative: %8d wop/s | %8d wkb/s | %8d rop/s | %8d rkb/s\n",
phaseName,
phaseMSecs,
phaseWOpsPerSec,
phaseWKBPerSec,
phaseROpsPerSec,
phaseRKBPerSec,
cumWOpsPerSec,
cumWKBPerSec,
cumROpsPerSec,
cumRKBPerSec)
}
phase := func(phaseName, phaseKind string, f func()) {
phaseDo(phaseName, phaseKind, true, f)
for accessi, access := range spec.accesses {
if access.after == phaseName {
phaseDo("access", access.kind, false, func() {
fmt.Printf(" <<access %d: %+v>>\n", accessi, access)
var batch Batch
batch, err = coll.NewBatch(access.batchSize, access.batchSize*(spec.keySize+spec.valSize))
if err != nil {
b.Fatal(err)
}
var ss Snapshot
ss, err = coll.Snapshot()
if err != nil {
b.Fatal(err)
}
pos := int64(0)
domainSize64 := int64(access.domainTo - access.domainFrom)
for i := 0; i < access.ops; i++ {
clearBuf(buf)
binary.PutVarint(buf, pos+int64(access.domainFrom))
if access.random {
pos = pos + LargePrime
} else {
pos++
}
pos = pos % domainSize64
if float32(i%100)/100.0 < access.pctGet {
var v []byte
v, err = ss.Get(buf[0:spec.keySize], readOptions)
if err != nil {
b.Fatal(err)
}
phaseROps++
phaseRBytes += int64(spec.keySize + len(v))
} else {
err = batch.Set(buf[0:spec.keySize], buf[0:spec.valSize])
if err != nil {
b.Fatal(err)
}
phaseWOps++
phaseWBytes += int64(spec.keySize + spec.valSize)
if (i != 0) && (i%access.batchSize == 0) {
err = coll.ExecuteBatch(batch, WriteOptions{})
if err != nil {
b.Fatal(err)
}
batch.Close()
batch, err = coll.NewBatch(access.batchSize, access.batchSize*(spec.keySize+spec.valSize))
if err != nil {
b.Fatal(err)
}
ss.Close()
ss, err = coll.Snapshot()
if err != nil {
b.Fatal(err)
}
}
}
}
batch.Close()
ss.Close()
})
}
}
}
// ------------------------------------------------
phase("open", "", func() {
store, coll, err = OpenStoreCollection(tmpDir, so, spo)
if err != nil {
b.Fatal(err)
}
})
// ------------------------------------------------
phase("load", "w", func() {
var batch Batch
batch, err = coll.NewBatch(spec.batchSize, spec.batchSize*(spec.keySize+spec.valSize))
if err != nil {
b.Fatal(err)
}
pos := int64(0)
numItems64 := int64(spec.numItems)
for i := 0; i < spec.numItems; i++ {
clearBuf(buf)
binary.PutVarint(buf, pos)
if spec.randomLoad {
pos = (pos + LargePrime) % numItems64
} else {
pos++
}
err = batch.Set(buf[0:spec.keySize], buf[0:spec.valSize])
if err != nil {
b.Fatal(err)
}
phaseWOps++
phaseWBytes += int64(spec.keySize + spec.valSize)
if i%spec.batchSize == 0 {
err = coll.ExecuteBatch(batch, WriteOptions{})
if err != nil {
b.Fatal(err)
}
batch.Close()
batch, err = coll.NewBatch(spec.batchSize, spec.batchSize*(spec.keySize+spec.valSize))
if err != nil {
b.Fatal(err)
}
}
}
})
// ------------------------------------------------
phase("drain", "w", func() {
for {
var stats *CollectionStats
stats, err = coll.Stats()
if err != nil {
b.Fatal(b)
}
if stats.CurDirtyOps <= 0 &&
stats.CurDirtyBytes <= 0 &&
stats.CurDirtySegments <= 0 {
return
}
persistenceProgressCh := make(chan struct{})
mu.Lock()
eventWaiters[EventKindPersisterProgress] = persistenceProgressCh
mu.Unlock()
select {
case <-persistenceProgressCh:
// NO-OP.
case <-time.After(200 * time.Millisecond):
// NO-OP.
}
}
})
// ------------------------------------------------
phase("close", "", func() {
coll.Close()
store.Close()
})
// ------------------------------------------------
phase("reopen", "", func() {
store, coll, err = OpenStoreCollection(tmpDir, so, spo)
if err != nil {
b.Fatal(err)
}
})
// ------------------------------------------------
phase("iter", "r", func() {
var ss Snapshot
ss, err = coll.Snapshot()
if err != nil {
b.Fatal(err)
}
var iter Iterator
iter, err = ss.StartIterator(nil, nil, IteratorOptions{})
if err != nil {
b.Fatal(err)
}
for {
var k, v []byte
k, v, err = iter.Current()
if err == ErrIteratorDone {
break
}
phaseROps++
phaseRBytes += int64(len(k) + len(v))
if len(k) <= len(v) {
if !bytes.HasPrefix(v, k) {
b.Fatalf("wrong iter bytes")
}
} else {
if !bytes.HasPrefix(k, v) {
b.Fatalf("wrong bytes iter")
}
}
err = iter.Next()
if err == ErrIteratorDone {
break
}
}
iter.Close()
ss.Close()
})
// ------------------------------------------------
phase("close", "", func() {
coll.Close()
store.Close()
})
// ------------------------------------------------
fmt.Printf("total time: %d (ms)\n", cumMSecs)
fileInfos, err := ioutil.ReadDir(tmpDir)
if err != nil {
b.Fatal(err)
}
if len(fileInfos) != 1 {
b.Fatalf("expected just 1 file")
}
fileInfo := fileInfos[0]
fmt.Printf("file size: %d (MB), amplification: %.3f\n",
fileInfo.Size()/1000000.0,
float64(fileInfo.Size())/float64(int64(spec.numItems)*int64((spec.keySize+spec.valSize))))
}
func clearBuf(buf []byte) {
for i := 0; i < len(buf); i++ {
buf[i] = 0
}
}