-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathbenchmark_test.go
553 lines (481 loc) · 13.9 KB
/
benchmark_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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// Copyright 2015 Alex Browne. All rights reserved.
// Use of this source code is governed by the MIT
// license, which can be found in the LICENSE file.
// File benchmark_test.go contains all benchmarks.
package zoom
import (
"math/rand"
"testing"
)
// BenchmarkConnection just gets a connection and then closes it
func BenchmarkConnection(b *testing.B) {
testingSetUp()
defer testingTearDown()
b.ResetTimer()
for i := 0; i < b.N; i++ {
conn := testPool.NewConn()
_ = conn.Close()
}
}
// BenchmarkPing sends the PING command
func BenchmarkPing(b *testing.B) {
benchmarkCommand(b, "PING")
}
// BenchmarkSet sends the SET command
func BenchmarkSet(b *testing.B) {
benchmarkCommand(b, "SET", "foo", "bar")
}
// BenchmarkGet sends the GET command after first sending SET
func BenchmarkGet(b *testing.B) {
conn := testPool.NewConn()
defer func() {
_ = conn.Close()
}()
_, err := conn.Do("SET", "foo", "bar")
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
benchmarkCommand(b, "GET", "foo")
}
// benchmarkCommand benchmarks a specific redis command.
func benchmarkCommand(b *testing.B, cmd string, args ...interface{}) {
testingSetUp()
defer testingTearDown()
for i := 0; i < b.N; i++ {
conn := testPool.NewConn()
if _, err := conn.Do(cmd, args...); err != nil {
_ = conn.Close()
b.Fatal(err)
}
_ = conn.Close()
}
}
// BenchmarkSave saves a single model
func BenchmarkSave(b *testing.B) {
testingSetUp()
defer testingTearDown()
models := createTestModels(1)
model := models[0]
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := testModels.Save(model); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkSave100 saves 100 models in a single transaction.
func BenchmarkSave100(b *testing.B) {
testingSetUp()
defer testingTearDown()
models := createTestModels(100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
t := testPool.NewTransaction()
for _, model := range models {
t.Save(testModels, model)
}
b.StartTimer()
if err := t.Exec(); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkFind finds one model at a time randomly from
// a set of 1,000 models
func BenchmarkFind(b *testing.B) {
testingSetUp()
defer testingTearDown()
models, err := createAndSaveTestModels(1000)
if err != nil {
b.Fatal(err)
}
ids := modelIDs(Models(models))
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
id := selectUnique(1, ids)[0]
b.StartTimer()
if err := testModels.Find(id, &testModel{}); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkFind100 finds 100 models at a time (in a single transaction)
// selected randomly from a set of 1,000 models
func BenchmarkFind100(b *testing.B) {
testingSetUp()
defer testingTearDown()
models, err := createAndSaveTestModels(1000)
if err != nil {
b.Fatal(err)
}
ids := modelIDs(Models(models))
b.ResetTimer()
// run the actual test
for i := 0; i < b.N; i++ {
b.StopTimer()
selectedIDs := selectUnique(100, ids)
t := testPool.NewTransaction()
for _, id := range selectedIDs {
t.Find(testModels, id, &testModel{})
}
b.StartTimer()
if err := t.Exec(); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkFindAll100 finds 100 models with the FindAll command
func BenchmarkFindAll100(b *testing.B) {
testingSetUp()
defer testingTearDown()
_, err := createAndSaveTestModels(100)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := testModels.FindAll(&[]*testModel{}); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkFindAll10000 finds 10,000 models with the FindAll command
func BenchmarkFindAll10000(b *testing.B) {
testingSetUp()
defer testingTearDown()
_, err := createAndSaveTestModels(10000)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := testModels.FindAll(&[]*testModel{}); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkDelete deletes a single model. It recreates the model on
// each iteration, but stops the timer during the Save method, so only
// Delete is timed.
func BenchmarkDelete(b *testing.B) {
testingSetUp()
defer testingTearDown()
for i := 0; i < b.N; i++ {
b.StopTimer()
// Stop the timer, save a model, then start the timer again
models, err := createAndSaveTestModels(1)
if err != nil {
b.Fatal(err)
}
b.StartTimer()
if _, err := testModels.Delete(models[0].ModelID()); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkDelete deletes 100 models in a single transaction. It
// recreates the models on each iteration, but stops the timer during
// the Save methods, so only Delete is timed.
func BenchmarkDelete100(b *testing.B) {
testingSetUp()
defer testingTearDown()
for i := 0; i < b.N; i++ {
b.StopTimer()
// Stop the timer, save the models, then start the timer again
models, err := createAndSaveTestModels(100)
if err != nil {
b.Fatal(err)
}
b.StartTimer()
t := testPool.NewTransaction()
for _, model := range models {
deleted := false
t.Delete(testModels, model.ModelID(), &deleted)
}
if err := t.Exec(); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkDeleteAll100 deletes 100 models with the DeleteAll command.
// It recreates the models on each iteration, but stops the timer during
// the Save methods, so only DeleteAll is timed.
func BenchmarkDeleteAll100(b *testing.B) {
testingSetUp()
defer testingTearDown()
for i := 0; i < b.N; i++ {
b.StopTimer()
_, err := createAndSaveTestModels(100)
if err != nil {
b.Fatal(err)
}
b.StartTimer()
if _, err := testModels.DeleteAll(); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkDeleteAll1000 deletes 1,000 models with the DeleteAll command
func BenchmarkDeleteAll1000(b *testing.B) {
testingSetUp()
defer testingTearDown()
for i := 0; i < b.N; i++ {
b.StopTimer()
_, err := createAndSaveTestModels(1000)
if err != nil {
b.Fatal(err)
}
b.StartTimer()
if _, err := testModels.DeleteAll(); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkCount100 counts 100 models with the Count command
func BenchmarkCount100(b *testing.B) {
testingSetUp()
defer testingTearDown()
_, err := createAndSaveTestModels(100)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := testModels.Count(); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkCount10000 counts 10,000 models with the Count command
func BenchmarkCount10000(b *testing.B) {
testingSetUp()
defer testingTearDown()
_, err := createAndSaveTestModels(10000)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := testModels.Count(); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkQueryFilterInt1From1 runs a query which selects 1
// model out of 1 total, filtering by the Int field
func BenchmarkQueryFilterInt1From1(b *testing.B) {
benchmarkQueryFilterInt(b, 1, 1)
}
// BenchmarkQueryFilterInt1From10 runs a query which selects 1
// model out of 10 total, filtering by the Int field
func BenchmarkQueryFilterInt1From10(b *testing.B) {
benchmarkQueryFilterInt(b, 1, 10)
}
// BenchmarkQueryFilterInt10From100 runs a query which selects 10
// models out of 100 total, filtering by the Int field
func BenchmarkQueryFilterInt10From100(b *testing.B) {
benchmarkQueryFilterInt(b, 10, 100)
}
// BenchmarkQueryFilterInt100From1000 runs a query which selects 100
// models out of 1000 total, filtering by the Int field
func BenchmarkQueryFilterInt100From1000(b *testing.B) {
benchmarkQueryFilterInt(b, 100, 1000)
}
// BenchmarkQueryFilterString1From1 runs a query which selects
// 1 model out of 1 models total, filtering by the String field
func BenchmarkQueryFilterString1From1(b *testing.B) {
benchmarkQueryFilterString(b, 1, 1)
}
// BenchmarkQueryFilterString1From10 runs a query which selects
// 1 model out of 10 models total, filtering by the String field
func BenchmarkQueryFilterString1From10(b *testing.B) {
benchmarkQueryFilterString(b, 1, 10)
}
// BenchmarkQueryFilterString10From100 runs a query which selects
// 10 models out of 100 models total, filtering by the String field
func BenchmarkQueryFilterString10From100(b *testing.B) {
benchmarkQueryFilterString(b, 10, 100)
}
// BenchmarkQueryFilterString100From1000 runs a query which selects
// 100 models out of 1,000 models total, filtering by the String field
func BenchmarkQueryFilterString100From1000(b *testing.B) {
benchmarkQueryFilterString(b, 100, 1000)
}
// BenchmarkQueryFilterBool1From1 runs a query which selects
// 1 model out of 1 models total, filtering by the Bool field
func BenchmarkQueryFilterBool1From1(b *testing.B) {
benchmarkQueryFilterBool(b, 1, 1)
}
// BenchmarkQueryFilterBool1From10 runs a query which selects
// 1 model out of 10 models total, filtering by the Bool field
func BenchmarkQueryFilterBool1From10(b *testing.B) {
benchmarkQueryFilterBool(b, 1, 10)
}
// BenchmarkQueryFilterBool10From100 runs a query which selects
// 10 models out of 100 models total, filtering by the Bool field
func BenchmarkQueryFilterBool10From100(b *testing.B) {
benchmarkQueryFilterBool(b, 10, 100)
}
// BenchmarkQueryFilterBool100From1000 runs a query which selects
// 100 models out of 1000 models total, filtering by the Bool field
func BenchmarkQueryFilterBool100From1000(b *testing.B) {
benchmarkQueryFilterBool(b, 100, 1000)
}
// BenchmarkQueryOrderInt100 runs a query which finds all 100 models ordered
// by the Int field
func BenchmarkQueryOrderInt100(b *testing.B) {
benchmarkQueryOrder(b, 100, "Int")
}
// BenchmarkQueryOrderInt10000 runs a query which finds all 10,000 models
// ordered by the Int field
func BenchmarkQueryOrderInt10000(b *testing.B) {
benchmarkQueryOrder(b, 10000, "Int")
}
// BenchmarkQueryOrderString100 runs a query which finds all 100 models ordered
// by the String field
func BenchmarkQueryOrderString100(b *testing.B) {
benchmarkQueryOrder(b, 100, "String")
}
// BenchmarkQueryOrderString10000 runs a query which finds all 10,000 models
// ordered by the String field
func BenchmarkQueryOrderString10000(b *testing.B) {
benchmarkQueryOrder(b, 10000, "String")
}
// BenchmarkQueryOrderBool100 runs a query which finds all 100 models ordered
// by the Bool field
func BenchmarkQueryOrderBool100(b *testing.B) {
benchmarkQueryOrder(b, 100, "Bool")
}
// BenchmarkQueryOrderBool10000 runs a query which finds all 10,000 models
// ordered by the Bool field
func BenchmarkQueryOrderBool10000(b *testing.B) {
benchmarkQueryOrder(b, 10000, "Bool")
}
// BenchmarkComplexQuery runs a query which incorporates nearly all options.
// The query has a filter on the String and Int Fields, is ordered in reverse
// by the Bool field, and includes only Bool and Int. Out of 1,000 models created,
// 100 should fit the query criteria, but the query limits the number of results
// to 10.
func BenchmarkComplexQuery(b *testing.B) {
testingSetUp()
defer testingTearDown()
// create 1000 models to be saved
models := createIndexedTestModels(1000)
// give 100 models an Int value of 1 and all others
// an Int value of 2
for _, m := range models[100:200] {
m.Int = 1
}
for _, m := range append(models[0:100], models[200:]...) {
m.Int = 2
}
// give 100 models a String value of "find me" and all others
// a String value of "not me"
for _, m := range models[150:250] {
m.String = "find me"
}
for _, m := range append(models[0:150], models[250:]...) {
m.String = "not me"
}
// Save all the models in a single transaction
t := testPool.NewTransaction()
for _, model := range models {
t.Save(indexedTestModels, model)
}
if err := t.Exec(); err != nil {
b.Fatal(err)
}
// Construct the query and benchmark it
q := indexedTestModels.NewQuery().Filter("Int =", 1).Filter("String =", "find me").Order("Bool").Include("Int", "Bool").Limit(10).Offset(10)
benchmarkQuery(b, q)
}
func benchmarkQuery(b *testing.B, q *Query) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := q.Run(&[]*indexedTestModel{}); err != nil {
b.Fatal(err)
}
}
}
func benchmarkQueryFilterInt(b *testing.B, selected int, total int) {
testingSetUp()
defer testingTearDown()
models := createIndexedTestModels(total)
t := testPool.NewTransaction()
for i := 0; i < selected; i++ {
models[i].Int = 1
t.Save(indexedTestModels, models[i])
}
for i := selected; i < len(models); i++ {
models[i].Int = 2
t.Save(indexedTestModels, models[i])
}
if err := t.Exec(); err != nil {
b.Fatal(err)
}
benchmarkQuery(b, indexedTestModels.NewQuery().Filter("Int =", 1))
}
func benchmarkQueryFilterString(b *testing.B, selected int, total int) {
testingSetUp()
defer testingTearDown()
models := createIndexedTestModels(total)
t := testPool.NewTransaction()
for i := 0; i < selected; i++ {
models[i].String = "find me"
t.Save(indexedTestModels, models[i])
}
for i := selected; i < len(models); i++ {
models[i].String = "not me"
t.Save(indexedTestModels, models[i])
}
if err := t.Exec(); err != nil {
b.Fatal(err)
}
benchmarkQuery(b, indexedTestModels.NewQuery().Filter("String =", "find me"))
}
func benchmarkQueryFilterBool(b *testing.B, selected int, total int) {
testingSetUp()
defer testingTearDown()
models := createIndexedTestModels(total)
t := testPool.NewTransaction()
for i := 0; i < selected; i++ {
models[i].Bool = true
t.Save(indexedTestModels, models[i])
}
for i := selected; i < len(models); i++ {
models[i].Bool = false
t.Save(indexedTestModels, models[i])
}
if err := t.Exec(); err != nil {
b.Fatal(err)
}
benchmarkQuery(b, indexedTestModels.NewQuery().Filter("Bool =", true))
}
func benchmarkQueryOrder(b *testing.B, n int, field string) {
testingSetUp()
defer testingTearDown()
if _, err := createAndSaveIndexedTestModels(n); err != nil {
b.Fatal(err)
}
q := indexedTestModels.NewQuery().Order(field)
benchmarkQuery(b, q)
}
// selectUnique selects num random, unique strings from a slice of strings
func selectUnique(num int, ids []string) []string {
selected := make(map[string]bool)
for len(selected) < num {
index := rand.Intn(len(ids) - 1)
id := ids[index]
if _, found := selected[id]; !found {
selected[id] = true
}
}
results := make([]string, 0)
for key := range selected {
results = append(results, key)
}
return results
}