-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
571 lines (459 loc) · 10.8 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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
package main
import (
"database/sql"
"encoding/csv"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"math"
"os"
"sort"
"strings"
"sync"
"time"
_ "github.com/lib/pq"
)
type PostgresConfig struct {
Host string `json:"host"`
Port int `json:"port"`
User string `json:"user"`
Password string `json:"password"`
Database string `json:"db"`
SSL bool `json:"ssl"`
}
type Config struct {
File string `json:"-"`
WorkerCount int `json:"worker_count"`
Postgres PostgresConfig `json:"postgres"`
}
// read config file.
func readConfig(path string, config *Config) error {
f, err := os.Open(path)
if err != nil {
return fmt.Errorf("could not open file: %w", err)
}
b, err := ioutil.ReadAll(f)
if err != nil {
return fmt.Errorf("could not read file: %w", err)
}
err = json.Unmarshal(b, &config)
if err != nil {
return fmt.Errorf("could not parse json: %w", err)
}
err = f.Close()
if err != nil {
return fmt.Errorf("could not close file: %w", err)
}
return nil
}
const (
DefaultPostgresPort = 5432
DefaultWorkerCount = 5
)
// initialize config with defaults.
func initConfig() (*Config, error) {
config := Config{
WorkerCount: DefaultWorkerCount,
File: "",
Postgres: PostgresConfig{
Host: "localhost",
Port: DefaultPostgresPort,
SSL: false,
Database: "postgres",
User: "postgres",
Password: "",
},
}
var workerCount, port int
var confPath, host, user, password, db string
var ssl bool
flag.IntVar(&workerCount, "worker", config.WorkerCount, "worker count")
flag.StringVar(&host, "host", config.Postgres.Host, "database host")
flag.IntVar(&port, "port", config.Postgres.Port, "database port")
flag.StringVar(&user, "user", config.Postgres.User, "database user")
flag.StringVar(&password, "password", config.Postgres.Password, "database password")
flag.StringVar(&db, "db", config.Postgres.Database, "database schema name")
flag.BoolVar(&ssl, "ssl", config.Postgres.SSL, "database ssl mode")
flag.StringVar(&config.File, "file", "", "csv file input path for query parameters")
flag.StringVar(&confPath, "config", "", "custom config path")
flag.Parse()
// load user defined custom config file
if confPath != "" {
err := readConfig(confPath, &config)
if err != nil {
return nil, fmt.Errorf("invalid config %s, %w", confPath, err)
}
}
// provided flags always override configuration
flag.Visit(func(f *flag.Flag) {
switch f.Name {
case "worker":
config.WorkerCount = workerCount
case "host":
config.Postgres.Host = host
case "port":
config.Postgres.Port = port
case "db":
config.Postgres.Database = db
case "user":
config.Postgres.User = user
case "password":
config.Postgres.Password = password
case "ssl":
config.Postgres.SSL = ssl
}
})
return &config, nil
}
func connectDB(host, user, password, dbname string, port int, ssl bool) (*sql.DB, error) {
dsn := fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s",
host, port, user, password, dbname,
)
if !ssl {
dsn += " sslmode=disable"
}
db, err := sql.Open("postgres", dsn)
if err != nil {
return nil, fmt.Errorf("can't open db connection: %w", err)
}
err = db.Ping()
if err != nil {
return nil, fmt.Errorf("can't ping db: %w", err)
}
return db, nil
}
type QueryParams struct {
Host string
Start time.Time
End time.Time
}
const testQuery = `
SELECT
time_bucket('1 minute', ts) AS one_min,
AVG(usage),
MIN(usage),
MAX(usage)
FROM cpu_usage
WHERE host = $1
AND ts BETWEEN $2 AND $3
GROUP BY one_min
ORDER BY one_min DESC;
`
func runTestQuery(db *sql.DB, q QueryParams) error {
rows, err := db.Query(
testQuery,
q.Host,
q.Start,
q.End,
)
if err != nil {
return fmt.Errorf("failed to execute query: %w", err)
}
for rows.Next() {
if rows.Err() != nil {
return fmt.Errorf("failed to read rows: %w", err)
}
var oneMin time.Time
var avg float64
var max float64
var min float64
err = rows.Scan(&oneMin, &avg, &min, &max)
if err != nil {
return fmt.Errorf("failed to scan row: %w", err)
}
}
defer func() {
if err := rows.Close(); err != nil {
return
}
}()
return nil
}
type QueryResult struct {
Duration float64
Host string
Error error
}
// initialize workers and setup channels.
func startWorkers(workerCount int, work func(q QueryParams) error) ([]chan QueryParams, chan QueryResult) {
var workerChs []chan QueryParams
var workerWg sync.WaitGroup
result := make(chan QueryResult)
workerWg.Add(workerCount)
for i := 0; i < workerCount; i++ {
workerChs = append(workerChs, make(chan QueryParams))
go func(ch chan QueryParams) {
defer workerWg.Done()
for q := range ch {
now := time.Now()
err := work(q)
elapsed := time.Since(now)
result <- QueryResult{
Host: q.Host,
Duration: float64(elapsed.Milliseconds()),
Error: err,
}
}
}(workerChs[i])
}
// close result channel after every worker finishes
go func() {
workerWg.Wait()
close(result)
}()
return workerChs, result
}
const (
DateTimeLayout = "2006-01-02 15:04:05"
LineLength = 3
)
var (
errInvalidLineLen = errors.New("invalid line length")
errEmptyHostname = errors.New("empty host name")
errInvalidHeader = errors.New("invalid header")
)
func parseLine(line []string) (*QueryParams, error) {
if len(line) != LineLength {
return nil, fmt.Errorf("given line len: %d, %w", len(line), errInvalidLineLen)
}
hostname := line[0]
if hostname == "" {
return nil, errEmptyHostname
}
start, err := time.Parse(DateTimeLayout, line[1])
if err != nil {
return nil, fmt.Errorf("invalid start time value %v: %w", line[1], err)
}
end, err := time.Parse(DateTimeLayout, line[2])
if err != nil {
return nil, fmt.Errorf("invalid start time value %v: %w", line[2], err)
}
query := QueryParams{
Host: hostname,
Start: start,
End: end,
}
return &query, nil
}
const ExpectedHeaderStr = "hostname,start_time,end_time"
func validateHeader(line []string) error {
givenHeaderStr := strings.Join(line, ",")
if givenHeaderStr == ExpectedHeaderStr {
return nil
}
return fmt.Errorf("expected: %s but give: %s, %w", ExpectedHeaderStr, givenHeaderStr, errInvalidHeader)
}
func parseCsv(reader io.Reader, cb func(err error, params *QueryParams)) error {
csvReader := csv.NewReader(reader)
first, err := csvReader.Read()
if err != nil {
return fmt.Errorf("could not read header: %w", err)
}
err = validateHeader(first)
if err != nil {
return err
}
lineNum := 1
for {
record, err := csvReader.Read()
if errors.Is(err, io.EOF) {
break
}
lineNum++
if err != nil {
cb(err, nil)
continue
}
query, err := parseLine(record)
if err != nil {
cb(fmt.Errorf("record on line %d: %w", lineNum, err), nil)
continue
}
cb(nil, query)
}
return nil
}
func readCsv(file string) (io.Reader, error) {
if file == "" {
return os.Stdin, nil
}
reader, err := os.Open(file)
if err != nil {
return nil, fmt.Errorf("could not open csv file: %w", err)
}
return reader, nil
}
// parse input rows and assign to workers.
func processCsv(file string, errCh chan error, workerChs []chan QueryParams) (parseFailure int, err error) {
reader, err := readCsv(file)
if err != nil {
return
}
hostWorkerMap := map[string]int{}
workerIndex := 0
err = parseCsv(reader, func(err error, query *QueryParams) {
if err != nil {
errCh <- err
parseFailure++
return
}
w, ok := hostWorkerMap[query.Host]
if !ok {
w = workerIndex
hostWorkerMap[query.Host] = w
workerIndex = (workerIndex + 1) % len(workerChs)
}
ch := workerChs[w]
ch <- *query
})
return
}
const (
MaxPercentile = 100
MinPercentile = 0
MinPercentileDataLen = 2
)
func percentile(data []float64, p float64) float64 {
if p < MinPercentile {
return math.NaN()
}
if p > MaxPercentile {
return math.NaN()
}
n := float64(len(data))
if n < MinPercentileDataLen {
return math.NaN()
}
rank := (p/100)*(n-1) + 1
ri := float64(int64(rank))
rf := rank - ri
i := int(ri) - 1
return data[i] + rf*(data[i+1]-data[i])
}
type Stats struct {
ExecCount int
FailedCount int
Sum float64
Min float64
Max float64
Mean float64
Median float64
Percentile95th float64
Percentile99th float64
}
// listen to results and accumulate then print.
func collectResult(errCh chan error, result chan QueryResult, statCh chan Stats) {
durations := []float64{}
execCount := 0
sqlFailure := 0
min := math.Inf(1)
max := math.Inf(-1)
var sum float64 = 0
for r := range result {
if r.Error != nil {
errCh <- fmt.Errorf("sql err: %w", r.Error)
sqlFailure++
continue
}
execCount++
durations = append(durations, r.Duration)
min = math.Min(min, r.Duration)
max = math.Max(max, r.Duration)
sum += r.Duration
}
sort.Float64s(durations)
statCh <- Stats{
ExecCount: execCount,
FailedCount: sqlFailure,
Sum: sum,
Min: min,
Max: max,
Mean: sum / float64(execCount),
Median: percentile(durations, 50),
Percentile95th: percentile(durations, 95),
Percentile99th: percentile(durations, 99),
}
}
const resultMsg = `#summary:
total_count: %d
exec_count: %d
sql_failure: %d
parse_failure: %d
#durations:
total: %.2fms
min: %.2fms
max: %.2fms
mean: %.2fms
median: %.2fms
95th: %.2fms
99th: %.2fms
`
func formatResult(parseFailure int, stat Stats) string {
return fmt.Sprintf(
resultMsg,
stat.ExecCount+stat.FailedCount+parseFailure,
stat.ExecCount,
stat.FailedCount,
parseFailure,
stat.Sum,
stat.Min,
stat.Max,
stat.Mean,
stat.Median,
stat.Percentile95th,
stat.Percentile99th,
)
}
func main() {
errLogger := log.New(os.Stderr, "", log.Lmsgprefix)
infoLogger := log.New(os.Stdout, "", log.Lmsgprefix)
config, err := initConfig()
if err != nil {
errLogger.Fatalln(err)
}
db, err := connectDB(
config.Postgres.Host,
config.Postgres.User,
config.Postgres.Password,
config.Postgres.Database,
config.Postgres.Port,
config.Postgres.SSL,
)
if err != nil {
errLogger.Fatalln(err)
}
infoLogger.Println("db connection opened...")
defer func() {
if err = db.Close(); err != nil {
errLogger.Println(err)
}
}()
workerChs, result := startWorkers(config.WorkerCount, func(q QueryParams) error {
return runTestQuery(db, q)
})
errCh := make(chan error)
go func() {
for err := range errCh {
errLogger.Println(err)
}
}()
infoLogger.Println("workers started...")
statCh := make(chan Stats)
go collectResult(errCh, result, statCh)
parseFailure, err := processCsv(config.File, errCh, workerChs)
if err != nil {
errLogger.Fatalln(err)
}
for _, ch := range workerChs {
close(ch)
}
stat := <-statCh
close(errCh)
resultStr := formatResult(parseFailure, stat)
infoLogger.Print(resultStr)
}