Skip to content

Commit 75948c7

Browse files
author
Sam Kleinman
committed
GODRIVER-115: multi document benchmarks
Change-Id: I0d73d3ae90bfd1286f2dfd158bea31e8873c5988
1 parent 59e0104 commit 75948c7

File tree

6 files changed

+183
-9
lines changed

6 files changed

+183
-9
lines changed

.evergreen/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ buildvariants:
631631
- name: perf
632632
display_name: "Performance"
633633
run_on:
634-
- ubuntu1604-test
634+
- ubuntu1604-build
635635
expansions:
636636
GO_DIST: "/opt/golang/go1.9"
637637
tasks:

benchmark/harness.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,5 +203,23 @@ func getAllCases() []*CaseDefinition {
203203
Size: 27310890,
204204
Runtime: StandardRuntime,
205205
},
206+
{
207+
Bench: MultiFindMany,
208+
Count: tenThousand,
209+
Size: 16220000,
210+
Runtime: StandardRuntime,
211+
},
212+
{
213+
Bench: MultiInsertSmallDocument,
214+
Count: tenThousand,
215+
Size: 2750000,
216+
Runtime: StandardRuntime,
217+
},
218+
{
219+
Bench: MultiInsertLargeDocument,
220+
Count: ten,
221+
Size: 27310890,
222+
Runtime: StandardRuntime,
223+
},
206224
}
207225
}

benchmark/harness_case.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ func (c *CaseDefinition) StopTimer() {
4040
if c.startAt.IsZero() {
4141
return
4242
}
43-
44-
c.startAt = time.Time{}
4543
c.Runtime += time.Since(c.startAt)
44+
c.startAt = time.Time{}
45+
}
46+
47+
func (c *CaseDefinition) roundedRuntime() time.Duration {
48+
return roundDurationMS(c.Runtime)
4649
}
4750

