Skip to content

Commit 201b3c3

Browse files
author
Sam Kleinman
committed
GODRIVER-115: single document benchmarks
Change-Id: Ifcbc9c463345ba268f2fe2288104407e1e2169a8
1 parent ca0b2ce commit 201b3c3

File tree

6 files changed

+222
-19
lines changed

6 files changed

+222
-19
lines changed

benchmark/bson.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package benchmark
22

33
import (
4+
"errors"
45
"io/ioutil"
56
"path/filepath"
67

78
"github.com/mongodb/mongo-go-driver/bson"
8-
"github.com/pkg/errors"
99
)
1010

1111
const (

benchmark/bson_map.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package benchmark
33
import (
44
"bytes"
55
"context"
6+
"errors"
67
"fmt"
78

89
"github.com/mongodb/mongo-go-driver/bson"
9-
"github.com/pkg/errors"
1010
)
1111

1212
func bsonMapDecoding(ctx context.Context, tm TimerManager, iters int, dataSet string) error {

benchmark/harness.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ const (
1414
MinimumRuntime = 10 * time.Second
1515
MinIterations = 100
1616

17-
hundred = 100
18-
thousand = 10 * hundred
19-
tenThousand = 10 * thousand
17+
ten = 10
18+
hundred = ten * ten
19+
thousand = ten * hundred
20+
tenThousand = ten * thousand
2021
)
2122

2223
type BenchCase func(context.Context, TimerManager, int) error
@@ -178,5 +179,29 @@ func getAllCases() []*CaseDefinition {
178179
Size: 75310000,
179180
Runtime: StandardRuntime,
180181
},
182+
{
183+
Bench: SingleRunCommand,
184+
Count: tenThousand,
185+
Size: 160000,
186+
Runtime: StandardRuntime,
187+
},
188+
{
189+
Bench: SingleFindOneByID,
190+
Count: tenThousand,
191+
Size: 16220000,
192+
Runtime: StandardRuntime,
193+
},
194+
{
195+
Bench: SingleInsertSmallDocument,
196+
Count: tenThousand,
197+
Size: 2750000,
198+
Runtime: StandardRuntime,
199+
},
200+
{
201+
Bench: SingleInsertLargeDocument,
202+
Count: ten,
203+
Size: 27310890,
204+
Runtime: StandardRuntime,
205+
},
181206
}
182207
}

benchmark/single.go

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package benchmark
2+
3+
import (
4+
"context"
5+
"errors"
6+
7+
"github.com/mongodb/mongo-go-driver/bson"
8+
"github.com/mongodb/mongo-go-driver/internal/testutil"
9+
"github.com/mongodb/mongo-go-driver/mongo"
10+
)
11+
12+
const (
13+
singleAndMultiDataDir = "single_and_multi_document"
14+
tweetData = "tweet.json"
15+
smallData = "small_doc.json"
16+
largeData = "large_doc.json"
17+
)
18+
19+
func getClientDB(ctx context.Context) (*mongo.Database, error) {
20+
cs, err := testutil.GetConnString()
21+
if err != nil {
22+
return nil, err
23+
}
24+
client, err := mongo.NewClientFromConnString(cs)
25+
if err != nil {
26+
return nil, err
27+
}
28+
if err = client.Connect(ctx); err != nil {
29+
return nil, err
30+
}
31+
32+
db := client.Database(testutil.GetDBName(cs))
33+
return db, nil
34+
}
35+
36+
func SingleRunCommand(ctx context.Context, tm TimerManager, iters int) error {
37+
ctx, cancel := context.WithCancel(ctx)
38+
defer cancel()
39+
40+
db, err := getClientDB(ctx)
41+
if err != nil {
42+
return err
43+
}
44+
45+
cmd := bson.NewDocument(bson.EC.Boolean("ismaster", true))
46+
47+
tm.ResetTimer()
48+
for i := 0; i < iters; i++ {
49+
out, err := db.RunCommand(ctx, cmd)
50+
if err != nil {
51+
return err
52+
}
53+
// read the document and then throw it away to prevent
54+
if len(out) == 0 {
55+
return errors.New("output of ismaster is empty")
56+
}
57+
}
58+
tm.StopTimer()
59+
60+
return nil
61+
}
62+
63+
func SingleFindOneByID(ctx context.Context, tm TimerManager, iters int) error {
64+
ctx, cancel := context.WithCancel(ctx)
65+
defer cancel()
66+
67+
db, err := getClientDB(ctx)
68+
if err != nil {
69+
return err
70+
}
71+
72+
db = db.Client().Database("perftest")
73+
if err = db.Drop(ctx); err != nil {
74+
return err
75+
}
76+
77+
doc, err := loadSourceDocument(getProjectRoot(), perfDataDir, singleAndMultiDataDir, tweetData)
78+
if err != nil {
79+
return err
80+
}
81+
coll := db.Collection("corpus")
82+
for i := 0; i < iters; i++ {
83+
id := int32(i)
84+
res, err := coll.InsertOne(ctx, doc.Set(bson.EC.Int32("_id", id)))
85+
if err != nil {
86+
return err
87+
}
88+
if res.InsertedID == nil {
89+
return errors.New("insert failed")
90+
}
91+
}
92+
93+
tm.ResetTimer()
94+
95+
for i := 0; i < iters; i++ {
96+
res := coll.FindOne(ctx, bson.NewDocument(bson.EC.Int32("_id", int32(i))))
97+
if res == nil {
98+
return errors.New("find one query produced nil result")
99+
}
100+
}
101+
102+
tm.StopTimer()
103+
104+
if err = db.Drop(ctx); err != nil {
105+
return err
106+
}
107+
108+
return nil
109+
}
110+
111+
func singleInsertCase(ctx context.Context, tm TimerManager, iters int, data string) error {
112+
ctx, cancel := context.WithCancel(ctx)
113+
defer cancel()
114+
115+
db, err := getClientDB(ctx)
116+
if err != nil {
117+
return err
118+
}
119+
120+
db = db.Client().Database("perftest")
121+
if err = db.Drop(ctx); err != nil {
122+
return err
123+
}
124+
125+
doc, err := loadSourceDocument(getProjectRoot(), perfDataDir, singleAndMultiDataDir, data)
126+
if err != nil {
127+
return err
128+
}
129+
130+
_, err = db.RunCommand(ctx, bson.NewDocument(bson.EC.String("create", "corpus")))
131+
if err != nil {
132+
return err
133+
}
134+
135+
coll := db.Collection("corpus")
136+
137+
tm.ResetTimer()
138+
139+
for i := 0; i < iters; i++ {
140+
if _, err = coll.InsertOne(ctx, doc); err != nil {
141+
return err
142+
}
143+
144+
// TODO: should be remove after resolving GODRIVER-468
145+
_ = doc.Delete("_id")
146+
}
147+
148+
tm.StopTimer()
149+
150+
if err = db.Drop(ctx); err != nil {
151+
return err
152+
}
153+
154+
return nil
155+
}
156+
157+
func SingleInsertSmallDocument(ctx context.Context, tm TimerManager, iters int) error {
158+
return singleInsertCase(ctx, tm, iters, smallData)
159+
}
160+
161+
func SingleInsertLargeDocument(ctx context.Context, tm TimerManager, iters int) error {
162+
return singleInsertCase(ctx, tm, iters, largeData)
163+
}

