forked from dgraph-io/badger-bench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_test.go
360 lines (320 loc) · 7.97 KB
/
bench_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
package main
import (
"context"
"flag"
"fmt"
"math"
"math/rand"
"net/http"
"os"
"sync/atomic"
"testing"
"time"
"github.com/bmatsuo/lmdb-go/lmdb"
"github.com/dgraph-io/badger"
"github.com/dgraph-io/badger-bench/store"
"github.com/dgraph-io/badger/table"
"github.com/dgraph-io/badger/y"
)
var (
ctx = context.Background()
numKeys = flag.Float64("keys_mil", 10.0, "How many million keys to write.")
flagDir = flag.String("dir", "bench-tmp", "Where data is temporarily stored.")
flagValueSize = flag.Int("valsz", 128, "Size of each value.")
)
const Mi int = 1000000
const Mf float64 = 1000000
func getBadger() (*badger.KV, error) {
opt := badger.DefaultOptions
opt.MapTablesTo = table.LoadToRAM
opt.ValueGCRunInterval = 10 * time.Hour
opt.Dir = *flagDir + "/badger"
opt.ValueDir = opt.Dir
opt.DoNotCompact = true
opt.ValueGCThreshold = 0.0
return badger.NewKV(&opt)
}
func getRocks() *store.Store {
rdb, err := store.NewReadOnlyStore(*flagDir + "/rocks")
y.Check(err)
return rdb
}
func getLmdb() *lmdb.Env {
lmdbEnv, err := lmdb.NewEnv()
y.Check(err)
err = lmdbEnv.SetMaxReaders(math.MaxInt64)
y.Check(err)
err = lmdbEnv.SetMaxDBs(1)
y.Check(err)
err = lmdbEnv.SetMapSize(1 << 38) // ~273Gb
y.Check(err)
err = lmdbEnv.Open(*flagDir+"/lmdb", lmdb.Readonly, 0777)
y.Check(err)
return lmdbEnv
}
func newKey() []byte {
k := rand.Int() % int(*numKeys*Mf)
key := fmt.Sprintf("vsz=%05d-k=%010d", *flagValueSize, k) // 22 bytes.
return []byte(key)
}
func print(count int) {
if count%100000 == 0 {
fmt.Printf(".")
} else if count%Mi == 0 {
fmt.Printf("-")
}
}
type hitCounter struct {
found uint64
notFound uint64
errored uint64
}
func (h *hitCounter) Reset() {
h.found, h.notFound, h.errored = 0, 0, 0
}
func (h *hitCounter) Update(c *hitCounter) {
atomic.AddUint64(&h.found, c.found)
atomic.AddUint64(&h.notFound, c.notFound)
atomic.AddUint64(&h.errored, c.errored)
}
func (h *hitCounter) Print(storeName string, b *testing.B) {
b.Logf("%s: %d keys had valid values.", storeName, h.found)
b.Logf("%s: %d keys had no values", storeName, h.notFound)
b.Logf("%s: %d keys had errors", storeName, h.errored)
b.Logf("%s: %d total keys looked at", storeName, h.found+h.notFound+h.errored)
b.Logf("%s: hit rate : %.2f", storeName, float64(h.found)/float64(h.found+h.notFound+h.errored))
}
// A generic read benchmark that runs the doBench func for a specific key value store,
// aggregates the hit counts and prints them out.
func runRandomReadBenchmark(b *testing.B, storeName string, doBench func(*hitCounter, *testing.PB)) {
counter := &hitCounter{}
b.Run("read-random"+storeName, func(b *testing.B) {
counter.Reset()
b.RunParallel(func(pb *testing.PB) {
c := &hitCounter{}
doBench(c, pb)
counter.Update(c)
})
})
counter.Print(storeName, b)
}
func BenchmarkReadRandomBadger(b *testing.B) {
bdb, err := getBadger()
y.Check(err)
defer bdb.Close()
runRandomReadBenchmark(b, "badger", func(c *hitCounter, pb *testing.PB) {
var item badger.KVItem
for pb.Next() {
key := newKey()
err := bdb.Get(key, &item)
if err != nil {
c.errored++
} else if v := item.Value(); v != nil {
y.AssertTruef(len(v) == *flagValueSize, "Assertion failed. value size is %d, expected %d", len(v), *flagValueSize)
c.found++
} else {
c.notFound++
}
}
})
}
func BenchmarkReadRandomRocks(b *testing.B) {
rdb := getRocks()
defer rdb.Close()
runRandomReadBenchmark(b, "rocksdb", func(c *hitCounter, pb *testing.PB) {
for pb.Next() {
key := newKey()
// FIXME lookup API and increment accordingly
if _, err := rdb.Get(key); err == nil {
c.found++
}
}
})
}
func BenchmarkReadRandomLmdb(b *testing.B) {
lmdbEnv := getLmdb()
defer lmdbEnv.Close()
var lmdbDBI lmdb.DBI
// Acquire handle
err := lmdbEnv.View(func(txn *lmdb.Txn) error {
var err error
lmdbDBI, err = txn.OpenDBI("bench", 0)
return err
})
y.Check(err)
defer lmdbEnv.CloseDBI(lmdbDBI)
runRandomReadBenchmark(b, "lmdb", func(c *hitCounter, pb *testing.PB) {
for pb.Next() {
key := newKey()
err = lmdbEnv.View(func(txn *lmdb.Txn) error {
txn.RawRead = true
v, err := txn.Get(lmdbDBI, key)
if lmdb.IsNotFound(err) {
c.notFound++
return nil
} else if err != nil {
return err
}
y.AssertTruef(len(v) == *flagValueSize, "Assertion failed. value size is %d, expected %d", len(v), *flagValueSize)
c.found++
return nil
})
if err != nil {
c.errored++
}
}
})
}
func safecopy(dst []byte, src []byte) []byte {
if cap(dst) < len(src) {
dst = make([]byte, len(src))
}
dst = dst[0:len(src)]
copy(dst, src)
return dst
}
func BenchmarkIterateRocks(b *testing.B) {
rdb := getRocks()
defer rdb.Close()
k := make([]byte, 1024)
v := make([]byte, Mi)
b.ResetTimer()
b.Run("rocksdb-iterate", func(b *testing.B) {
for j := 0; j < b.N; j++ {
itr := rdb.NewIterator()
var count int
for itr.SeekToFirst(); itr.Valid(); itr.Next() {
{
// do some processing.
k = safecopy(k, itr.Key().Data())
v = safecopy(v, itr.Value().Data())
}
count++
print(count)
if count >= 2*Mi {
break
}
}
b.Logf("[%d] Counted %d keys\n", j, count)
}
})
}
func BenchmarkIterateLmdb(b *testing.B) {
lmdbEnv := getLmdb()
defer lmdbEnv.Close()
var lmdbDBI lmdb.DBI
// Acquire handle
err := lmdbEnv.View(func(txn *lmdb.Txn) error {
var err error
lmdbDBI, err = txn.OpenDBI("bench", 0)
return err
})
y.Check(err)
defer lmdbEnv.CloseDBI(lmdbDBI)
k := make([]byte, 1024)
v := make([]byte, Mi)
b.ResetTimer()
b.Run("lmdb-iterate", func(b *testing.B) {
for j := 0; j < b.N; j++ {
var count int
err = lmdbEnv.View(func(txn *lmdb.Txn) error {
txn.RawRead = true
cur, err := txn.OpenCursor(lmdbDBI)
if err != nil {
return err
}
defer cur.Close()
for {
k1, v1, err := cur.Get(nil, nil, lmdb.Next)
if lmdb.IsNotFound(err) {
return nil
}
if err != nil {
return err
}
//fmt.Printf("%s %s\n", k, v)
y.AssertTruef(len(v1) == *flagValueSize, "Assertion failed. value size is %d, expected %d", len(v1), *flagValueSize)
// do some processing.
k = safecopy(k, k1)
v = safecopy(v, v1)
count++
print(count)
if count >= 2*Mi {
break
}
}
return nil
})
y.Check(err)
b.Logf("[%d] Counted %d keys\n", j, count)
}
})
}
func BenchmarkIterateBadgerOnlyKeys(b *testing.B) {
bdb, err := getBadger()
y.Check(err)
defer bdb.Close()
k := make([]byte, 1024)
b.ResetTimer()
b.Run("badger-iterate-onlykeys", func(b *testing.B) {
for j := 0; j < b.N; j++ {
var count int
// 100 = size, 0 = num workers, false = fwd direction.
opt := badger.IteratorOptions{}
opt.PrefetchSize = 10000
itr := bdb.NewIterator(opt)
for itr.Rewind(); itr.Valid(); itr.Next() {
item := itr.Item()
{
// do some processing.
k = safecopy(k, item.Key())
}
count++
print(count)
if count >= 2*Mi {
break
}
}
b.Logf("[%d] Counted %d keys\n", j, count)
}
})
}
func BenchmarkIterateBadgerWithValues(b *testing.B) {
bdb, err := getBadger()
y.Check(err)
defer bdb.Close()
k := make([]byte, 1024)
v := make([]byte, Mi)
b.ResetTimer()
b.Run("badger-iterate-withvals", func(b *testing.B) {
for j := 0; j < b.N; j++ {
var count int
opt := badger.IteratorOptions{}
opt.PrefetchSize = 10000
opt.FetchValues = true
itr := bdb.NewIterator(opt)
for itr.Rewind(); itr.Valid(); itr.Next() {
item := itr.Item()
vsz := len(item.Value())
y.AssertTruef(vsz == *flagValueSize, "Assertion failed. value size is %d, expected %d", vsz, *flagValueSize)
{
// do some processing.
k = safecopy(k, item.Key())
v = safecopy(v, item.Value())
}
count++
print(count)
if count >= 2*Mi {
break
}
}
b.Logf("[%d] Counted %d keys\n", j, count)
}
})
}
func TestMain(m *testing.M) {
flag.Parse()
// call flag.Parse() here if TestMain uses flags
go http.ListenAndServe(":8080", nil)
os.Exit(m.Run())
}