-
Notifications
You must be signed in to change notification settings - Fork 1
/
runner.go
560 lines (512 loc) · 14.4 KB
/
runner.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
/*
* Copyright [2020] Sergey Kudasov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package loadgen
import (
"context"
"flag"
"fmt"
"os"
"runtime"
"sync"
"time"
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/rcrowley/go-metrics"
"github.com/spf13/viper"
"go.uber.org/ratelimit"
)
// BeforeRunner can be implemented by an Attacker
// and its method is called before a test or Run.
type BeforeRunner interface {
BeforeRun(c RunnerConfig) error
}
// AfterRunner can be implemented by an Attacker
// and its method is called after a test or Run.
// The report is passed to compute the Failed field and/or store values in Output.
type AfterRunner interface {
AfterRun(r *RunReport) error
}
type RuntimeCheckFunc func(r *Runner) bool
const (
rampUp int = iota
constantLoad
)
// Default runner runtime check types
const (
prometheusCheckType = "prometheus"
errorRatioCheckType = "error"
)
type Runner struct {
name string
TestStage int
ReadCsvName string
WriteCsvName string
RecycleData bool
Manager *LoadManager
Config RunnerConfig
attackersMu *sync.Mutex
attackers []Attack
failed bool // if tests are failed for any reason
running bool
shutDownOnce *sync.Once
stopped bool // if tests are stopped by hook
next, quit, stop chan bool
results chan result
prototype Attack
resultsPipeline func(r result) result
// Checks whether to stop generator
checkFunc RuntimeCheckFunc
CheckData []Checks
// Other clients for checks
PromClient v1.API
// Metrics
registeredMetricsLabels []string
RateLog []float64
MaxRPS float64
// RampUpMetrics store only rampup interval metrics, cleared every interval
RampUpMetrics map[string]*Metrics
// Metrics store full attack metrics
Metrics map[string]*Metrics
timerMu *sync.RWMutex
timers map[string]metrics.Timer
errorsMu *sync.RWMutex
Errors map[string]metrics.Counter
goroutinesCountGaugue metrics.Gauge
goroutinesCount int64
L *Logger
}
func NewRunner(name string, lm *LoadManager, a Attack, ch RuntimeCheckFunc, c RunnerConfig) *Runner {
var promClient v1.API
if lm.GeneratorConfig.Prometheus != nil {
promC, err := api.NewClient(api.Config{
Address: lm.GeneratorConfig.Prometheus.URL,
})
if err != nil {
log.Fatalf("failed to setup prometheus client: %s", err)
}
promClient = v1.NewAPI(promC)
}
r := &Runner{
name: name,
Manager: lm,
Config: c,
prototype: a,
checkFunc: ch,
CheckData: c.StopIf,
PromClient: promClient,
RateLog: []float64{},
shutDownOnce: &sync.Once{},
next: make(chan bool),
quit: make(chan bool),
stop: make(chan bool),
results: make(chan result),
attackersMu: &sync.Mutex{},
attackers: []Attack{},
registeredMetricsLabels: make([]string, 0),
RampUpMetrics: make(map[string]*Metrics),
Metrics: make(map[string]*Metrics),
timerMu: &sync.RWMutex{},
timers: make(map[string]metrics.Timer),
errorsMu: &sync.RWMutex{},
Errors: make(map[string]metrics.Counter),
goroutinesCount: 0,
goroutinesCountGaugue: metrics.NewGauge(),
L: &Logger{log.With("runner", name)},
}
r.L.Infof("bootstraping generator")
r.L.Infof("[%d] available logical CPUs", runtime.NumCPU())
// validate the configuration
if msg := c.Validate(); len(msg) > 0 {
for _, each := range msg {
fmt.Println("a configuration error was found", each)
}
fmt.Println()
flag.Usage()
os.Exit(0)
}
// is the attacker interested in the Run lifecycle?
if lifecycler, ok := a.(BeforeRunner); ok {
if err := lifecycler.BeforeRun(c); err != nil {
log.Fatalf("BeforeRun failed: %s", err)
}
}
// do a test if the flag says so
if *oSample > 0 {
r.test(*oSample)
report := RunReport{}
if lifecycler, ok := a.(AfterRunner); ok {
if err := lifecycler.AfterRun(&report); err != nil {
log.Fatalf("AfterRun failed: %s", err)
}
}
os.Exit(0)
// unreachable
return r
}
return r
}
func (r *Runner) initPipeline() {
r.resultsPipeline = r.addResult
}
func (r *Runner) spawnAttacker() {
if r.Config.Verbose {
r.L.Debugf("setup and spawn new attacker [%d]", len(r.attackers)+1)
}
attacker := r.prototype.Clone(r)
if err := attacker.Setup(r.Config); err != nil {
r.L.Infof("attacker [%d] setup failed with [%v]", len(r.attackers)+1, err)
return
}
r.attackersMu.Lock()
defer r.attackersMu.Unlock()
r.attackers = append(r.attackers, attacker)
go attack(attacker, r.next, r.quit, r.results, r.Config.timeout())
}
// addResult is called from a dedicated goroutine.
func (r *Runner) addResult(s result) result {
m, ok := r.Metrics[s.doResult.RequestLabel]
if !ok {
m = new(Metrics)
r.Metrics[s.doResult.RequestLabel] = m
}
m.add(s)
return s
}
// test uses the Attack to perform {count} calls and report its result
// it is intended for development of an Attack implementation.
func (r *Runner) test(count int) {
probe := r.prototype.Clone(r)
if err := probe.Setup(r.Config); err != nil {
log.Infof("test attack setup failed [%v]", err)
return
}
defer probe.Teardown()
for s := count; s > 0; s-- {
now := time.Now()
result := probe.Do(context.Background())
log.Infof("test attack call [%s] took [%v] with status [%v] and error [%v]", result.RequestLabel, time.Now().Sub(now), result.StatusCode, result.Error)
}
}
func (r *Runner) SetupHandleStore(m *LoadManager) {
csvReadName := r.Config.ReadFromCsvName
recycleData := r.Config.RecycleData
if csvReadName != "" {
log.Infof("creating read file: %s", csvReadName)
f, err := os.Open(csvReadName)
if err != nil {
log.Fatalf("no csv read file found: %s", csvReadName)
}
m.CsvStore[csvReadName] = NewCSVData(f, recycleData)
}
csvWriteName := r.Config.WriteToCsvName
if csvWriteName != "" {
log.Infof("creating write file: %s", csvWriteName)
csvFile := CreateOrReplaceFile(csvWriteName)
m.CsvStore[csvWriteName] = NewCSVData(csvFile, false)
}
}
// defaultCheckByData setups default prometheus or error ration check func
func (r *Runner) defaultCheckByData() {
if r.checkFunc != nil {
r.L.Info("custom check selected, see code in checks.go")
return
}
if r.CheckData != nil {
switch r.CheckData[0].Type {
case prometheusCheckType:
r.L.Infof("default prometheus check selected, query: %s", r.CheckData[0].Query)
r.checkFunc = func(r *Runner) bool {
return PromBooleanQuery(r)
}
return
case errorRatioCheckType:
r.L.Infof("default error check selected, threshold: %.2f perc errors", r.CheckData[0].Threshold)
r.checkFunc = func(r *Runner) bool {
return ErrorPercentCheck(r, r.CheckData[0].Threshold)
}
return
default:
r.L.Infof("unknown check type selected, skipping runner runtime check")
return
}
}
r.L.Info("no default check found")
}
func (r *Runner) init() {
r.shutDownOnce = &sync.Once{}
r.attackers = make([]Attack, 0)
r.failed = false
r.stopped = false
r.collectResults()
r.initMonitoring()
}
// Run offers the complete flow of a test.
func (r *Runner) Run(wg *sync.WaitGroup, lm *LoadManager) {
r.init()
r.resultsPipeline = r.addResult
if wg != nil {
defer wg.Done()
}
if lifecycler, ok := r.prototype.(BeforeRunner); ok {
if err := lifecycler.BeforeRun(r.Config); err != nil {
r.L.Infof("BeforeRun failed", err)
}
}
if r.Config.WaitBeforeSec != 0 {
r.L.Infof("awaiting runner start, sleeping for %d sec", r.Config.WaitBeforeSec)
time.Sleep(time.Duration(r.Config.WaitBeforeSec) * time.Second)
}
r.defaultCheckByData()
r.checkStopIf()
r.running = true
if r.rampUp() {
r.fullAttack()
}
r.Shutdown()
r.ReportMaxRPS()
report := RunReport{}
if lifecycler, ok := r.prototype.(AfterRunner); ok {
if err := lifecycler.AfterRun(&report); err != nil {
r.L.Infof("AfterRun failed", err)
}
}
lm.CsvMu.Lock()
defer lm.CsvMu.Unlock()
rep := r.reportMetrics()
lm.Reports[r.name] = rep
}
func (r *Runner) SetValidationParams() {
r.RateLog = []float64{}
r.Config.IsValidationRun = true
r.Config.AttackTimeSec = r.Config.Validation.AttackTimeSec
r.Config.RampUpTimeSec = 1
r.Config.StoreData = false
rpsWithNoErrors := int(r.Config.Validation.Threshold * r.MaxRPS)
if rpsWithNoErrors == 0 {
rpsWithNoErrors = 1
}
r.Config.RPS = rpsWithNoErrors
r.L.Infof("running validation of max rps: %d for %d seconds", r.Config.RPS, r.Config.AttackTimeSec)
}
func (r *Runner) initMonitoring() {
url := viper.GetString("graphite.url")
if url == "" {
return
}
flushDuration := time.Duration(viper.GetInt("graphite.flushDurationSec"))
loadGeneratorPrefix := viper.GetString("graphite.loadGeneratorPrefix")
StartGraphiteSender(loadGeneratorPrefix, flushDuration, url)
r.registerMetric("goroutines-"+r.name, r.goroutinesCountGaugue)
}
func (r *Runner) registerLabelTimings(label string) metrics.Timer {
r.timerMu.RLock()
timer, ok := r.timers[label]
r.timerMu.RUnlock()
if ok {
return timer
}
r.timerMu.Lock()
defer r.timerMu.Unlock()
timer = metrics.NewTimer()
r.timers[label] = timer
r.registerMetric(label+"-timer", timer)
return timer
}
func (r *Runner) registerErrCount(label string) metrics.Counter {
r.errorsMu.RLock()
cnt, ok := r.Errors[label]
r.errorsMu.RUnlock()
if ok {
return cnt
}
r.errorsMu.Lock()
defer r.errorsMu.Unlock()
cnt = metrics.NewCounter()
r.Errors[label] = cnt
r.registerMetric(label+"-err", cnt)
return cnt
}
func (r *Runner) registerMetric(name string, metric interface{}) {
r.registeredMetricsLabels = append(r.registeredMetricsLabels, name)
if err := metrics.Register(name, metric); err != nil {
log.Infof("failed to register metric: %s", err)
}
}
func (r *Runner) updateMetrics() {
go func() {
interval := 1 * time.Second
for {
time.Sleep(interval)
if r.stopped {
return
}
if r.Metrics == nil {
continue
}
if _, ok := r.Metrics[r.name]; !ok {
continue
}
r.Metrics[r.name].updateLatencies()
r.Metrics[r.name].updateSuccessRatio()
r.L.Infof("rate [%4f], mean response [%v], # requests [%d], # attackers [%d], %% success [%d]",
r.Metrics[r.name].Rate, r.Metrics[r.name].meanLogEntry(), r.Metrics[r.name].Requests, len(r.attackers), r.Metrics[r.name].successLogEntry())
}
}()
}
func (r *Runner) fullAttack() {
r.TestStage = constantLoad
r.L.Infof("begin full attack of [%d] remaining seconds", r.Config.AttackTimeSec-r.Config.RampUpTimeSec)
fullAttackStartedAt = time.Now()
limiter := ratelimit.New(r.Config.RPS)
doneDeadline := time.Now().Add(time.Duration(r.Config.AttackTimeSec-r.Config.RampUpTimeSec) * time.Second)
r.updateMetrics()
for time.Now().Before(doneDeadline) {
select {
case <-r.stop:
r.L.Infof("full attack stopped")
return
default:
limiter.Take()
if !r.stopped {
r.next <- true
}
}
}
r.L.Info("end full attack")
}
func (r *Runner) rampUp() bool {
r.TestStage = rampUp
strategy := r.Config.rampupStrategy()
if r.Config.Verbose {
r.L.Infof("begin rampup of [%d] seconds to RPS [%d] within attack of [%d] seconds using strategy [%s]",
r.Config.RampUpTimeSec,
r.Config.RPS,
r.Config.AttackTimeSec,
strategy,
)
}
var finished bool
switch strategy {
case "linear":
finished = linearIncreasingGoroutinesAndRequestsPerSecondStrategy{}.execute(r)
case "exp2":
finished = spawnAsWeNeedStrategy{}.execute(r)
}
// restore pipeline function in case it was changed by the rampup strategy
r.resultsPipeline = r.addResult
if r.Config.Verbose {
r.L.Infof("end rampup ending up with [%d] attackers", len(r.attackers))
}
return finished
}
func (r *Runner) tearDownAttackers() {
r.attackersMu.Lock()
defer r.attackersMu.Unlock()
if r.Config.Verbose {
log.Infof("stopping attackers [%d]", len(r.attackers))
}
for range r.attackers {
r.quit <- true
}
if r.Config.Verbose {
r.L.Infof("tearing down attackers [%d]", len(r.attackers))
}
for i, each := range r.attackers {
if err := each.Teardown(); err != nil {
r.L.Infof("failed to teardown attacker [%d]:%v", i, err)
}
}
}
func (r *Runner) unregisterMetrics() {
for _, m := range r.registeredMetricsLabels {
metrics.Unregister(m)
}
}
func (r *Runner) reportMetrics() *RunReport {
for _, each := range r.Metrics {
each.updateLatencies()
}
return &RunReport{
StartedAt: fullAttackStartedAt,
FinishedAt: time.Now(),
Configuration: r.Config,
Metrics: r.Metrics,
Failed: false, // must be overwritten by program
Output: map[string]interface{}{},
}
}
func (r *Runner) collectResults() {
go func() {
for {
r.resultsPipeline(<-r.results)
}
}()
}
func (r *Runner) ReportMaxRPS() {
r.MaxRPS = MaxRPS(r.RateLog)
r.L.Infof("max rps: %.2f", r.MaxRPS)
if r.Config.IsValidationRun && !r.failed {
entry := []string{r.name, os.Getenv("NETWORK_NODES"), fmt.Sprintf("%.2f", r.MaxRPS)}
r.L.Infof("writing scaling info: %s", entry)
if err := r.Manager.RPSScalingLog.Write(entry); err != nil {
r.L.Fatal(err)
}
}
}
func (r *Runner) Shutdown() {
if r.running {
r.shutDownOnce.Do(func() {
r.L.Infof("test ended, shutting down runner")
r.running = false
r.stop <- true
r.stopped = true
r.checkFunc = nil
r.tearDownAttackers()
r.unregisterMetrics()
r.L.Infof("runner shutdown complete")
})
}
}
// checkStopIf executing check function, shutdown if it returns true
func (r *Runner) checkStopIf() {
if r.checkFunc == nil {
return
}
go func() {
for {
select {
case <-r.stop:
return
default:
checkTime := time.Duration(r.CheckData[0].Interval)
time.Sleep(checkTime * time.Second)
if r.checkFunc == nil {
return
}
if r.checkFunc(r) {
r.L.Infof("runtime check failed, exiting")
r.failed = true
r.Manager.Failed = true
if r.Config.IsValidationRun {
r.Manager.ValidationFailed = true
}
r.Shutdown()
return
}
}
}
}()
}