forked from mongodb/mongo-go-driver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
167 lines (139 loc) · 3.6 KB
/
main.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
// 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 main
import (
"context"
"flag"
"log"
"math/rand"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"time"
"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/core/command"
"github.com/mongodb/mongo-go-driver/core/description"
"github.com/mongodb/mongo-go-driver/core/dispatch"
"github.com/mongodb/mongo-go-driver/core/option"
"github.com/mongodb/mongo-go-driver/core/readpref"
"github.com/mongodb/mongo-go-driver/core/session"
"github.com/mongodb/mongo-go-driver/core/topology"
"github.com/mongodb/mongo-go-driver/core/uuid"
)
var concurrency = flag.Int("concurrency", 24, "how much concurrency should be used")
var ns = flag.String("namespace", "test.foo", "the namespace to use for test data")
func main() {
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
c, err := topology.New()
if err != nil {
log.Fatalf("unable to create topology: %s", err)
}
done := make(chan struct{})
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-sig
cancel()
close(done)
}()
log.Println("prepping")
err = prep(ctx, c)
if err != nil {
log.Fatalf("unable to prep: %s", err)
}
log.Println("done prepping")
log.Println("working")
for i := 0; i < *concurrency; i++ {
go work(ctx, i, c)
}
<-done
log.Println("interrupt received: shutting down")
_ = c.Disconnect(ctx)
log.Println("finished")
}
func prep(ctx context.Context, c *topology.Topology) error {
var docs = make([]*bson.Document, 0, 1000)
for i := 0; i < 1000; i++ {
docs = append(docs, bson.NewDocument(bson.EC.Int32("_id", int32(i))))
}
ns := command.ParseNamespace(*ns)
s, err := c.SelectServer(ctx, description.WriteSelector())
if err != nil {
return err
}
conn, err := s.Connection(ctx)
if err != nil {
return err
}
defer conn.Close()
deletes := []*bson.Document{bson.NewDocument(
bson.EC.SubDocument("q", bson.NewDocument()),
bson.EC.Int32("limit", 0),
)}
_, err = (&command.Delete{WriteConcern: nil, NS: ns, Deletes: deletes}).RoundTrip(ctx, s.Description(), conn)
if err != nil {
return err
}
_, err = (&command.Insert{
NS: ns,
Docs: docs,
}).RoundTrip(
ctx,
s.Description(),
conn,
)
return err
}
func work(ctx context.Context, idx int, c *topology.Topology) {
r := rand.New(rand.NewSource(time.Now().Unix()))
ns := command.ParseNamespace(*ns)
rp := readpref.Nearest()
for {
select {
case <-ctx.Done():
default:
limit := r.Intn(999) + 1
pipeline := bson.NewArray(
bson.VC.DocumentFromElements(
bson.EC.Int32("$limit", int32(limit)),
),
)
id, _ := uuid.New()
cmd := command.Aggregate{
NS: ns,
Pipeline: pipeline,
Opts: []option.AggregateOptioner{option.OptBatchSize(200)},
ReadPref: rp,
}
cursor, err := dispatch.Aggregate(
ctx, cmd, c,
description.ReadPrefSelector(rp),
description.ReadPrefSelector(rp),
id,
&session.Pool{},
)
if err != nil {
log.Printf("%d-failed executing aggregate: %s", idx, err)
continue
}
count := 0
for cursor.Next(ctx) {
count++
}
if cursor.Err() != nil {
_ = cursor.Close(ctx)
log.Printf("%d-failed iterating aggregate results: %s", idx, cursor.Err())
return
}
_ = cursor.Close(ctx)
log.Printf("%d-iterated %d docs", idx, count)
}
}
}