/
random-db-put.go
94 lines (85 loc) · 2.08 KB
/
random-db-put.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
// rocksdb test
package main
import (
"fmt"
"math/rand"
"os"
"time"
"github.com/tecbot/gorocksdb"
)
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func randBytes(from int, to int) []byte {
n := from + rand.Intn(to-from)
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return (b)
}
type putStat struct {
maxTime time.Duration
avgTime time.Duration
}
func doPut(db *gorocksdb.DB, from int, to int) <-chan putStat {
var totalTime time.Duration
var maxTime time.Duration
putCount := 0
loopCount := 100000
putStatCh := make(chan putStat)
wo := gorocksdb.NewDefaultWriteOptions()
createDBEntry := func(key string) {
value := randBytes(from, to)
start := time.Now()
err := db.Put(wo, key, value)
if err != nil {
fmt.Println("dbPut error : ", err)
os.Exit(1)
}
delta := time.Since(start)
if delta > maxTime {
maxTime = delta
}
totalTime += delta
putCount++
if (putCount % loopCount) == 0 {
avgTime := totalTime / time.Duration(loopCount)
putStatCh <- putStat{maxTime, avgTime}
maxTime = 0
totalTime = 0
}
}
go func() {
for i, key := range filenames {
createDBEntry(key)
}
close(putStatCh)
}()
return putStatCh
}
func main() {
dbName := "/data/mydb"
cache := gorocksdb.NewLRUCache(512 * 1024 * 1024)
filter := gorocksdb.NewBloomFilter(15)
to := gorocksdb.NewDefaultBlockBasedTableOptions()
to.SetBlockSize(256 * 1024)
to.SetBlockCache(cache)
to.SetFilterPolicy(filter)
options := gorocksdb.NewDefaultOptions()
options.SetBlockBasedTableFactory(to)
options.SetCreateIfMissing(true)
options.SetStatsDumpPeriodSec(60 * 1) // dump stats at 10 minute interval
options.SetCompactionStyle(gorocksdb.UniversalCompactionStyle)
options.SetWriteBufferSize(512 * 1024 * 1024)
options.SetMaxWriteBufferNumber(5)
options.SetMinWriteBufferNumberToMerge(2)
db, err := gorocksdb.OpenDb(options, dbName)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
putStatch := doPut(db, 16*1024, 32*1024)
for {
p := <-putStatch
fmt.Println("avgTime ", p.avgTime, "maxTime", p.maxTime)
}
}