-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
180 lines (176 loc) · 4.51 KB
/
main.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
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"log"
"math/rand"
"os"
"strconv"
"time"
"github.com/boltdb/bolt"
)
func main() {
concurrency, _ := strconv.Atoi(os.Args[1])
tasks, _ := strconv.Atoi(os.Args[2])
disks, _ := strconv.Atoi(os.Args[3])
partitions, _ := strconv.Atoi(os.Args[4])
dbPaths := listDBPaths('b', disks, partitions)
dbs := openDBs(dbPaths)
wch := make(chan time.Duration, concurrency)
wsuccess := make(chan int, concurrency)
rch := make(chan time.Duration, concurrency)
rsuccess := make(chan int, concurrency)
srcs := prepareData(1000)
keys := populateData(1000, srcs, dbs)
di := rand.Intn(len(dbs))
db := dbs[di]
//var bucket *bolt.Bucket
//var err error
db.Update(func(tx *bolt.Tx) error {
//bucket, err = tx.CreateBucketIfNotExists([]byte("iqiyi"))
_, err := tx.CreateBucketIfNotExists([]byte("iqiyi"))
if err != nil {
return err
}
return nil
})
for t := 0; t < 3; t++ {
log.Printf("start to benchmark...")
begin := time.Now()
for i := 0; i < concurrency; i++ {
go func(id int) {
var rduration time.Duration
var wduration time.Duration
rsucc := 0
wsucc := 0
for j := 0; j < tasks; j++ {
if rand.Intn(100) <= 20 {
key := generateKey()
data := srcs[rand.Intn(len(srcs))].Data
start := time.Now()
if err := db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("iqiyi"))
err := b.Put([]byte(key), []byte(data))
return err
}); err == nil {
wduration += time.Since(start)
wsucc++
}
} else {
key := keys[di][rand.Intn(len(keys[di]))]
start := time.Now()
if err := db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("iqiyi"))
_ = b.Get([]byte(key))
return nil
}); err == nil {
rduration += time.Since(start)
rsucc++
}
}
}
rch <- rduration
rsuccess <- rsucc
wch <- wduration
wsuccess <- wsucc
}(i)
}
var relapsed, welapsed time.Duration
var rcount, wcount int64
for i := 0; i < concurrency; i++ {
relapsed += <-rch
welapsed += <-wch
rcount += int64(<-rsuccess)
wcount += int64(<-wsuccess)
}
d := time.Since(begin)
log.Printf("For read requests:")
log.Printf("it took %s", relapsed)
log.Printf("success requests: %d", rcount)
log.Printf("time cost per request: %.6fms", float64((relapsed / time.Millisecond))/float64(rcount))
log.Printf("qps: %.2f\n\n", float64(rcount*1000)/float64(d/time.Millisecond))
log.Printf("For write requests:")
log.Printf("it took %s", welapsed)
log.Printf("success requests: %d", wcount)
log.Printf("time cost per request: %.6fms", float64((welapsed / time.Millisecond))/float64(wcount))
log.Printf("qps: %.2f\n\n\n", float64(wcount*1000)/float64(d/time.Millisecond))
time.Sleep(time.Second * 10)
}
for _, d := range dbs {
d.Close()
}
}
func generateKey() string {
t := fmt.Sprintf("tenant%06d", rand.Intn(100))
c := fmt.Sprintf("container%04d", rand.Intn(10))
ts := time.Now()
o := strconv.FormatInt(ts.UnixNano(), 10)
return fmt.Sprintf("/%s/%s/%s", t, c, o)
}
type src struct {
Data []byte
Checksum string
}
func prepareData(n int) []src {
var srcs []src
for i := 0; i < n; i++ {
data := Bytes(256)
checksum := md5.Sum(data)
srcs = append(srcs, src{Data: data, Checksum: hex.EncodeToString(checksum[:])})
}
return srcs
}
func openDBs(paths []string) []*bolt.DB {
var dbs []*bolt.DB
for _, p := range paths {
db, err := bolt.Open(p, 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
log.Fatal(err)
}
dbs = append(dbs, db)
}
return dbs
}
func listDBPaths(begin rune, disks int, partitions int) []string {
var dbs []string
for i := 0; i < disks; i++ {
for j := 0; j < partitions; j++ {
db := fmt.Sprintf("/srv/node/sd%c/%d", begin, j)
dbs = append(dbs, db)
}
begin += 1
}
return dbs
}
func populateData(num int, srcs []src, dbs []*bolt.DB) [][]string {
keys := make([][]string, len(dbs))
for i, db := range dbs {
var ks []string
for j := 0; j < num; j++ {
data := srcs[rand.Intn(len(srcs))].Data
p := generateKey()
if err := db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte("iqiyi"))
if err != nil {
return err
}
return b.Put([]byte(p), []byte(data))
}); err != nil {
continue
}
ks = append(ks, p)
}
keys[i] = ks
}
return keys
}
func checksum(data []byte) string {
checksum := md5.Sum(data)
return hex.EncodeToString(checksum[:])
}
func Bytes(n int) []byte {
d := make([]byte, n)
rand.Read(d)
return d
}