-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbench_boltdb_test.go
73 lines (63 loc) · 1.27 KB
/
bench_boltdb_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
package contrast_benchmark
import (
"go.etcd.io/bbolt"
"os"
"testing"
)
var boltDB *bbolt.DB
func init() {
opts := bbolt.DefaultOptions
opts.NoSync = true
var err error
_ = os.MkdirAll("benchmark/boltdb", os.ModePerm)
boltDB, err = bbolt.Open("benchmark/boltdb/bolt.data", 0644, opts)
if err != nil {
panic(err)
}
boltDB.Update(func(tx *bbolt.Tx) error {
_, err := tx.CreateBucket([]byte("test-bucket"))
if err != nil {
panic(err)
}
return nil
})
initBoltDBData()
}
func initBoltDBData() {
var k int
for i := 0; i < 5; i++ {
boltDB.Update(func(tx *bbolt.Tx) error {
for j := 0; j < 100000; j++ {
err := tx.Bucket([]byte("test-bucket")).Put(GetKey(k), GetValue())
if err != nil {
panic(err)
}
k++
}
return nil
})
}
}
func Benchmark_PutValue_BoltDB(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
boltDB.Update(func(tx *bbolt.Tx) error {
err := tx.Bucket([]byte("test-bucket")).Put(GetKey(i), GetValue())
if err != nil {
panic(err)
}
return nil
})
}
}
func Benchmark_GetValue_BoltDB(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
boltDB.View(func(tx *bbolt.Tx) error {
tx.Bucket([]byte("test-bucket")).Get(GetKey(i))
return nil
})
}
}