-
Notifications
You must be signed in to change notification settings - Fork 102
/
speed_test.go
151 lines (122 loc) · 2.68 KB
/
speed_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
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
package diskv
import (
"fmt"
"math/rand"
"testing"
)
func shuffle(keys []string) {
ints := rand.Perm(len(keys))
for i := range keys {
keys[i], keys[ints[i]] = keys[ints[i]], keys[i]
}
}
func genValue(size int) []byte {
v := make([]byte, size)
for i := 0; i < size; i++ {
v[i] = uint8((rand.Int() % 26) + 97) // a-z
}
return v
}
const (
keyCount = 1000
)
func genKeys() []string {
keys := make([]string, keyCount)
for i := 0; i < keyCount; i++ {
keys[i] = fmt.Sprintf("%d", i)
}
return keys
}
func (d *Diskv) load(keys []string, val []byte) {
for _, key := range keys {
d.Write(key, val)
}
}
func benchRead(b *testing.B, size, cachesz int) {
b.StopTimer()
d := New(Options{
BasePath: "speed-test",
CacheSizeMax: uint64(cachesz),
})
defer d.EraseAll()
keys := genKeys()
value := genValue(size)
d.load(keys, value)
shuffle(keys)
b.SetBytes(int64(size))
b.StartTimer()
for i := 0; i < b.N; i++ {
_, _ = d.Read(keys[i%len(keys)])
}
b.StopTimer()
}
func benchWrite(b *testing.B, size int, withIndex bool) {
b.StopTimer()
options := Options{
BasePath: "speed-test",
CacheSizeMax: 0,
}
if withIndex {
options.Index = &BTreeIndex{}
options.IndexLess = strLess
}
d := New(options)
defer d.EraseAll()
keys := genKeys()
value := genValue(size)
shuffle(keys)
b.SetBytes(int64(size))
b.StartTimer()
for i := 0; i < b.N; i++ {
d.Write(keys[i%len(keys)], value)
}
b.StopTimer()
}
func BenchmarkWrite__32B_NoIndex(b *testing.B) {
benchWrite(b, 32, false)
}
func BenchmarkWrite__1KB_NoIndex(b *testing.B) {
benchWrite(b, 1024, false)
}
func BenchmarkWrite__4KB_NoIndex(b *testing.B) {
benchWrite(b, 4096, false)
}
func BenchmarkWrite_10KB_NoIndex(b *testing.B) {
benchWrite(b, 10240, false)
}
func BenchmarkWrite__32B_WithIndex(b *testing.B) {
benchWrite(b, 32, true)
}
func BenchmarkWrite__1KB_WithIndex(b *testing.B) {
benchWrite(b, 1024, true)
}
func BenchmarkWrite__4KB_WithIndex(b *testing.B) {
benchWrite(b, 4096, true)
}
func BenchmarkWrite_10KB_WithIndex(b *testing.B) {
benchWrite(b, 10240, true)
}
func BenchmarkRead__32B_NoCache(b *testing.B) {
benchRead(b, 32, 0)
}
func BenchmarkRead__1KB_NoCache(b *testing.B) {
benchRead(b, 1024, 0)
}
func BenchmarkRead__4KB_NoCache(b *testing.B) {
benchRead(b, 4096, 0)
}
func BenchmarkRead_10KB_NoCache(b *testing.B) {
benchRead(b, 10240, 0)
}
func BenchmarkRead__32B_WithCache(b *testing.B) {
benchRead(b, 32, keyCount*32*2)
}
func BenchmarkRead__1KB_WithCache(b *testing.B) {
benchRead(b, 1024, keyCount*1024*2)
}
func BenchmarkRead__4KB_WithCache(b *testing.B) {
benchRead(b, 4096, keyCount*4096*2)
}
func BenchmarkRead_10KB_WithCache(b *testing.B) {
benchRead(b, 10240, keyCount*4096*2)
}