4851
func (c *CaseDefinition) Run(ctx context.Context) *BenchResult {
@@ -58,6 +61,7 @@ func (c *CaseDefinition) Run(ctx context.Context) *BenchResult {
5861

5962
fmt.Println("=== RUN", out.Name)
6063
c.startAt = time.Now()
64+
6165
for {
6266
if time.Since(c.startAt) > c.Runtime {
6367
if out.Trials >= MinIterations {
@@ -66,10 +70,10 @@ func (c *CaseDefinition) Run(ctx context.Context) *BenchResult {
6670
break
6771
}
6872
}
69-
7073
res := Result{
7174
Iterations: c.Count,
7275
}
76+
7377
c.StartTimer()
7478
res.Error = c.Bench(ctx, c, c.Count)
7579
c.StopTimer()
@@ -85,9 +89,9 @@ func (c *CaseDefinition) Run(ctx context.Context) *BenchResult {
8589

8690
out.Duration = out.totalDuration()
8791
if out.HasErrors() {
88-
fmt.Printf("--- FAIL: %s (%s)\n", out.Name, out.Duration.Round(time.Millisecond))
92+
fmt.Printf("--- FAIL: %s (%s)\n", out.Name, out.roundedRuntime())
8993
} else {
90-
fmt.Printf("--- PASS: %s (%s)\n", out.Name, out.Duration.Round(time.Millisecond))
94+
fmt.Printf("--- PASS: %s (%s)\n", out.Name, out.roundedRuntime())
9195
}
9296

9397
return out
@@ -100,7 +104,6 @@ func (c *CaseDefinition) String() string {
100104
}
101105

102106
func (c *CaseDefinition) Name() string { return getName(c.Bench) }
103-
104107
func getName(i interface{}) string {
105108
n := runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
106109
parts := strings.Split(n, ".")

benchmark/harness_results.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,15 @@ func (r *BenchResult) timings() []float64 {
8080

8181
func (r *BenchResult) totalDuration() time.Duration {
8282
var out time.Duration
83-
for _, r := range r.Raw {
84-
out += r.Duration
83+
for _, trial := range r.Raw {
84+
out += trial.Duration
8585
}
8686
return out
8787
}
8888

8989
func (r *BenchResult) adjustResults(data float64) float64 { return float64(r.DataSize) / data }
9090
func (r *BenchResult) getThroughput(data float64) float64 { return float64(r.Operations) / data }
91+
func (r *BenchResult) roundedRuntime() time.Duration { return roundDurationMS(r.Duration) }
9192

9293
func (r *BenchResult) String() string {
9394
return fmt.Sprintf("name=%s, trials=%d, secs=%s", r.Name, r.Trials, r.Duration)
@@ -113,3 +114,11 @@ type Result struct {
113114
Iterations int
114115
Error error
115116
}
117+
118+
func roundDurationMS(d time.Duration) time.Duration {
119+
rounded := d.Round(time.Millisecond)
120+
if rounded == 1<<63-1 {
121+
return 0
122+
}
123+
return rounded
124+
}

benchmark/multi.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package benchmark
2+
3+
import (
4+
"context"
5+
"errors"
6+
7+
"github.com/mongodb/mongo-go-driver/bson"
8+
)
9+
10+
func MultiFindMany(ctx context.Context, tm TimerManager, iters int) error {
11+
ctx, cancel := context.WithCancel(ctx)
12+
defer cancel()
13+
14+
db, err := getClientDB(ctx)
15+
if err != nil {
16+
return err
17+
}
18+
19+
db = db.Client().Database("perftest")
20+
if err = db.Drop(ctx); err != nil {
21+
return err
22+
}
23+
24+
doc, err := loadSourceDocument(getProjectRoot(), perfDataDir, singleAndMultiDataDir, tweetData)
25+
if err != nil {
26+
return err
27+
}
28+
29+
coll := db.Collection("corpus")
30+
31+
for i := 0; i < iters; i++ {
32+
if _, err = coll.InsertOne(ctx, doc); err != nil {
33+
return err
34+
}
35+
36+
// TODO: should be remove after resolving GODRIVER-468
37+
_ = doc.Delete("_id")
38+
}
39+
40+
tm.ResetTimer()
41+
42+
cursor, err := coll.Find(ctx, bson.NewDocument())
43+
if err != nil {
44+
return err
45+
}
46+
47+
counter := 0
48+
for cursor.Next(ctx) {
49+
err = cursor.Err()
50+
if err != nil {
51+
return err
52+
}
53+
r, err := cursor.DecodeBytes()
54+
if err != nil {
55+
return err
56+
}
57+
if len(r) == 0 {
58+
return errors.New("error retrieving document")
59+
}
60+
61+
counter++
62+
}
63+
64+
if counter != iters {
65+
return errors.New("problem iterating cursors")
66+
67+
}
68+
69+
tm.StopTimer()
70+
71+
if err = cursor.Close(ctx); err != nil {
72+
return err
73+
}
74+
75+
if err = db.Drop(ctx); err != nil {
76+
return err
77+
}
78+
79+
return nil
80+
}
81+
82+
func multiInsertCase(ctx context.Context, tm TimerManager, iters int, data string) error {
83+
ctx, cancel := context.WithCancel(ctx)
84+
defer cancel()
85+
86+
db, err := getClientDB(ctx)
87+
if err != nil {
88+
return err
89+
}
90+
91+
db = db.Client().Database("perftest")
92+
if err = db.Drop(ctx); err != nil {
93+
return err
94+
}
95+
96+
doc, err := loadSourceDocument(getProjectRoot(), perfDataDir, singleAndMultiDataDir, data)
97+
if err != nil {
98+
return err
99+
}
100+
101+
_, err = db.RunCommand(ctx, bson.NewDocument(bson.EC.String("create", "corpus")))
102+
if err != nil {
103+
return err
104+
}
105+
106+
payload := make([]interface{}, iters)
107+
for idx := range payload {
108+
payload[idx] = *doc
109+
}
110+
111+
coll := db.Collection("corpus")
112+
113+
tm.ResetTimer()
114+
res, err := coll.InsertMany(ctx, payload)
115+
if err != nil {
116+
return err
117+
}
118+
tm.StopTimer()
119+
120+
if len(res.InsertedIDs) != iters {
121+
return errors.New("bulk operation did not complete")
122+
}
123+
124+
if err = db.Drop(ctx); err != nil {
125+
return err
126+
}
127+
128+
return nil
129+
}
130+
131+
func MultiInsertSmallDocument(ctx context.Context, tm TimerManager, iters int) error {
132+
return multiInsertCase(ctx, tm, iters, smallData)
133+
}
134+
135+
func MultiInsertLargeDocument(ctx context.Context, tm TimerManager, iters int) error {
136+
return multiInsertCase(ctx, tm, iters, largeData)
137+
}

benchmark/multi_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package benchmark
2+
3+
import "testing"
4+
5+
func BenchmarkMultiFindMany(b *testing.B) { WrapCase(MultiFindMany)(b) }
6+
func BenchmarkMultiInsertSmallDocument(b *testing.B) { WrapCase(MultiInsertSmallDocument)(b) }
7+
func BenchmarkMultiInsertLargeDocument(b *testing.B) { WrapCase(MultiInsertLargeDocument)(b) }

0 commit comments

Comments
 (0)