-
Notifications
You must be signed in to change notification settings - Fork 671
/
benchmark_database.go
265 lines (231 loc) · 7.47 KB
/
benchmark_database.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package database
import (
"fmt"
"math/rand"
"testing"
"github.com/ava-labs/avalanchego/utils/units"
)
var (
// Benchmarks is a list of all database benchmarks
Benchmarks = []func(b *testing.B, db Database, name string, keys, values [][]byte){
BenchmarkGet,
BenchmarkPut,
BenchmarkDelete,
BenchmarkBatchPut,
BenchmarkBatchDelete,
BenchmarkBatchWrite,
BenchmarkParallelGet,
BenchmarkParallelPut,
BenchmarkParallelDelete,
}
// BenchmarkSizes to use with each benchmark
BenchmarkSizes = [][]int{
// count, keySize, valueSize
{1024, 32, 32},
{1024, 256, 256},
{1024, 2 * units.KiB, 2 * units.KiB},
}
)
// Writes size data into the db in order to setup reads in subsequent tests.
func SetupBenchmark(b *testing.B, count int, keySize, valueSize int) ([][]byte, [][]byte) {
b.Helper()
keys := make([][]byte, count)
values := make([][]byte, count)
for i := 0; i < count; i++ {
keyBytes := make([]byte, keySize)
valueBytes := make([]byte, valueSize)
_, err := rand.Read(keyBytes) // #nosec G404
if err != nil {
b.Fatal(err)
}
_, err = rand.Read(valueBytes) // #nosec G404
if err != nil {
b.Fatal(err)
}
keys[i], values[i] = keyBytes, valueBytes
}
return keys, values
}
// BenchmarkGet measures the time it takes to get an operation from a database.
func BenchmarkGet(b *testing.B, db Database, name string, keys, values [][]byte) {
count := len(keys)
if count == 0 {
b.Fatal("no keys")
}
b.Run(fmt.Sprintf("%s_%d_pairs_%d_keys_%d_values_db.get", name, count, len(keys[0]), len(values[0])), func(b *testing.B) {
for i, key := range keys {
value := values[i]
if err := db.Put(key, value); err != nil {
b.Fatalf("Unexpected error in Put %s", err)
}
}
b.ResetTimer()
// Reads b.N values from the db
for i := 0; i < b.N; i++ {
if _, err := db.Get(keys[i%count]); err != nil {
b.Fatalf("Unexpected error in Get %s", err)
}
}
})
}
// BenchmarkPut measures the time it takes to write an operation to a database.
func BenchmarkPut(b *testing.B, db Database, name string, keys, values [][]byte) {
count := len(keys)
if count == 0 {
b.Fatal("no keys")
}
b.Run(fmt.Sprintf("%s_%d_pairs_%d_keys_%d_values_db.put", name, count, len(keys[0]), len(values[0])), func(b *testing.B) {
// Writes b.N values to the db
for i := 0; i < b.N; i++ {
if err := db.Put(keys[i%count], values[i%count]); err != nil {
b.Fatalf("Unexpected error in Put %s", err)
}
}
})
}
// BenchmarkDelete measures the time it takes to delete a (k, v) from a database.
//nolint:interfacer // This function takes in a database to be the expected type.
func BenchmarkDelete(b *testing.B, db Database, name string, keys, values [][]byte) {
count := len(keys)
if count == 0 {
b.Fatal("no keys")
}
b.Run(fmt.Sprintf("%s_%d_pairs_%d_keys_%d_values_db.delete", name, count, len(keys[0]), len(values[0])), func(b *testing.B) {
// Writes random values of size _size_ to the database
for i, key := range keys {
value := values[i]
if err := db.Put(key, value); err != nil {
b.Fatalf("Unexpected error in Put %s", err)
}
}
b.ResetTimer()
// Deletes b.N values from the db
for i := 0; i < b.N; i++ {
if err := db.Delete(keys[i%count]); err != nil {
b.Fatalf("Unexpected error in Delete %s", err)
}
}
})
}
// BenchmarkBatchPut measures the time it takes to batch put.
//nolint:interfacer // This function takes in a database to be the expected type.
func BenchmarkBatchPut(b *testing.B, db Database, name string, keys, values [][]byte) {
count := len(keys)
if count == 0 {
b.Fatal("no keys")
}
b.Run(fmt.Sprintf("%s_%d_pairs_%d_keys_%d_values_batch.put", name, count, len(keys[0]), len(values[0])), func(b *testing.B) {
batch := db.NewBatch()
for i := 0; i < b.N; i++ {
if err := batch.Put(keys[i%count], values[i%count]); err != nil {
b.Fatalf("Unexpected error in batch.Put: %s", err)
}
}
})
}
// BenchmarkBatchDelete measures the time it takes to batch delete.
//nolint:interfacer // This function takes in a database to be the expected type.
func BenchmarkBatchDelete(b *testing.B, db Database, name string, keys, values [][]byte) {
count := len(keys)
if count == 0 {
b.Fatal("no keys")
}
b.Run(fmt.Sprintf("%s_%d_pairs_%d_keys_%d_values_batch.delete", name, count, len(keys[0]), len(values[0])), func(b *testing.B) {
batch := db.NewBatch()
for i := 0; i < b.N; i++ {
if err := batch.Delete(keys[i%count]); err != nil {
b.Fatalf("Unexpected error in batch.Delete: %s", err)
}
}
})
}
// BenchmarkBatchWrite measures the time it takes to batch write.
//nolint:interfacer // This function takes in a database to be the expected type.
func BenchmarkBatchWrite(b *testing.B, db Database, name string, keys, values [][]byte) {
count := len(keys)
if count == 0 {
b.Fatal("no keys")
}
b.Run(fmt.Sprintf("%s_%d_pairs_%d_keys_%d_values_batch.write", name, count, len(keys[0]), len(values[0])), func(b *testing.B) {
batch := db.NewBatch()
for i, key := range keys {
value := values[i]
if err := batch.Put(key, value); err != nil {
b.Fatalf("Unexpected error in batch.Put: %s", err)
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := batch.Write(); err != nil {
b.Fatalf("Unexpected error in batch.Write: %s", err)
}
}
})
}
// BenchmarkParallelGet measures the time it takes to read in parallel.
func BenchmarkParallelGet(b *testing.B, db Database, name string, keys, values [][]byte) {
count := len(keys)
if count == 0 {
b.Fatal("no keys")
}
b.Run(fmt.Sprintf("%s_%d_pairs_%d_keys_%d_values_db.get_parallel", name, count, len(keys[0]), len(values[0])), func(b *testing.B) {
for i, key := range keys {
value := values[i]
if err := db.Put(key, value); err != nil {
b.Fatalf("Unexpected error in Put %s", err)
}
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for i := 0; pb.Next(); i++ {
if _, err := db.Get(keys[i%count]); err != nil {
b.Fatalf("Unexpected error in Get %s", err)
}
}
})
})
}
// BenchmarkParallelPut measures the time it takes to write to the db in parallel.
func BenchmarkParallelPut(b *testing.B, db Database, name string, keys, values [][]byte) {
count := len(keys)
if count == 0 {
b.Fatal("no keys")
}
b.Run(fmt.Sprintf("%s_%d_pairs_%d_keys_%d_values_db.put_parallel", name, count, len(keys[0]), len(values[0])), func(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
// Write N values to the db
for i := 0; pb.Next(); i++ {
if err := db.Put(keys[i%count], values[i%count]); err != nil {
b.Fatalf("Unexpected error in Put %s", err)
}
}
})
})
}
// BenchmarkParallelDelete measures the time it takes to delete a (k, v) from the db.
//nolint:interfacer // This function takes in a database to be the expected type.
func BenchmarkParallelDelete(b *testing.B, db Database, name string, keys, values [][]byte) {
count := len(keys)
if count == 0 {
b.Fatal("no keys")
}
b.Run(fmt.Sprintf("%s_%d_pairs_%d_keys_%d_values_db.delete_parallel", name, count, len(keys[0]), len(values[0])), func(b *testing.B) {
for i, key := range keys {
value := values[i]
if err := db.Put(key, value); err != nil {
b.Fatalf("Unexpected error in Put %s", err)
}
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
// Deletes b.N values from the db
for i := 0; pb.Next(); i++ {
if err := db.Delete(keys[i%count]); err != nil {
b.Fatalf("Unexpected error in Delete %s", err)
}
}
})
})
}