-
Notifications
You must be signed in to change notification settings - Fork 153
/
03_long_running_write_and_read.go
181 lines (160 loc) · 3.97 KB
/
03_long_running_write_and_read.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
package main
import (
"fmt"
"log"
"math/rand"
"os"
"time"
"github.com/boltdb/bolt"
"github.com/renstrom/shortuuid"
)
var (
dbPath = shortuuid.New() + ".db"
bucketName = shortuuid.New()
// 2GB
// numKeys = 500000
numKeys = 10000
keyLen = 100
valLen = 750
keys = make([][]byte, numKeys)
vals = make([][]byte, numKeys)
keysForRead = make([][]byte, numKeys)
printIdx = 2000
// readStartIdx = 300000
readStartIdx = 300
readRepeat = 1000
timeout = time.Second
)
func init() {
fmt.Println("Starting generating random data...")
for i := range keys {
if i%printIdx == 0 {
fmt.Println("Generating", i+1, "/", numKeys)
}
keys[i] = randBytes(keyLen)
vals[i] = randBytes(valLen)
if i+1 == numKeys {
fmt.Println("Generating", i+1, "/", numKeys)
fmt.Println("Done with generating random data...")
}
}
fmt.Println("Copying 'keys' to 'keysForRead'...")
copy(keysForRead, keys)
fmt.Println("Done with copying...")
}
func main() {
fmt.Println("dbPath:", dbPath)
fmt.Println("bucketName:", bucketName)
defer os.Remove(dbPath)
db, err := bolt.Open(dbPath, 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
panic(err)
}
defer db.Close()
fmt.Println("Creating bucket:", bucketName)
if err := db.Update(func(tx *bolt.Tx) error {
if _, err := tx.CreateBucket([]byte(bucketName)); err != nil {
return err
}
return nil
}); err != nil {
panic(err)
}
fmt.Println("Done with creating bucket:", bucketName)
readyToRead := make(chan struct{})
doneWithWrite := make(chan struct{})
doneWithRead := make(chan struct{})
go func() {
fmt.Println("Starting long-running writing...")
for i := range keys {
if i == readStartIdx {
readyToRead <- struct{}{}
}
st := time.Now()
ch := make(chan struct{})
go func(i int) {
if err := db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucketName))
if err := b.Put(keys[i], vals[i]); err != nil {
return err
}
return nil
}); err != nil {
panic(err)
}
ch <- struct{}{}
}(i)
select {
case <-ch:
if i%printIdx == 0 {
fmt.Printf("Write took: %v (%d/%d)\n", time.Since(st), i+1, numKeys)
}
continue
case <-time.After(timeout):
log.Fatalf("Write timeout: %v (%d/%d)\n", time.Since(st), i+1, numKeys)
}
}
doneWithWrite <- struct{}{}
fmt.Println("Done with long-running writing...")
}()
go func() {
<-readyToRead
for j := 0; j < readRepeat; j++ {
fmt.Printf("Starting long-running reading...#%d\n", j)
for i := range keysForRead {
st := time.Now()
ch := make(chan struct{})
go func(i int) {
if err := db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucketName))
k := keys[i]
v := b.Get(k)
// fmt.Printf("Read: %s ---> %s\n", k, v)
_ = k
_ = v
return nil
}); err != nil {
panic(err)
}
ch <- struct{}{}
}(i)
select {
case <-ch:
if i%printIdx == 0 {
fmt.Printf("Read took: %v (%d/%d)\n", time.Since(st), i+1, numKeys)
}
continue
case <-time.After(timeout):
log.Fatalf("Read timeout: %v (%d/%d)\n", time.Since(st), i+1, numKeys)
}
}
}
doneWithRead <- struct{}{}
fmt.Println("Done with long-running reading...")
}()
<-doneWithWrite
<-doneWithRead
fmt.Println("Done!")
}
func randBytes(n int) []byte {
const (
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
src := rand.NewSource(time.Now().UnixNano())
b := make([]byte, n)
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return b
}