-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.go
475 lines (402 loc) · 12.6 KB
/
tests.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
package main
import (
"context"
"fmt"
"math/rand"
"time"
"github.com/google/uuid"
"github.com/oklog/ulid/v2"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
func init() {
uuid.EnableRandPool()
}
type mongoDocumentULID struct {
ID ulid.ULID `bson:"_id"`
}
type mongoDocumentUUID struct {
ID uuid.UUID `bson:"_id"`
}
type mongoDocumentObjectID struct {
ID primitive.ObjectID `bson:"_id"`
}
type TesterResults struct {
InsertsBatched1M1K *InsertBatchesTestResult
InsertsBatched1M5K *InsertBatchesTestResult
InsertsBatched1M10K *InsertBatchesTestResult
Insert1M *InsertTestResult
InsertsBatchedPres10M10K *InsertBatchesWithPresentTestResult
InsertsBatchedPres10M100K *InsertBatchesWithPresentTestResult
}
type Tester struct {
Coll *mongo.Collection
}
func (t *Tester) Run() (*TesterResults, error) {
var err error
results := new(TesterResults)
const (
OneMillion = 1000000
TenMillion = 10 * OneMillion
OneThousand = 1000
FiveThousand = 5 * OneThousand
TenThousand = 10 * OneThousand
HundredThousand = 100 * OneThousand
)
results.InsertsBatched1M1K, err = t.testInsertBatches(OneMillion, OneThousand)
if err != nil {
return nil, fmt.Errorf("failed to run insert batches test: %w", err)
}
results.InsertsBatched1M5K, err = t.testInsertBatches(OneMillion, FiveThousand)
if err != nil {
return nil, fmt.Errorf("failed to run insert batches test: %w", err)
}
results.InsertsBatched1M10K, err = t.testInsertBatches(OneMillion, TenThousand)
if err != nil {
return nil, fmt.Errorf("failed to run insert batches test: %w", err)
}
results.Insert1M, err = t.testInserts(OneMillion)
if err != nil {
return nil, fmt.Errorf("failed to run inserts test: %w", err)
}
results.InsertsBatchedPres10M10K, err = t.testInsertBatchesWithPresent(TenMillion, TenMillion, TenThousand)
if err != nil {
return nil, fmt.Errorf("failed to run insert batches with present test: %w", err)
}
results.InsertsBatchedPres10M100K, err = t.testInsertBatchesWithPresent(TenMillion, TenMillion, HundredThousand)
if err != nil {
return nil, fmt.Errorf("failed to run insert batches with present test: %w", err)
}
return results, nil
}
type InsertBatchesTestResult struct {
ULIDDuration time.Duration
UUIDDuration time.Duration
ObjectIDDuration time.Duration
}
func (t *Tester) testInsertBatches(totalDocs, batchSize int) (*InsertBatchesTestResult, error) {
var start time.Time
result := new(InsertBatchesTestResult)
{
start = time.Now()
if err := t.insertDocumentsInBatches(batchSize, generateDocsUlid(totalDocs)); err != nil {
return nil, fmt.Errorf("error on insert documents in batches test run: %w", err)
}
result.ULIDDuration = time.Now().Sub(start)
if err := t.dropCollection(); err != nil {
return nil, fmt.Errorf("collection cleanup error: %w", err)
}
}
{
start = time.Now()
if err := t.insertDocumentsInBatches(batchSize, generateDocsUUID(totalDocs)); err != nil {
return nil, fmt.Errorf("error on insert documents in batches test run: %w", err)
}
result.UUIDDuration = time.Now().Sub(start)
if err := t.dropCollection(); err != nil {
return nil, fmt.Errorf("collection cleanup error: %w", err)
}
}
{
start = time.Now()
if err := t.insertDocumentsInBatches(batchSize, generateDocsObjectID(totalDocs)); err != nil {
return nil, fmt.Errorf("error on insert documents in batches test run: %w", err)
}
result.ObjectIDDuration = time.Now().Sub(start)
if err := t.dropCollection(); err != nil {
return nil, fmt.Errorf("collection cleanup error: %w", err)
}
}
return result, nil
}
type InsertTestResult struct {
ULIDDuration time.Duration
UUIDDuration time.Duration
ObjectIDDuration time.Duration
}
func (t *Tester) testInserts(totalDocs int) (*InsertTestResult, error) {
var start time.Time
result := new(InsertTestResult)
{
start = time.Now()
if err := t.insertDocuments(generateDocsUlid(totalDocs)); err != nil {
return nil, fmt.Errorf("error on insert documents test run: %w", err)
}
result.ULIDDuration = time.Now().Sub(start)
if err := t.dropCollection(); err != nil {
return nil, fmt.Errorf("collection cleanup error: %w", err)
}
}
{
start = time.Now()
if err := t.insertDocuments(generateDocsUUID(totalDocs)); err != nil {
return nil, fmt.Errorf("error on insert documents test run: %w", err)
}
result.UUIDDuration = time.Now().Sub(start)
if err := t.dropCollection(); err != nil {
return nil, fmt.Errorf("collection cleanup error: %w", err)
}
}
{
start = time.Now()
if err := t.insertDocuments(generateDocsObjectID(totalDocs)); err != nil {
return nil, fmt.Errorf("error on insert documents test run: %w", err)
}
result.ObjectIDDuration = time.Now().Sub(start)
if err := t.dropCollection(); err != nil {
return nil, fmt.Errorf("collection cleanup error: %w", err)
}
}
return result, nil
}
type InsertBatchesWithPresentTestResult struct {
ObjectIDInsertDuration time.Duration
ObjectIDIdxSize int64
ObjectIDGetDuration time.Duration
ULIDInsertDuration time.Duration
ULIDIdxSize int64
ULIDGetDuration time.Duration
UUIDInsertDuration time.Duration
UUIDIdxSize int64
UUIDGetDuration time.Duration
}
func (t *Tester) testInsertBatchesWithPresent(insertCount, presentCount, batchSize int) (*InsertBatchesWithPresentTestResult, error) {
var start time.Time
result := new(InsertBatchesWithPresentTestResult)
const prepareBatchSize = 100000
const getProbes = 100
{
// provisioning with fixtures
fixtures := generateDocsUlid(presentCount)
if err := t.insertDocumentsInBatches(prepareBatchSize, fixtures); err != nil {
return nil, fmt.Errorf("error on insert documents in batches: %w", err)
}
// inserting batches
start = time.Now()
if err := t.insertDocumentsInBatches(batchSize, generateDocsUlid(insertCount)); err != nil {
return nil, fmt.Errorf("error on insert documents in batches test run: %w", err)
}
result.ULIDInsertDuration = time.Now().Sub(start)
// getting random docs
getIDs := pickRandomULID(fixtures, getProbes)
start = time.Now()
for _, id := range getIDs {
if err := t.getDocumentByID(id); err != nil {
return nil, fmt.Errorf("error on getting document by id: %w", err)
}
}
result.ULIDGetDuration = time.Now().Sub(start) / getProbes
// getting the index size
if idxSize, err := t.getDefaultIDIndexSize(); err != nil {
return nil, fmt.Errorf("failed to get default id index size: %w", err)
} else {
result.ULIDIdxSize = idxSize
}
if err := t.dropCollection(); err != nil {
return nil, fmt.Errorf("collection cleanup error: %w", err)
}
}
{
// provisioning with fixtures
fixtures := generateDocsUUID(presentCount)
if err := t.insertDocumentsInBatches(prepareBatchSize, fixtures); err != nil {
return nil, fmt.Errorf("error on insert documents in batches: %w", err)
}
// inserting batches
start = time.Now()
if err := t.insertDocumentsInBatches(batchSize, generateDocsUUID(insertCount)); err != nil {
return nil, fmt.Errorf("error on insert documents in batches test run: %w", err)
}
result.UUIDInsertDuration = time.Now().Sub(start)
// getting random docs
getIDs := pickRandomUUID(fixtures, getProbes)
start = time.Now()
for _, id := range getIDs {
if err := t.getDocumentByID(id); err != nil {
return nil, fmt.Errorf("error on getting document by id: %w", err)
}
}
result.UUIDGetDuration = time.Now().Sub(start) / getProbes
// getting the index size
if idxSize, err := t.getDefaultIDIndexSize(); err != nil {
return nil, fmt.Errorf("failed to get default id index size: %w", err)
} else {
result.UUIDIdxSize = idxSize
}
if err := t.dropCollection(); err != nil {
return nil, fmt.Errorf("collection cleanup error: %w", err)
}
}
{
// provisioning with fixtures
fixtures := generateDocsObjectID(presentCount)
if err := t.insertDocumentsInBatches(prepareBatchSize, fixtures); err != nil {
return nil, fmt.Errorf("error on insert documents in batches: %w", err)
}
// inserting batches
start = time.Now()
if err := t.insertDocumentsInBatches(batchSize, generateDocsObjectID(insertCount)); err != nil {
return nil, fmt.Errorf("error on insert documents in batches test run: %w", err)
}
result.ObjectIDInsertDuration = time.Now().Sub(start)
// getting random docs
getIDs := pickRandomObjectID(fixtures, getProbes)
start = time.Now()
for _, id := range getIDs {
if err := t.getDocumentByID(id); err != nil {
return nil, fmt.Errorf("error on getting document by id: %w", err)
}
}
result.ObjectIDGetDuration = time.Now().Sub(start) / getProbes
// getting the index size
if idxSize, err := t.getDefaultIDIndexSize(); err != nil {
return nil, fmt.Errorf("failed to get default id index size: %w", err)
} else {
result.ObjectIDIdxSize = idxSize
}
if err := t.dropCollection(); err != nil {
return nil, fmt.Errorf("collection cleanup error: %w", err)
}
}
return result, nil
}
func (t *Tester) insertDocumentsInBatches(batchSize int, docs []interface{}) error {
totalDocs := len(docs)
var end int
for start := 0; start < totalDocs; start += batchSize {
if (start + batchSize) > totalDocs {
end = totalDocs
} else {
end = start + batchSize
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
_, err := t.Coll.InsertMany(ctx, docs[start:end])
cancel()
if err != nil {
return fmt.Errorf("error inserting documents in batch: %w", err)
}
}
return nil
}
func (t *Tester) insertDocuments(docs []interface{}) error {
totalDocs := len(docs)
for i := 0; i < totalDocs; i += 1 {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
_, err := t.Coll.InsertOne(ctx, docs[i])
cancel()
if err != nil {
return fmt.Errorf("error inserting document: %w", err)
}
}
return nil
}
func (t *Tester) getDocumentByID(id interface{}) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
_, err := t.Coll.Find(ctx, bson.M{"_id": id})
cancel()
if err != nil {
return fmt.Errorf("error getting document: %w", err)
}
return nil
}
func (t *Tester) dropCollection() error {
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Second)
defer cancel()
if err := t.Coll.Drop(ctx); err != nil {
return fmt.Errorf("failed to drop collection: %w", err)
}
return nil
}
func (t *Tester) getDefaultIDIndexSize() (int64, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
res := t.Coll.Database().RunCommand(ctx, bson.M{"collStats": "perftest"})
var document bson.M
if err := res.Decode(&document); err != nil {
return 0, fmt.Errorf("%w", err)
}
idxSizes, ok := document["indexSizes"]
if !ok {
return 0, fmt.Errorf("indexSizes key not found")
}
size, ok := idxSizes.(bson.M)["_id_"]
if !ok {
return 0, fmt.Errorf("_id_ key not found")
}
return int64(size.(int32)), nil
}
func generateDocsUlid(n int) []interface{} {
result := make([]interface{}, n)
for i := 0; i < n; i++ {
result[i] = mongoDocumentULID{
ID: ulid.Make(),
}
}
return result
}
func generateDocsUUID(n int) []interface{} {
result := make([]interface{}, n)
for i := 0; i < n; i++ {
result[i] = mongoDocumentUUID{
ID: uuid.New(),
}
}
return result
}
func generateDocsObjectID(n int) []interface{} {
result := make([]interface{}, n)
for i := 0; i < n; i++ {
result[i] = mongoDocumentObjectID{
ID: primitive.NewObjectID(),
}
}
return result
}
func byteCountIEC(b int64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB",
float64(b)/float64(div), "KMGTPE"[exp])
}
func pickRandomULID(docs []interface{}, n int) []ulid.ULID {
docsLen := len(docs)
var result = make([]ulid.ULID, n)
for i := 0; i < n; i++ {
d := docs[rand.Intn(docsLen)]
result[i] = d.(mongoDocumentULID).ID
}
return result
}
func pickRandomUUID(docs []interface{}, n int) []uuid.UUID {
docsLen := len(docs)
var result = make([]uuid.UUID, n)
for i := 0; i < n; i++ {
d := docs[rand.Intn(docsLen)]
result[i] = d.(mongoDocumentUUID).ID
}
return result
}
func pickRandomObjectID(docs []interface{}, n int) []primitive.ObjectID {
docsLen := len(docs)
var result = make([]primitive.ObjectID, n)
for i := 0; i < n; i++ {
d := docs[rand.Intn(docsLen)]
result[i] = d.(mongoDocumentObjectID).ID
}
return result
}
func calcDiffPercent(baseline, newVal int64) float64 {
var k float64 = 1
if newVal > baseline {
k = -1
}
return k * (float64(newVal) - float64(baseline)) * 100 / float64(baseline)
}