forked from paquesid/badgerhold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_test.go
227 lines (188 loc) · 4.78 KB
/
bench_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
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
// Copyright 2019 Tim Shannon. All rights reserved.
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
package badgerhold_test
import (
"encoding/binary"
"encoding/json"
"io/ioutil"
"os"
"testing"
"github.com/dgraph-io/badger"
"github.com/timshannon/badgerhold"
)
type BenchData struct {
ID int
Category string
}
type BenchDataIndexed struct {
ID int
Category string `badgerholdIndex:"Category"`
}
var benchItem = BenchData{
ID: 30,
Category: "test category",
}
var benchItemIndexed = BenchData{
ID: 30,
Category: "test category",
}
// benchWrap creates a temporary database for testing and closes and cleans it up when
// completed.
func benchWrap(b *testing.B, options *badgerhold.Options, bench func(store *badgerhold.Store, b *testing.B)) {
tempDir, err := ioutil.TempDir("", "badgerhold_tests")
if err != nil {
b.Fatalf("Error opening %s: %s", tempDir, err)
}
defer os.RemoveAll(tempDir)
if options == nil {
options = &badgerhold.DefaultOptions
}
options.Dir = tempDir
options.ValueDir = tempDir
store, err := badgerhold.Open(*options)
if err != nil {
b.Fatalf("Error opening %s: %s", tempDir, err)
}
defer store.Close()
if store == nil {
b.Fatalf("store is null!")
}
bench(store, b)
}
var idVal uint64
func id() []byte {
idVal++
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, idVal)
return b
}
func BenchmarkRawInsert(b *testing.B) {
data, err := badgerhold.DefaultEncode(benchItem)
if err != nil {
b.Fatalf("Error encoding data for raw benchmarking: %s", err)
}
benchWrap(b, nil, func(store *badgerhold.Store, b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
err = store.Badger().Update(func(tx *badger.Txn) error {
return tx.Set(id(), data)
})
if err != nil {
b.Fatalf("Error inserting raw bytes: %s", err)
}
}
})
}
func BenchmarkNoIndexInsert(b *testing.B) {
benchWrap(b, nil, func(store *badgerhold.Store, b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := store.Insert(id(), benchItem)
if err != nil {
b.Fatalf("Error inserting into store: %s", err)
}
}
})
}
func BenchmarkIndexedInsert(b *testing.B) {
benchWrap(b, nil, func(store *badgerhold.Store, b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := store.Insert(id(), benchItemIndexed)
if err != nil {
b.Fatalf("Error inserting into store: %s", err)
}
}
})
}
func BenchmarkNoIndexUpsert(b *testing.B) {
benchWrap(b, nil, func(store *badgerhold.Store, b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := store.Upsert(id(), benchItem)
if err != nil {
b.Fatalf("Error inserting into store: %s", err)
}
}
})
}
func BenchmarkIndexedUpsert(b *testing.B) {
benchWrap(b, nil, func(store *badgerhold.Store, b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := store.Upsert(id(), benchItemIndexed)
if err != nil {
b.Fatalf("Error inserting into store: %s", err)
}
}
})
}
func BenchmarkNoIndexInsertJSON(b *testing.B) {
opt := badgerhold.DefaultOptions
opt.Encoder = json.Marshal
opt.Decoder = json.Unmarshal
benchWrap(b, &opt, func(store *badgerhold.Store, b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := store.Insert(id(), benchItem)
if err != nil {
b.Fatalf("Error inserting into store: %s", err)
}
}
})
}
func BenchmarkFindNoIndex(b *testing.B) {
benchWrap(b, nil, func(store *badgerhold.Store, b *testing.B) {
for i := 0; i < 3; i++ {
for k := 0; k < 100; k++ {
err := store.Insert(id(), benchItem)
if err != nil {
b.Fatalf("Error inserting benchmarking data: %s", err)
}
}
err := store.Insert(id(), &BenchData{
ID: 30,
Category: "findCategory",
})
if err != nil {
b.Fatalf("Error inserting benchmarking data: %s", err)
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
var result []BenchData
err := store.Find(&result, badgerhold.Where("Category").Eq("findCategory"))
if err != nil {
b.Fatalf("Error finding data in store: %s", err)
}
}
})
}
func BenchmarkFindIndexed(b *testing.B) {
benchWrap(b, nil, func(store *badgerhold.Store, b *testing.B) {
for i := 0; i < 3; i++ {
for k := 0; k < 100; k++ {
err := store.Insert(id(), benchItemIndexed)
if err != nil {
b.Fatalf("Error inserting benchmarking data: %s", err)
}
}
err := store.Insert(id(), &BenchDataIndexed{
ID: 30,
Category: "findCategory",
})
if err != nil {
b.Fatalf("Error inserting benchmarking data: %s", err)
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
var result []BenchDataIndexed
err := store.Find(&result, badgerhold.Where("Category").Eq("findCategory"))
if err != nil {
b.Fatalf("Error finding data in store: %s", err)
}
}
})
}