-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
473 lines (420 loc) · 15 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"math/rand"
"neobench/pkg/neobench"
"neobench/pkg/neobench/builtin"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/neo4j/neo4j-go-driver/v4/neo4j"
"github.com/pkg/errors"
"github.com/spf13/pflag"
)
var fInitMode bool
var fLatencyMode bool
var fScale int64
var fClients int
var fRate float64
var fAddress string
var fUser string
var fPassword string
var fEncryptionMode string
var fDuration time.Duration
var fProgress time.Duration
var fVariables map[string]string
var fBuiltinWorkloads []string
var fWorkloadFiles []string
var fWorkloadScripts []string
var fOutputFormat string
var fPrometheusAddr string
var fNoCheckCertificates bool
var fDriverDebugLogging bool
var fMaxConnLifetime time.Duration
func init() {
pflag.BoolVarP(&fInitMode, "init", "i", false, "when running built-in workloads, run their built-in dataset generator first")
pflag.Int64VarP(&fScale, "scale", "s", 1, "sets the `scale` variable, impact depends on workload")
pflag.IntVarP(&fClients, "clients", "c", 1, "number of concurrent clients / sessions")
pflag.StringVarP(&fAddress, "address", "a", "neo4j://localhost:7687", "address to connect to")
pflag.StringVarP(&fUser, "user", "u", "neo4j", "username")
pflag.StringVarP(&fPassword, "password", "p", "neo4j", "password")
pflag.StringVarP(&fEncryptionMode, "encryption", "e", "auto", "whether to use encryption, `auto`, `true` or `false`")
pflag.DurationVarP(&fDuration, "duration", "d", 60*time.Second, "duration to run, ex: 15s, 1m, 10h")
pflag.BoolVarP(&fLatencyMode, "latency", "l", false, "run in latency testing more rather than throughput mode")
pflag.Float64VarP(&fRate, "rate", "r", 1, "in latency mode (see -l) sets total transactions per second")
pflag.StringVarP(&fOutputFormat, "output", "o", "auto", "output format, `auto`, `interactive` or `csv`")
// Flags defining the workload to run
pflag.StringToStringVarP(&fVariables, "define", "D", nil, "defines variables for workload scripts and query parameters")
pflag.StringSliceVarP(&fBuiltinWorkloads, "builtin", "b", []string{}, "built-in workload to run 'tpcb-like' or 'ldbc-like', default is tpcb-like")
pflag.StringSliceVarP(&fWorkloadFiles, "file", "f", []string{}, "path to workload script file(s)")
pflag.StringArrayVarP(&fWorkloadScripts, "script", "S", []string{}, "script(s) to run, directly specified on the command line")
// Less common command line vars
pflag.DurationVar(&fProgress, "progress", 10*time.Second, "interval to report progress, ex: 15s, 1m, 1h")
pflag.BoolVar(&fNoCheckCertificates, "no-check-certificates", false, "disable TLS certificate validation, exposes your credentials to anyone on the network")
pflag.DurationVar(&fMaxConnLifetime, "max-conn-lifetime", 1*time.Hour, "when connections are older than this, they are ejected from the connection pool")
pflag.BoolVar(&fDriverDebugLogging, "driver-debug-logging", false, "enable debug-level logging for the underlying neo4j driver")
pflag.StringVar(&fPrometheusAddr, "prometheus", "", "enable prometheus metrics at this host:port, ex: localhost:1234, :1234")
}
func main() {
pflag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), `neobench is a benchmarking tool for Neo4j.
Usage:
neobench [OPTION]... [DBNAME]
Options:
`)
pflag.PrintDefaults()
}
pflag.Parse()
if len(os.Args) == 1 {
pflag.Usage()
os.Exit(1)
}
// If no workloads at all are specified, we run tpc-b
if len(fBuiltinWorkloads) == 0 && len(fWorkloadScripts) == 0 && len(fWorkloadFiles) == 0 {
fBuiltinWorkloads = []string{"tpcb-like"}
}
seed := time.Now().Unix()
scenario := describeScenario()
out, err := neobench.InitOutput(fOutputFormat, fPrometheusAddr)
if err != nil {
log.Fatal(err)
}
var encryptionMode neobench.EncryptionMode
switch strings.ToLower(fEncryptionMode) {
case "auto":
encryptionMode = neobench.EncryptionAuto
case "true", "yes", "y", "1":
encryptionMode = neobench.EncryptionOn
case "false", "no", "n", "0":
encryptionMode = neobench.EncryptionOff
default:
log.Fatalf("Invalid encryption mode '%s', needs to be one of 'auto', 'true' or 'false'", fEncryptionMode)
}
dbName := ""
if pflag.NArg() > 0 {
dbName = pflag.Arg(0)
}
driver, err := neobench.NewDriver(fAddress, fUser, fPassword, encryptionMode, !fNoCheckCertificates, func(c *neo4j.Config) {
c.UserAgent = "neobench"
c.MaxConnectionLifetime = fMaxConnLifetime
if fDriverDebugLogging {
c.Log = neo4j.ConsoleLogger(neo4j.DEBUG)
}
})
if err != nil {
log.Fatal(err)
}
variables := make(map[string]interface{})
variables["scale"] = fScale
for k, v := range fVariables {
intVal, err := strconv.ParseInt(v, 10, 64)
if err == nil {
variables[k] = intVal
continue
}
floatVal, err := strconv.ParseFloat(v, 64)
if err == nil {
variables[k] = floatVal
continue
}
log.Fatalf("-D and --define values must be integers or floats, failing to parse '%s': %s", v, err)
}
wrk, err := createWorkload(driver, dbName, variables, seed)
if err != nil {
log.Fatalf("%+v", err)
}
version, err := neo4jVersion(driver)
if err != nil {
log.Fatalf("%+v", err)
}
if fInitMode {
err = initWorkload(fBuiltinWorkloads, dbName, fScale, seed, driver, out, version)
if err != nil {
log.Fatalf("%+v", err)
}
}
if fDuration == 0 {
fmt.Printf("Duration (--duration) is 0, exiting without running any load\n")
os.Exit(0)
}
if fLatencyMode {
result, err := runBenchmark(driver, fAddress, dbName, scenario, out, wrk, fDuration, fLatencyMode, fClients, fRate, fProgress)
if err != nil {
out.Errorf(err.Error())
os.Exit(1)
}
out.ReportLatency(result)
if result.TotalFailed() == 0 {
os.Exit(0)
} else {
os.Exit(1)
}
} else {
result, err := runBenchmark(driver, fAddress, dbName, scenario, out, wrk, fDuration, fLatencyMode, fClients, fRate, fProgress)
if err != nil {
out.Errorf(err.Error())
os.Exit(1)
}
out.ReportThroughput(result)
if result.TotalFailed() == 0 {
os.Exit(0)
} else {
os.Exit(1)
}
}
}
func neo4jVersion(driver neo4j.Driver) (string, error) {
session := driver.NewSession(neo4j.SessionConfig{})
defer session.Close()
res, err := session.Run("CALL dbms.components() YIELD name,versions WHERE name=\"Neo4j Kernel\" RETURN versions[0] AS version, name LIMIT 1", nil)
if err != nil {
return "", err
}
record, err := res.Single()
if err != nil {
return "", err
}
rawVersion, _ := record.Get("version")
return rawVersion.(string), nil
}
func createWorkload(driver neo4j.Driver, dbName string, variables map[string]interface{}, seed int64) (neobench.Workload, error) {
var err error
scripts := make([]neobench.Script, 0)
csvLoader := neobench.NewCsvLoader()
for _, rawPath := range fBuiltinWorkloads {
path, weight := splitScriptAndWeight(rawPath)
builtinScripts, err := loadBuiltinWorkload(path, weight)
if err != nil {
return neobench.Workload{}, errors.Wrapf(err, "failed to load script '%s'", path)
}
scripts = append(scripts, builtinScripts...)
}
for _, rawPath := range fWorkloadFiles {
path, weight := splitScriptAndWeight(rawPath)
script, err := loadScriptFile(driver, dbName, variables, path, weight, csvLoader)
if err != nil {
return neobench.Workload{}, errors.Wrapf(err, "failed to load script '%s'", path)
}
scripts = append(scripts, script)
}
for i, scriptContent := range fWorkloadScripts {
script, err := loadScript(driver, dbName, variables, fmt.Sprintf("-S #%d", i), scriptContent, 1.0, csvLoader)
if err != nil {
return neobench.Workload{}, errors.Wrapf(err, "failed to parse script '%s'", scriptContent)
}
scripts = append(scripts, script)
}
return neobench.Workload{
Variables: variables,
Scripts: neobench.NewScripts(scripts...),
Rand: rand.New(rand.NewSource(seed)),
CsvLoader: csvLoader,
}, err
}
// Splits command-line specified scripts-with-weight into script and weight
//
// -f my.script@100 becomes "myscript", 100.0
// -b tpcb-like@10 becomes "tpcb-like", 10.0
func splitScriptAndWeight(raw string) (string, float64) {
parts := strings.Split(raw, "@")
if len(parts) < 2 {
return raw, 1.0
}
weight, err := strconv.ParseFloat(parts[1], 64)
if err != nil {
log.Fatalf("Failed to parse weight; value after @ symbol for workload weight must be a number: %s", raw)
}
return parts[0], weight
}
func loadScriptFile(driver neo4j.Driver, dbName string, vars map[string]interface{}, path string, weight float64,
csvLoader *neobench.CsvLoader) (neobench.Script, error) {
scriptContent, err := ioutil.ReadFile(path)
if err != nil {
return neobench.Script{}, fmt.Errorf("failed to read workload file at %s: %s", path, err)
}
return loadScript(driver, dbName, vars, path, string(scriptContent), weight, csvLoader)
}
func loadScript(driver neo4j.Driver, dbName string, vars map[string]interface{}, path, scriptContent string, weight float64,
csvLoader *neobench.CsvLoader) (neobench.Script, error) {
script, err := neobench.Parse(path, scriptContent, weight)
if err != nil {
return neobench.Script{}, err
}
readonly, err := neobench.WorkloadPreflight(driver, dbName, script, vars, csvLoader)
script.Readonly = readonly
return script, err
}
func loadBuiltinWorkload(path string, weight float64) ([]neobench.Script, error) {
if path == "tpcb-like" {
script, err := neobench.Parse("builtin:tpcp-like", builtin.TPCBLike, weight)
return []neobench.Script{script}, err
}
if path == "match-only" {
script, err := neobench.Parse("builtin:match-only", builtin.MatchOnly, weight)
return []neobench.Script{script}, err
}
if path == "ldbc-like" {
ic2Rate, ic6Rate, ic10Rate, ic14Rate := 37.0, 129.0, 30.0, 49.0
totalRate := ic2Rate + ic6Rate + ic10Rate + ic14Rate
ic2, err := neobench.Parse("builtin:ldbc-like/ic2", builtin.LDBCIC2, ic2Rate/totalRate*weight)
if err != nil {
return []neobench.Script{}, err
}
ic6, err := neobench.Parse("builtin:ldbc-like/ic6", builtin.LDBCIC6, ic6Rate/totalRate*weight)
if err != nil {
return []neobench.Script{}, err
}
ic10, err := neobench.Parse("builtin:ldbc-like/ic10", builtin.LDBCIC10, ic10Rate/totalRate*weight)
if err != nil {
return []neobench.Script{}, err
}
ic14, err := neobench.Parse("builtin:ldbc-like/ic14", builtin.LDBCIC14, ic14Rate/totalRate*weight)
if err != nil {
return []neobench.Script{}, err
}
return []neobench.Script{
ic2,
ic6,
ic10,
ic14,
}, err
}
if path == "ldbc-like/ic2" {
script, err := neobench.Parse("builtin:ldbc-like/ic2", builtin.LDBCIC2, weight)
return []neobench.Script{script}, err
}
if path == "ldbc-like/ic6" {
script, err := neobench.Parse("builtin:ldbc-like/ic6", builtin.LDBCIC6, weight)
return []neobench.Script{script}, err
}
if path == "ldbc-like/ic10" {
script, err := neobench.Parse("builtin:ldbc-like/ic10", builtin.LDBCIC10, weight)
return []neobench.Script{script}, err
}
if path == "ldbc-like/ic14" {
script, err := neobench.Parse("builtin:ldbc-like/ic14", builtin.LDBCIC14, weight)
return []neobench.Script{script}, err
}
return []neobench.Script{}, fmt.Errorf("unknown built-in workload: %s, supported built-in workloads are 'tpcb-like', 'match-only' and 'ldbc-like'", path)
}
func describeScenario() string {
out := strings.Builder{}
for _, path := range fBuiltinWorkloads {
out.WriteString(fmt.Sprintf(" -b %s", path))
}
for _, path := range fWorkloadFiles {
out.WriteString(fmt.Sprintf(" -f %s", path))
}
for _, script := range fWorkloadScripts {
out.WriteString(fmt.Sprintf(" -S \"%s\"", script))
}
out.WriteString(fmt.Sprintf(" -c %d", fClients))
out.WriteString(fmt.Sprintf(" -s %d", fScale))
out.WriteString(fmt.Sprintf(" -d %s", fDuration))
out.WriteString(fmt.Sprintf(" -e %s", fEncryptionMode))
if fLatencyMode {
out.WriteString(fmt.Sprintf(" -l -r %.3f", fRate))
}
if fInitMode {
out.WriteString(" -i")
}
return out.String()
}
func runBenchmark(driver neo4j.Driver, url, databaseName, scenario string, out neobench.Output, wrk neobench.Workload,
runtime time.Duration, latencyMode bool, numClients int, rate float64, progressInterval time.Duration) (neobench.Result, error) {
stopCh, stop := neobench.SetupSignalHandler()
defer stop()
ratePerWorkerDuration := time.Duration(0)
if latencyMode {
ratePerWorkerDuration = neobench.TotalRatePerSecondToDurationPerClient(numClients, rate)
}
out.BenchmarkStart(databaseName, url, scenario)
resultChan := make(chan neobench.WorkerResult, numClients)
resultRecorders := make([]*neobench.ResultRecorder, 0)
var wg sync.WaitGroup
for i := 0; i < numClients; i++ {
wg.Add(1)
recorder := neobench.NewResultRecorder(int64(i))
resultRecorders = append(resultRecorders, recorder)
worker := neobench.NewWorker(driver, int64(i))
workerId := i
clientWork := wrk.NewClient()
go func() {
defer wg.Done()
result := worker.RunBenchmark(clientWork, databaseName, ratePerWorkerDuration, 0, stopCh, recorder)
resultChan <- result
if result.Error != nil {
out.Errorf("worker %d crashed: %s", workerId, result.Error)
stop()
}
}()
}
deadline := time.Now().Add(runtime)
awaitCompletion(stopCh, deadline, out, databaseName, scenario, progressInterval, resultRecorders)
stop()
wg.Wait()
return collectResults(databaseName, scenario, out, numClients, resultChan)
}
func collectResults(databaseName, scenario string, out neobench.Output, concurrency int, resultChan chan neobench.WorkerResult) (neobench.Result, error) {
// Collect results
results := make([]neobench.WorkerResult, 0, concurrency)
for i := 0; i < concurrency; i++ {
results = append(results, <-resultChan)
}
total := neobench.NewResult(databaseName, scenario)
// Process results into one histogram and check for errors
for _, res := range results {
if res.Error != nil {
out.Errorf("Worker failed: %v", res.Error)
continue
}
total.Add(res)
}
return total, nil
}
func initWorkload(paths []string, dbName string, scale, seed int64, driver neo4j.Driver, out neobench.Output, version string) error {
for _, path := range paths {
if path == "tpcb-like" {
return builtin.InitTPCBLike(scale, dbName, driver, out, version)
}
if path == "match-only" {
return builtin.InitTPCBLike(scale, dbName, driver, out, version)
}
if path == "ldbc-like" {
return builtin.InitLDBCLike(scale, seed, dbName, driver, out, version)
}
}
return nil
}
func awaitCompletion(stopCh chan struct{}, deadline time.Time, out neobench.Output, databaseName, scenario string, progressInterval time.Duration, recorders []*neobench.ResultRecorder) {
nextProgressReport := time.Now().Add(progressInterval)
originalDelta := deadline.Sub(time.Now()).Seconds()
for {
select {
case <-stopCh:
return
default:
}
now := time.Now()
delta := deadline.Sub(now)
if delta < 2*time.Second {
time.Sleep(delta)
break
}
if now.After(nextProgressReport) {
nextProgressReport = nextProgressReport.Add(progressInterval)
checkpoint := neobench.NewResult(databaseName, scenario)
for _, r := range recorders {
checkpoint.Add(r.ProgressReport(time.Now()))
}
completeness := 1 - delta.Seconds()/originalDelta
out.ReportWorkloadProgress(completeness, checkpoint)
}
time.Sleep(time.Millisecond * 100)
}
}