benchmark/single_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package benchmark
2+
3+
import "testing"
4+
5+
func BenchmarkSingleRunCommand(b *testing.B) { WrapCase(SingleRunCommand)(b) }
6+
func BenchmarkSingleFindOneByID(b *testing.B) { WrapCase(SingleFindOneByID)(b) }
7+
func BenchmarkSingleInsertSmallDocument(b *testing.B) { WrapCase(SingleInsertSmallDocument)(b) }
8+
func BenchmarkSingleInsertLargeDocument(b *testing.B) { WrapCase(SingleInsertLargeDocument)(b) }

internal/testutil/config.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,30 +104,37 @@ func ColName(t *testing.T) string {
104104
// ConnString gets the globally configured connection string.
105105
func ConnString(t *testing.T) connstring.ConnString {
106106
connectionStringOnce.Do(func() {
107-
mongodbURI := os.Getenv("MONGODB_URI")
108-
if mongodbURI == "" {
109-
mongodbURI = "mongodb://localhost:27017"
110-
}
111-
112-
mongodbURI = AddTLSConfigToURI(mongodbURI)
113-
114-
var err error
115-
connectionString, err = connstring.Parse(mongodbURI)
116-
if err != nil {
117-
connectionStringErr = err
118-
}
107+
connectionString, connectionStringErr = GetConnString()
119108
})
120-
121109
if connectionStringErr != nil {
122110
t.Fatal(connectionStringErr)
123111
}
124112

125113
return connectionString
126114
}
127115

116+
func GetConnString() (connstring.ConnString, error) {
117+
mongodbURI := os.Getenv("MONGODB_URI")
118+
if mongodbURI == "" {
119+
mongodbURI = "mongodb://localhost:27017"
120+
}
121+
122+
mongodbURI = AddTLSConfigToURI(mongodbURI)
123+
124+
cs, err := connstring.Parse(mongodbURI)
125+
if err != nil {
126+
return connstring.ConnString{}, err
127+
}
128+
129+
return cs, nil
130+
}
131+
128132
// DBName gets the globally configured database name.
129133
func DBName(t *testing.T) string {
130-
cs := ConnString(t)
134+
return GetDBName(ConnString(t))
135+
}
136+
137+
func GetDBName(cs connstring.ConnString) string {
131138
if cs.Database != "" {
132139
return cs.Database
133140
}

0 commit comments

Comments
 (0)