forked from scylladb/scylla-bench
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
344 lines (296 loc) · 11.5 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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"strings"
"sync/atomic"
"time"
"github.com/gocql/gocql"
)
var keyspaceName string
var tableName string
var counterTableName string
var concurrency int
var maximumRate int
var testDuration time.Duration
var partitionCount int64
var clusteringRowCount int64
var clusteringRowSize int64
var rowsPerRequest int
var provideUpperBound bool
var inRestriction bool
var noLowerBound bool
var timeout time.Duration
var startTime time.Time
var stopAll uint32
var measureLatency bool
func PrepareDatabase(session *gocql.Session, replicationFactor int) {
request := fmt.Sprintf("CREATE KEYSPACE IF NOT EXISTS %s WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : %d }", keyspaceName, replicationFactor)
err := session.Query(request).Exec()
if err != nil {
log.Fatal(err)
}
err = session.Query("CREATE TABLE IF NOT EXISTS " + keyspaceName + "." + tableName + " (pk bigint, ck bigint, v blob, PRIMARY KEY(pk, ck)) WITH compression = { }").Exec()
if err != nil {
log.Fatal(err)
}
err = session.Query("CREATE TABLE IF NOT EXISTS " + keyspaceName + "." + counterTableName + " (pk bigint, ck bigint, c1 counter, c2 counter, c3 counter, c4 counter, c5 counter, PRIMARY KEY(pk, ck)) WITH compression = { }").Exec()
if err != nil {
log.Fatal(err)
}
}
func GetWorkload(name string, threadId int, partitionOffset int64, mode string, writeRate int64, distribution string) WorkloadGenerator {
switch name {
case "sequential":
pksPerThread := partitionCount / int64(concurrency)
thisOffset := pksPerThread * int64(threadId)
var thisSize int64
if threadId+1 == concurrency {
thisSize = partitionCount - thisOffset
} else {
thisSize = pksPerThread
}
return NewSequentialVisitAll(thisOffset+partitionOffset, thisSize, clusteringRowCount)
case "uniform":
return NewRandomUniform(threadId, partitionCount, clusteringRowCount)
case "timeseries":
if mode == "read" {
return NewTimeSeriesReader(threadId, concurrency, partitionCount, clusteringRowCount, writeRate, distribution, startTime)
} else if mode == "write" {
return NewTimeSeriesWriter(threadId, concurrency, partitionCount, clusteringRowCount, startTime, int64(maximumRate/concurrency))
} else {
log.Fatal("time series workload supports only write and read modes")
}
default:
log.Fatal("unknown workload: ", name)
}
panic("unreachable")
}
func GetMode(name string) func(session *gocql.Session, resultChannel chan Result, workload WorkloadGenerator, rateLimiter RateLimiter) {
switch name {
case "write":
if rowsPerRequest == 1 {
return DoWrites
}
return DoBatchedWrites
case "counter_update":
return DoCounterUpdates
case "read":
return DoReads
case "counter_read":
return DoCounterReads
default:
log.Fatal("unknown mode: ", name)
}
panic("unreachable")
}
func PrintPartialResult(result *MergedResult) {
latencyError := ""
if errorRecordingLatency {
latencyError = "latency measurement error"
}
if measureLatency {
fmt.Println(result.Time, "\t", result.Operations, "\t", result.ClusteringRows, "\t", result.Errors,
"\t", time.Duration(result.Latency.Max()), "\t", time.Duration(result.Latency.ValueAtQuantile(99.9)), "\t", time.Duration(result.Latency.ValueAtQuantile(99)),
"\t", time.Duration(result.Latency.ValueAtQuantile(95)), "\t", time.Duration(result.Latency.ValueAtQuantile(90)), "\t", time.Duration(result.Latency.ValueAtQuantile(50)),
latencyError)
} else {
fmt.Println(result.Time, "\t", result.Operations, "\t", result.ClusteringRows, "\t", result.Errors)
}
}
func toInt(value bool) int {
if value {
return 1
} else {
return 0
}
}
func main() {
var mode string
var workload string
var consistencyLevel string
var replicationFactor int
var nodes string
var clientCompression bool
var connectionCount int
var pageSize int
var partitionOffset int64
var writeRate int64
var distribution string
flag.StringVar(&mode, "mode", "", "operating mode: write, read, counter_update, counter_read")
flag.StringVar(&workload, "workload", "", "workload: sequential, uniform, timeseries")
flag.StringVar(&consistencyLevel, "consistency-level", "quorum", "consistency level")
flag.IntVar(&replicationFactor, "replication-factor", 1, "replication factor")
flag.DurationVar(&timeout, "timeout", 5*time.Second, "request timeout")
flag.StringVar(&nodes, "nodes", "127.0.0.1", "nodes")
flag.BoolVar(&clientCompression, "client-compression", true, "use compression for client-coordinator communication")
flag.IntVar(&concurrency, "concurrency", 16, "number of used goroutines")
flag.IntVar(&connectionCount, "connection-count", 4, "number of connections")
flag.IntVar(&maximumRate, "max-rate", 0, "the maximum rate of outbound requests in op/s (0 for unlimited)")
flag.IntVar(&pageSize, "page-size", 1000, "page size")
flag.Int64Var(&partitionCount, "partition-count", 10000, "number of partitions")
flag.Int64Var(&clusteringRowCount, "clustering-row-count", 100, "number of clustering rows in a partition")
flag.Int64Var(&clusteringRowSize, "clustering-row-size", 4, "size of a single clustering row")
flag.IntVar(&rowsPerRequest, "rows-per-request", 1, "clustering rows per single request")
flag.BoolVar(&provideUpperBound, "provide-upper-bound", false, "whether read requests should provide an upper bound")
flag.BoolVar(&inRestriction, "in-restriction", false, "use IN restriction in read requests")
flag.BoolVar(&noLowerBound, "no-lower-bound", false, "do not provide lower bound in read requests")
flag.DurationVar(&testDuration, "duration", 0, "duration of the test in seconds (0 for unlimited)")
flag.Int64Var(&partitionOffset, "partition-offset", 0, "start of the partition range (only for sequential workload)")
flag.BoolVar(&measureLatency, "measure-latency", true, "measure request latency")
var startTimestamp int64
flag.Int64Var(&writeRate, "write-rate", 0, "rate of writes (relevant only for time series reads)")
flag.Int64Var(&startTimestamp, "start-timestamp", 0, "start timestamp of the write load (relevant only for time series reads)")
flag.StringVar(&distribution, "distribution", "uniform", "distribution of keys (relevant only for time series reads): uniform, hnormal")
flag.StringVar(&keyspaceName, "keyspace", "scylla_bench", "keyspace to use")
flag.StringVar(&tableName, "table", "test", "table to use")
flag.Parse()
counterTableName = "test_counters"
flag.Usage = func() {
fmt.Fprintf(os.Stdout, "Usage:\n%s [options]\n\n", os.Args[0])
flag.PrintDefaults()
}
if workload == "" {
log.Fatal("workload type needs to be specified")
}
if mode == "" {
log.Fatal("test mode needs to be specified")
}
if workload == "uniform" && testDuration == 0 {
log.Fatal("uniform workload requires limited test duration")
}
if partitionOffset != 0 && workload != "sequential" {
log.Fatal("partition-offset has a meaning only in sequential workloads")
}
readModeTweaks := toInt(inRestriction) + toInt(provideUpperBound) + toInt(noLowerBound)
if mode != "read" && mode != "counter_read" {
if readModeTweaks != 0 {
log.Fatal("in-restriction, no-lower-bound and provide-uppder-bound flags make sense only in read mode")
}
} else if readModeTweaks > 1 {
log.Fatal("in-restriction, no-lower-bound and provide-uppder-bound flags are mutually exclusive")
}
if workload == "timeseries" && mode == "read" && writeRate == 0 {
log.Fatal("write rate must be provided for time series reads loads")
}
if workload == "timeseries" && mode == "read" && startTimestamp == 0 {
log.Fatal("start timestamp must be provided for time series reads loads")
}
if workload == "timeseries" && mode == "write" && int64(concurrency) > partitionCount {
log.Fatal("time series writes require concurrency less than or equal partition count")
}
cluster := gocql.NewCluster(strings.Split(nodes, ",")...)
cluster.NumConns = connectionCount
cluster.PageSize = pageSize
cluster.Timeout = timeout
switch consistencyLevel {
case "any":
cluster.Consistency = gocql.Any
case "one":
cluster.Consistency = gocql.One
case "two":
cluster.Consistency = gocql.Two
case "three":
cluster.Consistency = gocql.Three
case "quorum":
cluster.Consistency = gocql.Quorum
case "all":
cluster.Consistency = gocql.All
case "local_quorum":
cluster.Consistency = gocql.LocalQuorum
case "each_quorum":
cluster.Consistency = gocql.EachQuorum
case "local_one":
cluster.Consistency = gocql.LocalOne
default:
log.Fatal("unknown consistency level: ", consistencyLevel)
}
if clientCompression {
cluster.Compressor = &gocql.SnappyCompressor{}
}
session, err := cluster.CreateSession()
if err != nil {
log.Fatal(err)
}
defer session.Close()
PrepareDatabase(session, replicationFactor)
interrupted := make(chan os.Signal, 1)
signal.Notify(interrupted, os.Interrupt)
go func() {
<-interrupted
fmt.Println("\ninterrupted")
atomic.StoreUint32(&stopAll, 1)
}()
if testDuration > 0 {
go func() {
time.Sleep(testDuration)
atomic.StoreUint32(&stopAll, 1)
}()
}
fmt.Println("Configuration")
fmt.Println("Mode:\t\t\t", mode)
fmt.Println("Workload:\t\t", workload)
fmt.Println("Timeout:\t\t", timeout)
fmt.Println("Consistency level:\t", consistencyLevel)
fmt.Println("Partition count:\t", partitionCount)
if workload == "sequential" && partitionOffset != 0 {
fmt.Println("Partition offset:\t", partitionOffset)
}
fmt.Println("Clustering rows:\t", clusteringRowCount)
fmt.Println("Clustering row size:\t", clusteringRowSize)
fmt.Println("Rows per request:\t", rowsPerRequest)
if mode == "read" {
fmt.Println("Provide upper bound:\t", provideUpperBound)
fmt.Println("IN queries:\t\t", inRestriction)
fmt.Println("No lower bound:\t\t", noLowerBound)
}
fmt.Println("Page size:\t\t", pageSize)
fmt.Println("Concurrency:\t\t", concurrency)
fmt.Println("Connections:\t\t", connectionCount)
if maximumRate > 0 {
fmt.Println("Maximum rate:\t\t", maximumRate, "op/s")
} else {
fmt.Println("Maximum rate:\t\t unlimited")
}
fmt.Println("Client compression:\t", clientCompression)
if workload == "timeseries" {
fmt.Println("Start timestamp:\t", startTime.UnixNano())
fmt.Println("Write rate:\t\t", int64(maximumRate)/partitionCount)
}
if startTimestamp != 0 {
startTime = time.Unix(0, startTimestamp)
} else {
startTime = time.Now()
}
if measureLatency {
fmt.Println("\ntime\t\toperations/s\trows/s\t\terrors\tmax\t\t99.9th\t\t99th\t\t95th\t\t90th\t\tmedian")
} else {
fmt.Println("\ntime\t\toperations/s\trows/s\t\terrors")
}
result := RunConcurrently(maximumRate, func(i int, resultChannel chan Result, rateLimiter RateLimiter) {
GetMode(mode)(session, resultChannel, GetWorkload(workload, i, partitionOffset, mode, writeRate, distribution), rateLimiter)
})
fmt.Println("\nResults")
fmt.Println("Time (avg):\t", result.Time)
fmt.Println("Total ops:\t", result.Operations)
fmt.Println("Total rows:\t", result.ClusteringRows)
if result.Errors != 0 {
fmt.Println("Total errors:\t", result.Errors)
}
fmt.Println("Operations/s:\t", result.OperationsPerSecond)
fmt.Println("Rows/s:\t\t", result.ClusteringRowsPerSecond)
if errorRecordingLatency {
fmt.Println("Latency measurements may be inaccurate")
}
if measureLatency {
fmt.Println("Latency:\n max:\t\t", time.Duration(result.Latency.Max()),
"\n 99.9th:\t", time.Duration(result.Latency.ValueAtQuantile(99.9)),
"\n 99th:\t\t", time.Duration(result.Latency.ValueAtQuantile(99)),
"\n 95th:\t\t", time.Duration(result.Latency.ValueAtQuantile(95)),
"\n 90th:\t\t", time.Duration(result.Latency.ValueAtQuantile(90)),
"\n median:\t", time.Duration(result.Latency.ValueAtQuantile(50)))
}
}