-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
226 lines (193 loc) · 5.89 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
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
package main
import (
"encoding/csv"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/url"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/c2h5oh/datasize"
"github.com/domodwyer/mpjbt/mongo"
"github.com/domodwyer/mpjbt/plan"
"github.com/domodwyer/mpjbt/postgres"
)
var (
endpoint, tableName, histPath, paddingSize string
updateFreq, readFreq, numWorkers uint64
opsMax uint64
timeout time.Duration
workload string
versionTag = "unknown"
versionDate = "unknown"
)
func init() {
fs := flag.NewFlagSet("", flag.ExitOnError)
fs.StringVar(&endpoint, "connect", "", "Connection string")
fs.StringVar(&histPath, "histogram", "", "Histogram output file path (CSV)")
fs.StringVar(&tableName, "table", "test", "Table/collection name")
fs.StringVar(&paddingSize, "padding", "0", "Amount of binary padding in the records (valid suffixes: kb, mb)")
fs.StringVar(&workload, "workload", "insert", "Workload name")
fs.Uint64Var(&opsMax, "ops", 0, "Number of `operations` to perform (0 == unlimited)")
fs.Uint64Var(&numWorkers, "workers", 30, "Number of concurrent workers")
fs.DurationVar(&timeout, "timeout", time.Duration(0), "Stop if runtime exceeds `d` (0 == unlimited, valid suffixes: s,m,h")
fs.Usage = func() {
fmt.Fprintf(os.Stderr, "Build %s (%s)\n\n", versionTag, versionDate)
fs.PrintDefaults()
var info = `
Available workloads:
insert:
Insert records with a monotonically increasing ID
insert-update:
Same as "insert' but immediately updates the record
insert-select:
Same as insert, but immediately reads the record
insert5-select95:
Insert a record 5% of the time, and read the most recent record the other 95%
select-uniform:
Read a totally random record
select-zipfian:
Read a random record heavily weighted towards the most recent
select-update-uniform:
Same as select-uniform, but the record is immediately updated
select-update-zipfian:
Same as select-zipfian, but the record is immediately updated
update-zipfian:
Update a record, weighted towards the highest IDs
update-uniform:
Update a random record
read-range:
Perform a range query on the age field (age > 45 AND age < 75)
Postgres dial string parameters:
See https://godoc.org/github.com/lib/pq#hdr-Connection_String_Parameters
Example: "postgres://localhost/test?sslmode=disable;binary_parameters=yes"
Mongo dial string parameters:
In addition to the driver dial parameters (https://godoc.org/github.com/globalsign/mgo#Dial)
there are several more flags for specifying session parameters:
readConcern: (docs: https://docs.mongodb.com/manual/reference/read-concern/)
majority / local / linearizable
writeConcern: (docs: https://docs.mongodb.com/manual/reference/write-concern/)
majority / <number>
journal: (docs: https://docs.mongodb.com/manual/core/journaling/)
true / false
fsync: (docs: https://docs.mongodb.com/manual/reference/command/fsync/)
true / false
The default values of the above are the defaults specified by MonogDB.
Example: "mongodb://localhost/test?journal=true&writeConcern=majority"
`
fmt.Fprintf(os.Stderr, "\n%s\n", info)
}
fs.Parse(os.Args[1:])
if endpoint == "" {
fs.Usage()
os.Exit(1)
}
}
func main() {
var histW = ioutil.Discard
if histPath != "" {
f, err := os.Create(histPath)
if err != nil {
log.Fatal(err)
}
defer f.Close()
histW = f
}
// Parse the record padding
var padding datasize.ByteSize
if err := padding.UnmarshalText([]byte(paddingSize)); err != nil {
log.Fatalf("padding: %v", err)
}
// Get the correct provider for this DB type
db, err := getDB(endpoint, tableName)
if err != nil {
log.Fatal(err)
}
// Create the work plan
dbplan := plan.New(opsMax, padding.Bytes())
if err := setWorkload(workload, dbplan, db); err != nil {
log.Fatal(err)
}
// Clean-up handler
var sigInfo = make(chan os.Signal, 1)
signal.Notify(sigInfo, syscall.SIGINT)
// If the user hits Ctrl+C, stop the plan and aggregate the statistics.
go func() {
<-sigInfo
fmt.Printf("\nStopping... Ctrl+C again to force\n")
dbplan.Stop()
<-sigInfo
fmt.Printf("\nForcing...")
os.Exit(1)
}()
// If we exceed the timeout, stop the plan
if timeout != time.Duration(0) {
go func() {
time.Sleep(timeout)
dbplan.Stop()
fmt.Printf("\nStopped due to timeout (%v)...\n", timeout)
}()
}
// Go!
results := dbplan.Run(numWorkers, os.Stdout)
// Output the latency histograms as CSV files to histW.
reportHistograms(histW, results)
}
// getDB parses endpoint and returns a database provider based on the scheme.
//
// Available endpoint scehmes are "mongodb" and "postgres".
func getDB(endpoint, tableName string) (dbProvider, error) {
purl, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
var provider dbProvider
switch purl.Scheme {
case "mongodb":
provider, err = mongo.NewProvider(purl, tableName)
if err != nil {
return nil, err
}
case "postgres":
provider, err = postgres.NewProvider(purl, tableName)
if err != nil {
return nil, err
}
default:
log.Fatalf("unknown scheme '%s', valid: mongodb postgres", purl.Scheme)
}
return provider, nil
}
// reportHistograms writes runtime configuration and results to w as a CSV file.
func reportHistograms(w io.Writer, results []plan.Result) {
// Print some run statistics
cw := csv.NewWriter(w)
cw.WriteAll([][]string{
{"Endpoint:", endpoint},
{"Table:", tableName},
{"RecordLimit:", strconv.FormatUint(opsMax, 10)},
{"Workers:", strconv.FormatUint(numWorkers, 10)},
{"PaddingSize:", paddingSize},
{"Workload:", workload},
{},
})
defer cw.Flush()
for _, op := range results {
if op.Histogram.Count == 0 {
return
}
// Print to stdout
fmt.Printf("\n%s latency:\n", op.Name)
op.Histogram.Print(os.Stdout)
// Write to w
fmt.Fprintf(w, "\n%s latency\n", op.Name)
if err := op.Histogram.WriteCSV(w); err != nil {
log.Printf("error writing histogram: %v", err)
}
}
}