-
Notifications
You must be signed in to change notification settings - Fork 2
/
mod_bolt.go
354 lines (314 loc) · 8.37 KB
/
mod_bolt.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
package main
import "os"
import "fmt"
import "sync"
import "time"
import "sync/atomic"
import "math/rand"
import "path/filepath"
import "github.com/bnclabs/golog"
import "github.com/coreos/bbolt"
import humanize "github.com/dustin/go-humanize"
func perfbolt() error {
path := boltpath()
defer func() {
if err := os.RemoveAll(path); err != nil {
panic(err)
}
}()
fmt.Printf("BOLT path %q\n", path)
db, err := initbolt(path)
if err != nil {
panic(err)
}
defer db.Close()
seedl, seedc := int64(options.seed), int64(options.seed)+100
klen, vlen := int64(options.keylen), int64(options.vallen)+100
loadn := int64(options.load)
fmt.Printf("Seed for load: %v, for ops: %v\n", seedl, seedc)
if err = boltload(db, klen, vlen, loadn, seedl); err != nil {
return err
}
var wg sync.WaitGroup
n := atomic.LoadInt64(&numentries)
fin := make(chan struct{})
if options.inserts+options.upserts+options.deletes > 0 {
// writer routine
go boltWriter(db, klen, vlen, n, seedl, seedc, fin, &wg)
wg.Add(1)
}
if options.gets > 0 {
for i := 0; i < options.cpu; i++ {
go boltGetter(db, klen, vlen, n, seedl, seedc, fin, &wg)
wg.Add(1)
}
}
if options.ranges > 0 {
for i := 0; i < options.cpu; i++ {
go boltRanger(db, klen, vlen, n, seedl, seedc, fin, &wg)
wg.Add(1)
}
}
wg.Wait()
close(fin)
dirsize, err := DirSize(path)
if err != nil {
panic(err)
}
fmsg := "BOLT total indexed %v items, footprint %v\n"
fmt.Printf(fmsg, getboltCount(db), humanize.Bytes(uint64(dirsize)))
return nil
}
func initbolt(path string) (db *bolt.DB, err error) {
// Open the database.
db, err = bolt.Open(path, 0666, nil)
if err != nil {
fmt.Printf("bolt.Open(): %v\n", err)
return nil, err
}
if err := db.Update(func(tx *bolt.Tx) error {
// Create a bucket.
_, err := tx.CreateBucket([]byte(bucketname))
if err != nil {
return err
}
return nil
}); err != nil {
fmt.Printf("bolt.CreateBucket(%q): %v\n", bucketname, err)
return nil, err
}
return db, nil
}
func boltload(db *bolt.DB, klen, vlen, loadn, seedl int64) error {
var key, value []byte
markercount, count := int64(1000000), int64(0)
g := Generateloadr(klen, vlen, int64(options.load), int64(seedl))
populate := func(tx *bolt.Tx) (err error) {
b := tx.Bucket([]byte(bucketname))
for i := 0; i < 1000000 && key != nil; i++ {
if err = b.Put(key, value); err != nil {
return err
}
count++
key, value = g(key, value)
}
return nil
}
now, epoch := time.Now(), time.Now()
key, value = g(key, value)
for key != nil {
if err := db.Update(populate); err != nil {
log.Errorf("key %q err : %v", key, err)
return err
}
if count%markercount == 0 {
x := time.Since(now).Round(time.Second)
y := time.Since(epoch).Round(time.Second)
fmsg := "boltload {%v items in %v} {%v items in %v}\n"
fmt.Printf(fmsg, markercount, x, count, y)
now = time.Now()
}
}
atomic.AddInt64(&numentries, int64(options.load))
atomic.AddInt64(&totalwrites, int64(options.load))
count = int64(getboltCount(db))
took := time.Since(epoch).Round(time.Second)
fmt.Printf("Loaded %v items in %v\n", count, took)
return nil
}
func boltWriter(
db *bolt.DB, klen, vlen, loadn, seedl, seedc int64,
fin chan struct{}, wg *sync.WaitGroup) {
var key, value []byte
var x, y, z int64
gcreate := Generatecreate(klen, vlen, loadn, int64(options.inserts), seedc)
gupdate := Generateupdate(
klen, vlen, loadn, int64(options.inserts), seedl, seedc, -1,
)
gdelete := Generatedelete(
klen, vlen, loadn, int64(options.inserts), seedl, seedc, delmod,
)
bname := []byte(bucketname)
put := func(tx *bolt.Tx) (err error) {
b := tx.Bucket(bname)
if err := b.Put(key, value); err != nil {
return err
}
return nil
}
update := func(tx *bolt.Tx) (err error) {
b := tx.Bucket(bname)
if err := b.Put(key, value); err != nil {
return err
}
return nil
}
delete := func(tx *bolt.Tx) (err error) {
b := tx.Bucket(bname)
if err := b.Delete(key); err != nil {
return err
}
return nil
}
rnd := rand.New(rand.NewSource(seedl))
epoch, now, markercount := time.Now(), time.Now(), int64(1000000)
insn, upsn, deln := options.inserts, options.upserts, options.deletes
count := int64(0)
for totalops := insn + upsn + deln; totalops > 0; {
idx := rnd.Intn(totalops)
switch {
case idx < insn:
key, value = gcreate(key, value)
if err := db.Update(put); err != nil {
log.Errorf("key %q err : %v", key, err)
return
}
atomic.AddInt64(&numentries, 1)
x = atomic.AddInt64(&ninserts, 1)
insn--
case idx < (insn + upsn):
key, value = gupdate(key, value)
if err := db.Update(update); err != nil {
log.Errorf("key %q err : %v", key, err)
return
}
y = atomic.AddInt64(&nupserts, 1)
upsn--
case idx < (insn + upsn + deln):
key, value = gdelete(key, value)
if err := db.Update(delete); err != nil {
atomic.AddInt64(&xdeletes, 1)
} else {
atomic.AddInt64(&ndeletes, 1)
}
z = atomic.LoadInt64(&ndeletes) + atomic.LoadInt64(&xdeletes)
atomic.AddInt64(&numentries, -1)
deln--
default:
fmsg := "insn: %v, upsn: %v, deln: %v idx: %v"
panic(fmt.Errorf(fmsg, insn, upsn, deln, idx))
}
totalops = insn + upsn + deln
if count > 0 && count%markercount == 0 {
a := time.Since(now).Round(time.Second)
b := time.Since(epoch).Round(time.Second)
fmsg := "boltWriter {ins:%v,ups:%v,dels:%v in %v}, {%v ops %v}\n"
fmt.Printf(fmsg, x, y, z, b, markercount, a)
now = time.Now()
}
count++
}
n := atomic.AddInt64(&totalwrites, int64(x+y+z))
took := time.Since(epoch).Round(time.Second)
wg.Done()
<-fin
fmsg := "at exit boltWriter {%v,%v,%v (%v) in %v}\n"
fmt.Printf(fmsg, x, y, z, n, took)
}
func boltGetter(
db *bolt.DB, klen, vlen, loadn, seedl, seedc int64,
fin chan struct{}, wg *sync.WaitGroup) {
var ngets, nmisses int64
epoch := time.Now()
defer func() {
took := time.Since(epoch).Round(time.Second)
wg.Done()
<-fin
fmsg := "at exit, boltGetter %v:%v items in %v\n"
fmt.Printf(fmsg, ngets, nmisses, took)
}()
time.Sleep(time.Duration(rand.Intn(100)+300) * time.Millisecond)
var key []byte
g := Generateread(
int64(options.keylen), loadn, int64(options.inserts), seedl, seedc,
)
bname := []byte(bucketname)
get := func(tx *bolt.Tx) (err error) {
b := tx.Bucket(bname)
v := b.Get(key)
if v == nil {
nmisses++
} else {
ngets++
}
return nil
}
now, markercount := time.Now(), int64(10000000)
for ngets+nmisses < int64(options.gets) {
key = g(key, atomic.LoadInt64(&ninserts))
if err := db.View(get); err != nil {
panic(err)
}
if (ngets+nmisses)%markercount == 0 {
x := time.Since(now).Round(time.Second)
y := time.Since(epoch).Round(time.Second)
fmsg := "boltGetter {%v items in %v} {%v:%v items in %v}\n"
fmt.Printf(fmsg, markercount, x, ngets, nmisses, y)
now = time.Now()
}
}
}
func boltRanger(
db *bolt.DB, klen, vlen, loadn, seedl, seedc int64,
fin chan struct{}, wg *sync.WaitGroup) {
var nranges, nmisses int64
epoch := time.Now()
defer func() {
took := time.Since(epoch).Round(time.Second)
wg.Done()
<-fin
fmsg := "at exit, boltRanger %v:%v items in %v\n"
fmt.Printf(fmsg, nranges, nmisses, took)
}()
time.Sleep(time.Duration(rand.Intn(100)+300) * time.Millisecond)
var key []byte
g := Generateread(
int64(options.keylen), loadn, int64(options.inserts), seedl, seedc,
)
bname := []byte(bucketname)
ranger := func(tx *bolt.Tx) error {
b := tx.Bucket(bname)
cur := b.Cursor()
_, v := cur.Seek(key)
for i := 0; i < options.limit; i++ {
nranges++
if v == nil {
nmisses++
}
_, v = cur.Next()
}
return nil
}
now, markercount := time.Now(), int64(100000000)
for nranges+nmisses < int64(options.ranges) {
key = g(key, atomic.LoadInt64(&ninserts))
if err := db.View(ranger); err != nil {
panic(err)
}
if (nranges+nmisses)%markercount == 0 {
x := time.Since(now).Round(time.Second)
y := time.Since(epoch).Round(time.Second)
fmsg := "boltRanger {%v items in %v} {%v:%v items in %v}\n"
fmt.Printf(fmsg, markercount, x, nranges, nmisses, y)
now = time.Now()
}
}
}
func boltpath() string {
path := filepath.Join(os.TempDir(), "bolt.data")
if err := os.RemoveAll(path); err != nil {
panic(err)
}
return path
}
func getboltCount(db *bolt.DB) (count uint64) {
err := db.Update(func(tx *bolt.Tx) (err error) {
b := tx.Bucket([]byte(bucketname))
count = uint64(b.Stats().KeyN)
return nil
})
if err != nil {
panic(err)
}
return
}