-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.go
58 lines (48 loc) · 1.2 KB
/
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
package main
import (
"github.com/ababo/mastore/store"
logpkg "log"
"math/rand"
"time"
)
const testKeys = 10000000
const logValuesCount = 10000000000
func randomString(strlen int) string {
const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
result := make([]byte, strlen)
for i := 0; i < strlen; i++ {
result[i] = chars[rand.Intn(len(chars))]
}
return string(result)
}
func normIndex(size int) int {
i := size/2 + int(rand.NormFloat64()*float64(size/4))
if i < 0 {
i = 0
} else if i >= size {
i = size - 1
}
return i
}
func doTest(log *logpkg.Logger, st *store.Store, keys int, values int) bool {
rand.Seed(time.Now().UTC().UnixNano())
log.Printf("started to generate %d random keys", keys)
var keys2 []string
for i := 0; i < keys; i++ {
keys2 = append(keys2, randomString(rand.Int()%32+1))
}
log.Printf("started to insert %d values", values)
for i, size := 0, 0; i < values; i++ {
checkInterrupted(st)
key := keys2[normIndex(keys)]
val := randomString(rand.Int()%64 + 1)
if !st.AddValue(key, val) {
return false
}
size += len(key) + len(val) + 2
if i != 0 && i%logValuesCount == 0 {
log.Printf("added %d values (%d bytes)", i, size)
}
}
return st.Flush(true)
}