forked from cockroachdb/pebble
/
scan.go
160 lines (136 loc) · 4.02 KB
/
scan.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
// Copyright 2018 The LevelDB-Go and Pebble Authors. All rights reserved. Use
// of this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
package main
import (
"fmt"
"log"
"math"
"sync"
"sync/atomic"
"time"
"github.com/cockroachdb/pebble"
"github.com/cockroachdb/pebble/internal/randvar"
"github.com/spf13/cobra"
"golang.org/x/exp/rand"
)
var scanConfig struct {
reverse bool
rows *randvar.Flag
values *randvar.BytesFlag
}
var scanCmd = &cobra.Command{
Use: "scan <dir>",
Short: "run the scan benchmark",
Long: ``,
Args: cobra.ExactArgs(1),
Run: runScan,
}
func init() {
scanCmd.Flags().BoolVarP(
&scanConfig.reverse, "reverse", "r", false, "reverse scan")
scanConfig.rows = randvar.NewFlag("100")
scanCmd.Flags().Var(
scanConfig.rows, "rows", "number of rows to scan in each operation")
scanConfig.values = randvar.NewBytesFlag("8")
scanCmd.Flags().Var(
scanConfig.values, "values",
"value size distribution [{zipf,uniform}:]min[-max][/<target-compression>]")
}
func runScan(cmd *cobra.Command, args []string) {
var (
bytes int64
scanned int64
lastBytes int64
lastScanned int64
lastElapsed time.Duration
)
opts := pebble.Sync
if disableWAL {
opts = pebble.NoSync
}
rowDist := scanConfig.rows
runTest(args[0], test{
init: func(d DB, wg *sync.WaitGroup) {
const count = 100000
const batch = 1000
rng := rand.New(rand.NewSource(1449168817))
keys := make([][]byte, count)
for i := 0; i < count; {
b := d.NewBatch()
var value []byte
for end := i + batch; i < end; i++ {
keys[i] = mvccEncode(nil, encodeUint32Ascending([]byte("key-"), uint32(i)), uint64(i+1), 0)
value = scanConfig.values.Bytes(rng, value)
if err := b.Set(keys[i], value, nil); err != nil {
log.Fatal(err)
}
}
if err := b.Commit(opts); err != nil {
log.Fatal(err)
}
}
if err := d.Flush(); err != nil {
log.Fatal(err)
}
limiter := maxOpsPerSec.newRateLimiter()
wg.Add(concurrency)
for i := 0; i < concurrency; i++ {
go func(i int) {
defer wg.Done()
rng := rand.New(rand.NewSource(uint64(i)))
startKeyBuf := append(make([]byte, 0, 64), []byte("key-")...)
endKeyBuf := append(make([]byte, 0, 64), []byte("key-")...)
minTS := encodeUint64Ascending(nil, math.MaxUint64)
for {
wait(limiter)
rows := int(rowDist.Uint64(rng))
startIdx := rng.Int31n(int32(len(keys) - rows))
startKey := encodeUint32Ascending(startKeyBuf[:4], uint32(startIdx))
endKey := encodeUint32Ascending(endKeyBuf[:4], uint32(startIdx+int32(rows)))
var count int
var nbytes int64
if scanConfig.reverse {
count, nbytes = mvccReverseScan(d, startKey, endKey, minTS)
} else {
count, nbytes = mvccForwardScan(d, startKey, endKey, minTS)
}
if count != rows {
log.Fatalf("scanned %d, expected %d\n", count, rows)
}
atomic.AddInt64(&bytes, nbytes)
atomic.AddInt64(&scanned, int64(count))
}
}(i)
}
},
tick: func(elapsed time.Duration, i int) {
if i%20 == 0 {
fmt.Println("_elapsed_______rows/sec_______MB/sec_______ns/row")
}
curBytes := atomic.LoadInt64(&bytes)
curScanned := atomic.LoadInt64(&scanned)
dur := elapsed - lastElapsed
fmt.Printf("%8s %14.1f %12.1f %12.1f\n",
time.Duration(elapsed.Seconds()+0.5)*time.Second,
float64(curScanned-lastScanned)/dur.Seconds(),
float64(curBytes-lastBytes)/(dur.Seconds()*(1<<20)),
float64(dur)/float64(curScanned-lastScanned),
)
lastBytes = curBytes
lastScanned = curScanned
lastElapsed = elapsed
},
done: func(elapsed time.Duration) {
curBytes := atomic.LoadInt64(&bytes)
curScanned := atomic.LoadInt64(&scanned)
fmt.Println("\n_elapsed___ops/sec(cum)__MB/sec(cum)__ns/row(avg)")
fmt.Printf("%7.1fs %14.1f %12.1f %12.1f\n\n",
elapsed.Seconds(),
float64(curScanned)/elapsed.Seconds(),
float64(curBytes)/(elapsed.Seconds()*(1<<20)),
float64(elapsed)/float64(curScanned),
)
},
})
}