-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
371 lines (327 loc) · 13.7 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
// bulk_query_gen generates queries for various use cases. Its output will
// be consumed by query_benchmarker.
package main
import (
"bufio"
"encoding/gob"
"flag"
"fmt"
"github.com/caict-benchmark/BDC-TS/bulk_data_gen/common"
bulkQueryGen "github.com/caict-benchmark/BDC-TS/bulk_query_gen"
"github.com/caict-benchmark/BDC-TS/bulk_query_gen/cassandra"
"github.com/caict-benchmark/BDC-TS/bulk_query_gen/elasticsearch"
"github.com/caict-benchmark/BDC-TS/bulk_query_gen/graphite"
"github.com/caict-benchmark/BDC-TS/bulk_query_gen/influxdb"
"github.com/caict-benchmark/BDC-TS/bulk_query_gen/mongodb"
"github.com/caict-benchmark/BDC-TS/bulk_query_gen/opentsdb"
"github.com/caict-benchmark/BDC-TS/bulk_query_gen/timescaledb"
"log"
"math/rand"
"os"
"sort"
"time"
)
const (
DevOpsOneHostOneHour = "1-host-1-hr"
DevOpsOneHostTwelveHours = "1-host-12-hr"
DevOpsEightHostsOneHour = "8-host-1-hr"
DevOpsGroupBy = "groupby"
IotOneHomeTwelveHours = "1-home-12-hours"
DashboardAll = "dashboard-all"
DashboardAvailability = "availability"
DashboardCpuNum = "cpu-num"
DashboardCpuUtilization = "cpu-utilization"
DashboardDiskAllocated = "disk-allocated"
DashboardDiskUsage = "disk-usage"
DashboardDiskUtilization = "disk-utilization"
DashboardHttpRequestDuration = "http-request-duration"
DashboardHttpRequests = "http-requests"
DashboardKapaCpu = "kapa-cpu"
DashboardKapaLoad = "kapa-load"
DashboardKapaRam = "kapa-ram"
DashboardMemoryTotal = "memory-total"
DashboardMemoryUtilization = "memory-utilization"
DashboardNginxRequests = "nginx-requests"
DashboardQueueBytes = "queue-bytes"
DashboardRedisMemoryUtilization = "redis-memory-utilization"
DashboardSystemLoad = "system-load"
DashboardThroughput = "throughput"
VehicleReadTime = "vehicle-real-time"
VehicleAverage = "vehicle-average"
)
// query generator choices {use-case, query-type, format}
// (This object is shown to the user when flag.Usage is called.)
var useCaseMatrix = map[string]map[string]map[string]bulkQueryGen.QueryGeneratorMaker{
common.UseCaseDevOps: {
DevOpsOneHostOneHour: {
"cassandra": cassandra.NewCassandraDevopsSingleHost,
"es-http": elasticsearch.NewElasticSearchDevopsSingleHost,
"influx-flux-http": influxdb.NewFluxDevopsSingleHost,
"influx-http": influxdb.NewInfluxQLDevopsSingleHost,
"mongo": mongodb.NewMongoDevopsSingleHost,
"opentsdb": opentsdb.NewOpenTSDBDevopsSingleHost,
"timescaledb": timescaledb.NewTimescaleDevopsSingleHost,
"graphite": graphite.NewGraphiteDevopsSingleHost,
},
DevOpsOneHostTwelveHours: {
"cassandra": cassandra.NewCassandraDevopsSingleHost12hr,
"es-http": elasticsearch.NewElasticSearchDevopsSingleHost12hr,
"influx-flux-http": influxdb.NewFluxDevopsSingleHost12hr,
"influx-http": influxdb.NewInfluxQLDevopsSingleHost12hr,
"mongo": mongodb.NewMongoDevopsSingleHost12hr,
"opentsdb": opentsdb.NewOpenTSDBDevopsSingleHost12hr,
"timescaledb": timescaledb.NewTimescaleDevopsSingleHost12hr,
"graphite": graphite.NewGraphiteDevopsSingleHost12hr,
},
DevOpsEightHostsOneHour: {
"cassandra": cassandra.NewCassandraDevops8Hosts,
"es-http": elasticsearch.NewElasticSearchDevops8Hosts,
"influx-flux-http": influxdb.NewFluxDevops8Hosts,
"influx-http": influxdb.NewInfluxQLDevops8Hosts,
"mongo": mongodb.NewMongoDevops8Hosts1Hr,
"opentsdb": opentsdb.NewOpenTSDBDevops8Hosts,
"timescaledb": timescaledb.NewTimescaleDevops8Hosts1Hr,
"graphite": graphite.NewGraphiteDevops8Hosts,
},
DevOpsGroupBy: {
"cassandra": cassandra.NewCassandraDevopsGroupBy,
"es-http": elasticsearch.NewElasticSearchDevopsGroupBy,
"influx-flux-http": influxdb.NewFluxDevopsGroupBy,
"influx-http": influxdb.NewInfluxQLDevopsGroupBy,
"timescaledb": timescaledb.NewTimescaleDevopsGroupby,
"graphite": graphite.NewGraphiteDevopsGroupBy,
},
},
common.UseCaseIot: {
IotOneHomeTwelveHours: {
"influx-flux-http": influxdb.NewFluxIotSingleHost,
"influx-http": influxdb.NewInfluxQLIotSingleHost,
"timescaledb": timescaledb.NewTimescaleIotSingleHost,
"cassandra": cassandra.NewCassandraIotSingleHost,
"mongo": mongodb.NewMongoIotSingleHost,
},
},
common.UseCaseDashboard: {
DashboardAll: {
"influx-http": influxdb.NewInfluxQLDashboardAll,
},
DashboardCpuNum: {
"influx-http": influxdb.NewInfluxQLDashboardCpuNum,
},
DashboardAvailability: {
"influx-http": influxdb.NewInfluxQLDashboardAvailability,
},
DashboardCpuUtilization: {"influx-http": influxdb.NewInfluxQLDashboardCpuUtilization},
DashboardDiskAllocated: {"influx-http": influxdb.NewInfluxQLDashboardDiskAllocated},
DashboardDiskUsage: {"influx-http": influxdb.NewInfluxQLDashboardDiskUsage},
DashboardDiskUtilization: {"influx-http": influxdb.NewInfluxQLDashboardDiskUtilization},
DashboardHttpRequestDuration: {"influx-http": influxdb.NewInfluxQLDashboardHttpRequestDuration},
DashboardHttpRequests: {"influx-http": influxdb.NewInfluxQLDashboardHttpRequests},
DashboardKapaCpu: {"influx-http": influxdb.NewInfluxQLDashboardKapaCpu},
DashboardKapaLoad: {"influx-http": influxdb.NewInfluxQLDashboardKapaLoad},
DashboardKapaRam: {"influx-http": influxdb.NewInfluxQLDashboardKapaRam},
DashboardMemoryTotal: {"influx-http": influxdb.NewInfluxQLDashboardMemoryTotal},
DashboardMemoryUtilization: {"influx-http": influxdb.NewInfluxQLDashboardMemoryUtilization},
DashboardNginxRequests: {"influx-http": influxdb.NewInfluxQLDashboardNginxRequests},
DashboardQueueBytes: {"influx-http": influxdb.NewInfluxQLDashboardQueueBytes},
DashboardRedisMemoryUtilization: {"influx-http": influxdb.NewInfluxQLDashboardRedisMemoryUtilization},
DashboardSystemLoad: {"influx-http": influxdb.NewInfluxQLDashboardSystemLoad},
DashboardThroughput: {"influx-http": influxdb.NewInfluxQLDashboardThroughput},
},
common.UseCaseVehicle: {
VehicleReadTime: {
"es-http": elasticsearch.NewElasticSearchVehicleRealTime,
},
},
}
// Program option vars:
var (
useCase string
queryType string
format string
documentFormat string
scaleVar int
queryCount int
dbName string // TODO(rw): make this a map[string]string -> DatabaseConfig
timestampStartStr string
timestampEndStr string
timestampStart time.Time
timestampEnd time.Time
queryInterval time.Duration
timeWindowShift time.Duration
queryIntervalType string
seed int64
debug int
interleavedGenerationGroupID uint
interleavedGenerationGroups uint
)
// Parse args:
func init() {
// Change the Usage function to print the use case matrix of choices:
oldUsage := flag.Usage
flag.Usage = func() {
oldUsage()
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "The use case matrix of choices is:\n")
for uc, queryTypes := range useCaseMatrix {
for qt, formats := range queryTypes {
for f := range formats {
fmt.Fprintf(os.Stderr, " use case: %s, query type: %s, format: %s\n", uc, qt, f)
}
}
}
}
flag.StringVar(&format, "format", "influx-http", "Format to emit. (Choices are in the use case matrix.)")
flag.StringVar(&documentFormat, "document-format", "", "Document format specification. (for mongo format 'simpleArrays'; leave empty for previous behaviour)")
flag.StringVar(&useCase, "use-case", common.UseCaseChoices[0], "Use case to model. (Choices are in the use case matrix.)")
flag.StringVar(&queryType, "query-type", "", "Query type. (Choices are in the use case matrix.)")
flag.IntVar(&scaleVar, "scale-var", 1, "Scaling variable (must be the equal to the scalevar used for data generation).")
flag.IntVar(&queryCount, "queries", 1000, "Number of queries to generate.")
flag.StringVar(&dbName, "db", "benchmark_db", "Database for influx to use (ignored for ElasticSearch).")
flag.StringVar(×tampStartStr, "timestamp-start", common.DefaultDateTimeStart, "Beginning timestamp (RFC3339).")
flag.StringVar(×tampEndStr, "timestamp-end", common.DefaultDateTimeEnd, "Ending timestamp (RFC3339).")
flag.DurationVar(&queryInterval, "query-interval", bulkQueryGen.DefaultQueryInterval, "Time interval query should ask for.")
flag.StringVar(&queryIntervalType, "query-interval-type", "window", "Interval type query { window - either random or shifted, last - interval is defined relative to now() }")
flag.DurationVar(&timeWindowShift, "time-window-shift", -1, "Sliding time window shift. (When set to > 0s, queries option is ignored - number of queries is calculated.")
flag.Int64Var(&seed, "seed", 0, "PRNG seed (default, or 0, uses the current timestamp).")
flag.IntVar(&debug, "debug", 0, "Debug printing (choices: 0, 1) (default 0).")
flag.UintVar(&interleavedGenerationGroupID, "interleaved-generation-group-id", 0, "Group (0-indexed) to perform round-robin serialization within. Use this to scale up data generation to multiple processes.")
flag.UintVar(&interleavedGenerationGroups, "interleaved-generation-groups", 1, "The number of round-robin serialization groups. Use this to scale up data generation to multiple processes.")
flag.Parse()
if queryType == DevOpsEightHostsOneHour && scaleVar < 8 {
log.Fatal("\"scale-var\" must be greater than the hosts grouping number")
}
if !(interleavedGenerationGroupID < interleavedGenerationGroups) {
log.Fatal("incorrect interleaved groups configuration")
}
if _, ok := useCaseMatrix[useCase]; !ok {
log.Fatal("invalid use case specifier")
}
if _, ok := useCaseMatrix[useCase][queryType]; !ok {
log.Fatal("invalid query type specifier")
}
if _, ok := useCaseMatrix[useCase][queryType][format]; !ok {
log.Fatal("invalid format specifier")
}
//hourGroupInterval := 1
//
//if queryType == DevOpsOneHostTwelveHours {
// hourGroupInterval = 12
//}
// Parse timestamps:
var err error
timestampStart, err = time.Parse(time.RFC3339, timestampStartStr)
if err != nil {
log.Fatal(err)
}
timestampStart = timestampStart.UTC()
timestampEnd, err = time.Parse(time.RFC3339, timestampEndStr)
if err != nil {
log.Fatal(err)
}
timestampEnd = timestampEnd.UTC()
duration := timestampEnd.Sub(timestampStart)
if duration.Nanoseconds() < 0 {
log.Fatal("\"timestamp-end\" must be grater than \"timestamp-start\"")
}
//if duration.Nanoseconds()/time.Hour.Nanoseconds() < int64(hourGroupInterval) {
// log.Fatal("Time interval must be greater than the grouping interval")
//}
//if duration.Nanoseconds() < queryInterval.Nanoseconds() {
// log.Fatal("Query interval must be greater than the grouping interval")
//}
bulkQueryGen.QueryIntervalType = queryIntervalType
switch queryIntervalType {
case "window":
if useCase == common.UseCaseDashboard && timeWindowShift <= 0 { // when not set, always use 5s default for dashboard
timeWindowShift = 5 * time.Second
}
case "last":
timeWindowShift = 0
default:
log.Fatalf("Unsupported query interval type: %s\n", queryIntervalType)
}
if timeWindowShift > 0 {
bulkQueryGen.TimeWindowShift = timeWindowShift // global
queryCount = int(timestampEnd.Sub(timestampStart).Seconds() / timeWindowShift.Seconds())
if queryType == DashboardAll {
queryCount *= 18
}
log.Printf("%v queries will be generated to cover time interval using %v shift", queryCount, timeWindowShift)
}
if format == "mongo" {
if documentFormat != mongodb.SimpleArraysFormat {
documentFormat = "default"
}
mongodb.DocumentFormat = documentFormat
log.Printf("Using '%s' mongo serialization", mongodb.DocumentFormat)
}
// the default seed is the current timestamp:
if seed == 0 {
seed = int64(time.Now().Nanosecond())
}
fmt.Fprintf(os.Stderr, "using random seed %d\n", seed)
}
func main() {
rand.Seed(seed)
dbConfig := bulkQueryGen.DatabaseConfig{
bulkQueryGen.DatabaseName: dbName,
}
// Make the query generator:
maker := useCaseMatrix[useCase][queryType][format]
interval := bulkQueryGen.NewTimeInterval(timestampStart, timestampEnd)
var generator = maker(dbConfig, interval, queryInterval, scaleVar)
// Set up bookkeeping:
stats := make(map[string]int64)
// Set up output buffering:
out := bufio.NewWriter(os.Stdout)
defer out.Flush()
// Create request instances, serializing them to stdout and collecting
// counts for each kind. If applicable, only prints queries that
// belong to this interleaved group id:
var currentInterleavedGroup uint = 0
enc := gob.NewEncoder(out)
for i := 0; i < queryCount; i++ {
q := generator.Dispatch(i)
if currentInterleavedGroup == interleavedGenerationGroupID {
err := enc.Encode(q)
if err != nil {
log.Fatal("encoder ", err)
}
stats[string(q.HumanLabelName())]++
if debug == 1 {
_, err := fmt.Fprintf(os.Stderr, "%s\n", q.HumanLabelName())
if err != nil {
log.Fatal(err)
}
} else if debug == 2 {
_, err := fmt.Fprintf(os.Stderr, "%s\n", q.HumanDescriptionName())
if err != nil {
log.Fatal(err)
}
} else if debug >= 3 {
_, err := fmt.Fprintf(os.Stderr, "%s\n", q.String())
if err != nil {
log.Fatal(err)
}
}
}
q.Release()
currentInterleavedGroup++
if currentInterleavedGroup == interleavedGenerationGroups {
currentInterleavedGroup = 0
}
}
// Print stats:
keys := []string{}
for k, _ := range stats {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
_, err := fmt.Fprintf(os.Stderr, "%s: %d points\n", k, stats[k])
if err != nil {
log.Fatal(err)
}
}
}