-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.go
More file actions
203 lines (179 loc) · 4.42 KB
/
benchmark.go
File metadata and controls
203 lines (179 loc) · 4.42 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
package main
import (
"errors"
"fmt"
"math/rand"
"os"
"path/filepath"
"runtime"
"strings"
"sync/atomic"
"time"
"github.com/msrevive/kv-bench/kv"
"golang.org/x/sync/errgroup"
)
func dirSize(path string) (int64, error) {
var size int64
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if !info.IsDir() {
size += info.Size()
}
return err
})
return size, err
}
func randKey(minL int, maxL int) string {
n := rand.Intn(maxL-minL+1) + minL
buf := make([]byte, n)
for i := 0; i < n; i++ {
buf[i] = byte(rand.Intn(95) + 32)
}
return string(buf)
}
func randValue(rnd *rand.Rand, src []byte, minS int, maxS int) []byte {
n := rnd.Intn(maxS-minS+1) + minS
return src[:n]
}
func forceGC() {
runtime.GC()
time.Sleep(time.Millisecond * 500)
}
func shuffle(a [][]byte) {
for i := len(a) - 1; i > 0; i-- {
j := rand.Intn(i + 1)
a[i], a[j] = a[j], a[i]
}
}
func generateKeys(count int, minL int, maxL int) [][]byte {
keys := make([][]byte, 0, count)
seen := make(map[string]struct{}, count)
for len(keys) < count {
k := randKey(minL, maxL)
if _, ok := seen[k]; ok {
continue
}
seen[k] = struct{}{}
keys = append(keys, []byte(k))
}
return keys
}
func concurrentBatch(keys [][]byte, concurrency int, cb func(gid int, batch [][]byte) error) error {
eg := &errgroup.Group{}
batchSize := len(keys) / concurrency
for i := 0; i < concurrency; i++ {
batchStart := i * batchSize
batchEnd := (i + 1) * batchSize
if batchEnd > len(keys) {
batchEnd = len(keys)
}
gid := i
batch := keys[batchStart:batchEnd]
eg.Go(func() error {
return cb(gid, batch)
})
}
return eg.Wait()
}
func showProgress(cur int, total int) {
const (
width float32 = 40
freq = 10000
)
complete := int(float32(cur) / float32(total) * width)
if cur%freq != 0 {
return
}
fmt.Printf("\r[%-40s] %d/%d", strings.Repeat("-", complete), cur, total)
}
func clearLine() {
fmt.Print("\r\033[K")
}
func benchmarkPut(opts options, db kv.Store, keys [][]byte) error {
valSrc := make([]byte, opts.maxValueSize)
if _, err := rand.Read(valSrc); err != nil {
return err
}
var keysProcessed int64
err := concurrentBatch(keys, opts.concurrency, func(gid int, batch [][]byte) error {
rnd := rand.New(rand.NewSource(int64(rand.Uint64())))
for _, k := range batch {
if err := db.Put(k, randValue(rnd, valSrc, opts.minValueSize, opts.maxValueSize)); err != nil {
return err
}
showProgress(int(atomic.AddInt64(&keysProcessed, 1)), opts.numKeys)
}
return nil
})
if err != nil {
return err
}
showProgress(int(keysProcessed), opts.numKeys)
clearLine()
return nil
}
func benchmarkGet(opts options, db kv.Store, keys [][]byte) error {
var keysProcessed int64
err := concurrentBatch(keys, opts.concurrency, func(gid int, batch [][]byte) error {
for _, k := range batch {
v, err := db.Get(k)
if err != nil {
return err
}
if v == nil {
return errors.New("key doesn't exist")
}
showProgress(int(atomic.AddInt64(&keysProcessed, 1)), opts.numKeys)
}
return nil
})
if err != nil {
return err
}
showProgress(int(keysProcessed), opts.numKeys)
clearLine()
return nil
}
func benchmark(opts options) error {
db, err := kv.NewStore(opts.engine, opts.path)
if err != nil {
return err
}
fmt.Printf("arch: %s - %s\n", runtime.GOOS, runtime.GOARCH)
fmt.Printf("engine: %s\n", opts.engine)
fmt.Printf("keys: %d\n", opts.numKeys)
fmt.Printf("key size: %d-%d\n", opts.minKeySize, opts.maxKeySize)
fmt.Printf("value size %d-%d\n", opts.minValueSize, opts.maxValueSize)
fmt.Printf("concurrency: %d\n\n", opts.concurrency)
keys := generateKeys(opts.numKeys, opts.minKeySize, opts.maxKeySize)
clearLine()
var totalElapsed float64
// Put.
forceGC()
start := time.Now()
if err := benchmarkPut(opts, db, keys); err != nil {
return err
}
elapsed := time.Since(start).Seconds()
totalElapsed += elapsed
fmt.Printf("put: %.3fs\t%d ops/s\n", elapsed, int(float64(opts.numKeys)/elapsed))
// Get.
forceGC()
start = time.Now()
if err := benchmarkGet(opts, db, keys); err != nil {
return err
}
elapsed = time.Since(start).Seconds()
totalElapsed += elapsed
fmt.Printf("get: %.3fs\t%d ops/s\n", elapsed, int(float64(opts.numKeys)/elapsed))
// Total stats.
fmt.Printf("\nput + get: %.3fs\n", totalElapsed)
if err := db.Close(); err != nil {
return err
}
sz, err := dirSize(opts.path)
if err != nil {
return err
}
fmt.Printf("file size: %s\n", byteSize(sz))
return nil
}