-
Notifications
You must be signed in to change notification settings - Fork 2
/
influxdb_stress.go
369 lines (319 loc) · 9.55 KB
/
influxdb_stress.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"math/rand"
"strings"
"sync"
"text/template"
"time"
influx "github.com/influxdb/influxdb/client"
flag "launchpad.net/gnuflag"
)
type Config struct {
Address string
DBName string
User string
Pwd string
Writers int
WriterSeriesCnt int
WriterSeriesBaseName string
WriterBatchSize int
Readers int
QueriesFile string
Queries []string
Interval int
ResetDB bool
Help bool
}
const (
defaultAddress = "localhost:8086"
defaultDatabase = "stress_test"
defaultUser = "root"
defaultPwd = "root"
defaultWriters = 10
defaultWriterSeriesCnt = 10
defaultWriterSeriesBaseName = "stress"
defaultWriterBatchSize = 10
defaultReaders = 10
defaultQueriesFile = ""
defaultInterval = 1000
)
var cfg *Config
func main() {
cfg = getConfig()
if cfg.Help {
flag.Usage()
return
}
if cfg.WriterSeriesCnt < cfg.WriterBatchSize {
cfg.WriterSeriesCnt = cfg.WriterBatchSize
}
if cfg.QueriesFile != "" {
var err error
cfg.Queries, err = loadTextFile(cfg.QueriesFile)
fatalIfErr(err)
} else {
cfg.Queries = queryTemplates
}
rand.Seed(time.Now().UnixNano())
if cfg.ResetDB {
err := deleteDB(cfg)
fatalIfErr(err)
}
err := createDB(cfg)
fatalIfErr(err)
allClientsDone := startClients(cfg)
<-allClientsDone
}
func startClients(cfg *Config) chan struct{} {
done := make(chan struct{})
go func() {
var wg sync.WaitGroup
log.Printf("Starting %d writers (randomly delayed)\n", cfg.Writers)
for i := 0; i < cfg.Writers; i++ {
wg.Add(1)
name := fmt.Sprintf("writer%d", i)
dbw := NewDBWriter(name, cfg, &wg)
go dbw.Run()
}
log.Printf("Starting %d readers (randomly delayed)\n", cfg.Readers)
for i := 0; i < cfg.Readers; i++ {
wg.Add(1)
name := fmt.Sprintf("reader%d", i)
dbr := NewDBReader(name, cfg, &wg)
go dbr.Run()
}
wg.Wait()
close(done)
}()
return done
}
func createDB(cfg *Config) error {
clientCfg := &influx.ClientConfig{Host: cfg.Address, Username: cfg.User, Password: cfg.Pwd}
client, err := influx.NewClient(clientCfg)
if err != nil {
return err
}
dblist, err := client.GetDatabaseList()
if err != nil {
return err
}
if !dbExists(cfg.DBName, dblist) {
log.Printf("Creating database: %s\n", cfg.DBName)
err = client.CreateDatabase(cfg.DBName)
} else {
log.Printf("Database already exists: %s\n", cfg.DBName)
}
return err
}
func deleteDB(cfg *Config) error {
clientCfg := &influx.ClientConfig{Host: cfg.Address, Username: cfg.User, Password: cfg.Pwd}
client, err := influx.NewClient(clientCfg)
if err != nil {
return err
}
dblist, err := client.GetDatabaseList()
if err != nil {
return err
}
if dbExists(cfg.DBName, dblist) {
log.Printf("Deleting old database: %s\n", cfg.DBName)
err = client.DeleteDatabase(cfg.DBName)
if err != nil {
return err
}
}
return nil
}
type DBWriter struct {
Name string
cfg *Config
wg *sync.WaitGroup
}
func NewDBWriter(name string, c *Config, wg *sync.WaitGroup) *DBWriter {
dbw := &DBWriter{name, c, wg}
return dbw
}
func (self *DBWriter) Run() {
defer self.wg.Done()
WriterSeriesCnt := self.cfg.WriterSeriesCnt
batchSz := self.cfg.WriterBatchSize
batchInterval := time.Duration(self.cfg.Interval)
// Wait a pseudo-random amount of time before starting so that not all
// clients start writing to the DB at the same time.
startDelay := time.Duration(rand.Intn(30000))
<-time.After(startDelay * time.Millisecond)
log.Printf("%s: started\n", self.Name)
defer log.Printf("%s: stoped\n", self.Name)
// Connect to the db
cfg := &influx.ClientConfig{Host: self.cfg.Address, Username: self.cfg.User, Password: self.cfg.Pwd, Database: self.cfg.DBName}
client, err := influx.NewClient(cfg)
fatalIfErr(err)
// Generate array of series which we will periodically update and
// send batches from.
series := make([]*influx.Series, WriterSeriesCnt)
for i := 0; i < WriterSeriesCnt; i++ {
series[i] = &influx.Series{
Name: fmt.Sprintf("%s.%s.series%d", self.cfg.WriterSeriesBaseName, self.Name, i),
Columns: []string{"value"},
Points: [][]interface{}{},
}
}
// Main client loop
i := 0
batch := make([]*influx.Series, batchSz)
for {
// Select the next batch from series array
for j := 0; j < batchSz; j++ {
s := series[i%batchSz]
// generate new value
genNewValue(s)
batch[j] = s
i++
}
// Write the batch to InfluxDB
err = client.WriteSeries(batch)
fatalIfErr(err)
log.Printf("%s wrote a batch of %d series\n", self.Name, batchSz)
// Wait till time to send the next batch
<-time.After(batchInterval * time.Millisecond)
}
}
func genNewValue(s *influx.Series) {
newVal := []interface{}{rand.Int()}
s.Points = [][]interface{}{newVal}
}
type DBReader struct {
Name string
cfg *Config
wg *sync.WaitGroup
}
func NewDBReader(name string, c *Config, wg *sync.WaitGroup) *DBReader {
dbr := &DBReader{name, c, wg}
return dbr
}
func (self *DBReader) Run() {
defer self.wg.Done()
// Wait a pseudo-random amount of time before starting so that not all
// clients read the DB at the same time.
startDelay := time.Duration(rand.Intn(30000))
<-time.After(startDelay * time.Millisecond)
log.Printf("%s: started\n", self.Name)
defer log.Printf("%s: stoped\n", self.Name)
// Connect to the db
cfg := &influx.ClientConfig{Host: self.cfg.Address, Username: self.cfg.User, Password: self.cfg.Pwd, Database: self.cfg.DBName}
client, err := influx.NewClient(cfg)
fatalIfErr(err)
queries := self.cfg.Queries
// Main read loop
readInterval := time.Duration(self.cfg.Interval)
for {
tmplTxt := queries[rand.Intn(len(queries))]
tmpl := template.Must(template.New("query").Funcs(queryTemplateFuncs).Parse(tmplTxt))
var query bytes.Buffer
err = tmpl.Execute(&query, nil)
fatalIfErr(err)
log.Printf("query = %s\n", query.String())
_, err := client.Query(query.String())
logIfErr(err)
<-time.After(readInterval * time.Millisecond)
}
}
func randSeries() string {
w := rand.Intn(cfg.Writers)
s := rand.Intn(cfg.WriterSeriesCnt)
series := fmt.Sprintf("%s.writer%d.series%d", cfg.WriterSeriesBaseName, w, s)
return series
}
var aggregates = []string{
"count(value)",
"min(value)",
"max(value)",
"mean(value)",
"mode(value)",
"median(value)",
"distinct(value)",
"percentile(value, 10)",
"histogram(value)",
"histogram(value, 10)",
"derivative(value)",
"sum(value)",
"stddev(value)",
"first(value)",
"last(value)",
"difference(value)",
"top(value, 10)",
"bottom(value, 10)",
}
func randAggregate() string {
return aggregates[rand.Intn(len(aggregates))]
}
var queryTemplateFuncs = template.FuncMap{
"randSeries": randSeries,
"randAggregate": randAggregate,
}
var queryTemplates = []string{
"list series",
"select * from {{randSeries}}",
"select {{randAggregate}} from {{randSeries}}",
"select derivative(value) from {{randSeries}} group by time(20s)",
}
func dbExists(dbname string, dbList []map[string]interface{}) bool {
for _, db := range dbList {
if dbname == db["name"] {
return true
}
}
return false
}
func fatalIfErr(err error) {
if err != nil {
log.Fatalln(err)
}
}
func logIfErr(err error) {
if err != nil {
log.Println(err)
}
}
func loadTextFile(path string) ([]string, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
lines := strings.Split(string(b), "\n")
return lines, nil
}
func getConfig() *Config {
var cfg Config
flag.StringVar(&cfg.Address, "a", defaultAddress, "IP:port of InfluxDB")
flag.StringVar(&cfg.Address, "address", defaultAddress, "IP:port of InfluxDB")
flag.StringVar(&cfg.DBName, "d", defaultDatabase, "influxDB database name")
flag.StringVar(&cfg.DBName, "database", defaultDatabase, "influxDB database name")
flag.StringVar(&cfg.User, "u", defaultUser, "influxDB user")
flag.StringVar(&cfg.User, "user", defaultUser, "influxDB user")
flag.StringVar(&cfg.Pwd, "p", defaultPwd, "influxDB password")
flag.StringVar(&cfg.Pwd, "password", defaultPwd, "influxDB password")
flag.IntVar(&cfg.Writers, "w", defaultWriters, "number of writers to create")
flag.IntVar(&cfg.Writers, "writers", defaultWriters, "number of writers to create")
flag.IntVar(&cfg.WriterSeriesCnt, "s", defaultWriterSeriesCnt, "number of series to generate per writer")
flag.IntVar(&cfg.WriterSeriesCnt, "series-cnt", defaultWriterSeriesCnt, "number of series to generate per writer")
flag.StringVar(&cfg.WriterSeriesBaseName, "n", defaultWriterSeriesBaseName, "series base name")
flag.StringVar(&cfg.WriterSeriesBaseName, "series-name", defaultWriterSeriesBaseName, "series base name")
flag.IntVar(&cfg.WriterBatchSize, "b", defaultWriterBatchSize, "number of series to write per interval")
flag.IntVar(&cfg.WriterBatchSize, "batch-size", defaultWriterBatchSize, "number of series to write per interval")
flag.IntVar(&cfg.Readers, "r", defaultReaders, "number of readers to create")
flag.IntVar(&cfg.Readers, "readers", defaultReaders, "number of readers to create")
flag.StringVar(&cfg.QueriesFile, "q", defaultQueriesFile, "path to file containing query templates")
flag.StringVar(&cfg.QueriesFile, "queries-file", defaultQueriesFile, "path to file containing query templates")
flag.IntVar(&cfg.Interval, "i", defaultInterval, "milliseconds between writing batches")
flag.IntVar(&cfg.Interval, "interval", defaultInterval, "milliseconds between writing batches")
flag.BoolVar(&cfg.ResetDB, "reset-db", false, "drops & creates new database before starting")
flag.BoolVar(&cfg.Help, "h", false, "show usage")
flag.BoolVar(&cfg.Help, "help", false, "show usage")
flag.Parse(false)
return &cfg
}