forked from mongodb/mongo-go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti.go
142 lines (109 loc) · 2.75 KB
/
multi.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
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package benchmark
import (
"context"
"errors"
"go.mongodb.org/mongo-driver/x/bsonx"
)
func MultiFindMany(ctx context.Context, tm TimerManager, iters int) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
db, err := getClientDB(ctx)
if err != nil {
return err
}
defer db.Client().Disconnect(ctx)
db = db.Client().Database("perftest")
if err = db.Drop(ctx); err != nil {
return err
}
doc, err := loadSourceDocument(getProjectRoot(), perfDataDir, singleAndMultiDataDir, tweetData)
if err != nil {
return err
}
coll := db.Collection("corpus")
payload := make([]interface{}, iters)
for idx := range payload {
payload[idx] = doc
}
if _, err = coll.InsertMany(ctx, payload); err != nil {
return err
}
tm.ResetTimer()
cursor, err := coll.Find(ctx, bsonx.Doc{})
if err != nil {
return err
}
defer cursor.Close(ctx)
counter := 0
for cursor.Next(ctx) {
err = cursor.Err()
if err != nil {
return err
}
if len(cursor.Current) == 0 {
return errors.New("error retrieving document")
}
counter++
}
if counter != iters {
return errors.New("problem iterating cursors")
}
tm.StopTimer()
if err = cursor.Close(ctx); err != nil {
return err
}
if err = db.Drop(ctx); err != nil {
return err
}
return nil
}
func multiInsertCase(ctx context.Context, tm TimerManager, iters int, data string) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
db, err := getClientDB(ctx)
if err != nil {
return err
}
defer db.Client().Disconnect(ctx)
db = db.Client().Database("perftest")
if err = db.Drop(ctx); err != nil {
return err
}
doc, err := loadSourceDocument(getProjectRoot(), perfDataDir, singleAndMultiDataDir, data)
if err != nil {
return err
}
err = db.RunCommand(ctx, bsonx.Doc{{"create", bsonx.String("corpus")}}).Err()
if err != nil {
return err
}
payload := make([]interface{}, iters)
for idx := range payload {
payload[idx] = doc
}
coll := db.Collection("corpus")
tm.ResetTimer()
res, err := coll.InsertMany(ctx, payload)
if err != nil {
return err
}
tm.StopTimer()
if len(res.InsertedIDs) != iters {
return errors.New("bulk operation did not complete")
}
if err = db.Drop(ctx); err != nil {
return err
}
return nil
}
func MultiInsertSmallDocument(ctx context.Context, tm TimerManager, iters int) error {
return multiInsertCase(ctx, tm, iters, smallData)
}
func MultiInsertLargeDocument(ctx context.Context, tm TimerManager, iters int) error {
return multiInsertCase(ctx, tm, iters, largeData)
}