-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
engine.go
448 lines (386 loc) · 12.3 KB
/
engine.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
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2016 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package core
import (
"context"
"errors"
"strings"
"sync"
"time"
"github.com/sirupsen/logrus"
"gopkg.in/guregu/null.v3"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/metrics"
"github.com/loadimpact/k6/stats"
)
const (
metricsRate = 1 * time.Second
collectRate = 50 * time.Millisecond
thresholdsRate = 2 * time.Second
)
// The Engine is the beating heart of k6.
type Engine struct {
// TODO: Make most of the stuff here private! And think how to refactor the
// engine to be less stateful... it's currently one big mess of moving
// pieces, and you implicitly first have to call Init() and then Run() -
// maybe we should refactor it so we have a `Session` dauther-object that
// Init() returns? The only problem with doing this is the REST API - it
// expects to be able to get information from the Engine and is initialized
// before the Init() call...
ExecutionScheduler lib.ExecutionScheduler
executionState *lib.ExecutionState
Options lib.Options
Collectors []lib.Collector
NoThresholds bool
NoSummary bool
SummaryExport bool
logger *logrus.Entry
stopOnce sync.Once
stopChan chan struct{}
Metrics map[string]*stats.Metric
MetricsLock sync.Mutex
Samples chan stats.SampleContainer
// Assigned to metrics upon first received sample.
thresholds map[string]stats.Thresholds
submetrics map[string][]*stats.Submetric
// Are thresholds tainted?
thresholdsTainted bool
}
// NewEngine instantiates a new Engine, without doing any heavy initialization.
func NewEngine(ex lib.ExecutionScheduler, o lib.Options, logger *logrus.Logger) (*Engine, error) {
if ex == nil {
return nil, errors.New("missing ExecutionScheduler instance")
}
e := &Engine{
ExecutionScheduler: ex,
executionState: ex.GetState(),
Options: o,
Metrics: make(map[string]*stats.Metric),
Samples: make(chan stats.SampleContainer, o.MetricSamplesBufferSize.Int64),
stopChan: make(chan struct{}),
logger: logger.WithField("component", "engine"),
}
e.thresholds = o.Thresholds
e.submetrics = make(map[string][]*stats.Submetric)
for name := range e.thresholds {
if !strings.Contains(name, "{") {
continue
}
parent, sm := stats.NewSubmetric(name)
e.submetrics[parent] = append(e.submetrics[parent], sm)
}
return e, nil
}
// Init is used to initialize the execution scheduler and all metrics processing
// in the engine. The first is a costly operation, since it initializes all of
// the planned VUs and could potentially take a long time. It either returns an
// error immediately, or it returns test run() and wait() functions.
//
// Things to note:
// - The first lambda, Run(), synchronously executes the actual load test.
// - It can be prematurely aborted by cancelling the runCtx - this won't stop
// the metrics collection by the Engine.
// - Stopping the metrics collection can be done at any time after Run() has
// returned by cancelling the globalCtx
// - The second returned lambda can be used to wait for that process to finish.
func (e *Engine) Init(globalCtx, runCtx context.Context) (run func() error, wait func(), err error) {
e.logger.Debug("Initialization starting...")
// TODO: if we ever need metrics processing in the init context, we can move
// this below the other components... or even start them concurrently?
if err := e.ExecutionScheduler.Init(runCtx, e.Samples); err != nil {
return nil, nil, err
}
// TODO: move all of this in a separate struct? see main TODO above
runSubCtx, runSubCancel := context.WithCancel(runCtx)
resultCh := make(chan error)
processMetricsAfterRun := make(chan struct{})
runFn := func() error {
e.logger.Debug("Execution scheduler starting...")
err := e.ExecutionScheduler.Run(globalCtx, runSubCtx, e.Samples)
e.logger.WithError(err).Debug("Execution scheduler terminated")
select {
case <-runSubCtx.Done():
// do nothing, the test run was aborted somehow
default:
resultCh <- err // we finished normally, so send the result
}
// Make the background jobs process the currently buffered metrics and
// run the thresholds, then wait for that to be done.
processMetricsAfterRun <- struct{}{}
<-processMetricsAfterRun
return err
}
waitFn := e.startBackgroundProcesses(globalCtx, runCtx, resultCh, runSubCancel, processMetricsAfterRun)
return runFn, waitFn, nil
}
// This starts a bunch of goroutines to process metrics, thresholds, and set the
// test run status when it ends. It returns a function that can be used after
// the provided context is called, to wait for the complete winding down of all
// started goroutines.
func (e *Engine) startBackgroundProcesses( //nolint:funlen
globalCtx, runCtx context.Context, runResult <-chan error, runSubCancel func(), processMetricsAfterRun chan struct{},
) (wait func()) {
processes := new(sync.WaitGroup)
// Spin up all configured collectors
for _, collector := range e.Collectors {
processes.Add(1)
go func(collector lib.Collector) {
collector.Run(globalCtx)
processes.Done()
}(collector)
}
// Siphon and handle all produced metric samples
processes.Add(1)
go func() {
defer processes.Done()
e.processMetrics(globalCtx, processMetricsAfterRun)
}()
// Run VU metrics emission, only while the test is running.
// TODO: move? this seems like something the ExecutionScheduler should emit...
processes.Add(1)
go func() {
defer processes.Done()
e.logger.Debug("Starting emission of VU metrics...")
e.runMetricsEmission(runCtx)
e.logger.Debug("Metrics emission terminated")
}()
// Update the test run status when the test finishes
processes.Add(1)
thresholdAbortChan := make(chan struct{})
go func() {
defer processes.Done()
select {
case err := <-runResult:
if err != nil {
e.logger.WithError(err).Debug("run: execution scheduler returned an error")
e.setRunStatus(lib.RunStatusAbortedSystem)
} else {
e.logger.Debug("run: execution scheduler terminated")
e.setRunStatus(lib.RunStatusFinished)
}
case <-runCtx.Done():
e.logger.Debug("run: context expired; exiting...")
e.setRunStatus(lib.RunStatusAbortedUser)
case <-e.stopChan:
runSubCancel()
e.logger.Debug("run: stopped by user; exiting...")
e.setRunStatus(lib.RunStatusAbortedUser)
case <-thresholdAbortChan:
e.logger.Debug("run: stopped by thresholds; exiting...")
runSubCancel()
e.setRunStatus(lib.RunStatusAbortedThreshold)
}
}()
// Run thresholds, if not disabled.
if !e.NoThresholds {
processes.Add(1)
go func() {
defer processes.Done()
defer e.logger.Debug("Engine: Thresholds terminated")
ticker := time.NewTicker(thresholdsRate)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if e.processThresholds() {
close(thresholdAbortChan)
return
}
case <-runCtx.Done():
return
}
}
}()
}
return processes.Wait
}
func (e *Engine) processMetrics(globalCtx context.Context, processMetricsAfterRun chan struct{}) {
sampleContainers := []stats.SampleContainer{}
defer func() {
// Process any remaining metrics in the pipeline, by this point Run()
// has already finished and nothing else should be producing metrics.
e.logger.Debug("Metrics processing winding down...")
close(e.Samples)
for sc := range e.Samples {
sampleContainers = append(sampleContainers, sc)
}
e.processSamples(sampleContainers)
if !e.NoThresholds {
e.processThresholds() // Process the thresholds one final time
}
}()
ticker := time.NewTicker(collectRate)
defer ticker.Stop()
e.logger.Debug("Metrics processing started...")
processSamples := func() {
if len(sampleContainers) > 0 {
e.processSamples(sampleContainers)
// Make the new container with the same size as the previous
// one, assuming that we produce roughly the same amount of
// metrics data between ticks...
sampleContainers = make([]stats.SampleContainer, 0, cap(sampleContainers))
}
}
for {
select {
case <-ticker.C:
processSamples()
case <-processMetricsAfterRun:
e.logger.Debug("Processing metrics and thresholds after the test run has ended...")
processSamples()
if !e.NoThresholds {
e.processThresholds()
}
processMetricsAfterRun <- struct{}{}
case sc := <-e.Samples:
sampleContainers = append(sampleContainers, sc)
case <-globalCtx.Done():
return
}
}
}
func (e *Engine) setRunStatus(status lib.RunStatus) {
for _, c := range e.Collectors {
c.SetRunStatus(status)
}
}
func (e *Engine) IsTainted() bool {
return e.thresholdsTainted
}
// Stop closes a signal channel, forcing a running Engine to return
func (e *Engine) Stop() {
e.stopOnce.Do(func() {
close(e.stopChan)
})
}
// IsStopped returns a bool indicating whether the Engine has been stopped
func (e *Engine) IsStopped() bool {
select {
case <-e.stopChan:
return true
default:
return false
}
}
func (e *Engine) runMetricsEmission(ctx context.Context) {
ticker := time.NewTicker(metricsRate)
for {
select {
case <-ticker.C:
e.emitMetrics()
case <-ctx.Done():
return
}
}
}
func (e *Engine) emitMetrics() {
t := time.Now()
executionState := e.ExecutionScheduler.GetState()
// TODO: optimize and move this, it shouldn't call processSamples() directly
e.processSamples([]stats.SampleContainer{stats.ConnectedSamples{
Samples: []stats.Sample{
{
Time: t,
Metric: metrics.VUs,
Value: float64(executionState.GetCurrentlyActiveVUsCount()),
Tags: e.Options.RunTags,
}, {
Time: t,
Metric: metrics.VUsMax,
Value: float64(executionState.GetInitializedVUsCount()),
Tags: e.Options.RunTags,
},
},
Tags: e.Options.RunTags,
Time: t,
}})
}
func (e *Engine) processThresholds() (shouldAbort bool) {
e.MetricsLock.Lock()
defer e.MetricsLock.Unlock()
t := e.executionState.GetCurrentTestRunDuration()
e.thresholdsTainted = false
for _, m := range e.Metrics {
if len(m.Thresholds.Thresholds) == 0 {
continue
}
m.Tainted = null.BoolFrom(false)
e.logger.WithField("m", m.Name).Debug("running thresholds")
succ, err := m.Thresholds.Run(m.Sink, t)
if err != nil {
e.logger.WithField("m", m.Name).WithError(err).Error("Threshold error")
continue
}
if !succ {
e.logger.WithField("m", m.Name).Debug("Thresholds failed")
m.Tainted = null.BoolFrom(true)
e.thresholdsTainted = true
if m.Thresholds.Abort {
shouldAbort = true
}
}
}
return shouldAbort
}
func (e *Engine) processSamplesForMetrics(sampleContainers []stats.SampleContainer) {
for _, sampleContainer := range sampleContainers {
samples := sampleContainer.GetSamples()
if len(samples) == 0 {
continue
}
for _, sample := range samples {
m, ok := e.Metrics[sample.Metric.Name]
if !ok {
m = stats.New(sample.Metric.Name, sample.Metric.Type, sample.Metric.Contains)
m.Thresholds = e.thresholds[m.Name]
m.Submetrics = e.submetrics[m.Name]
e.Metrics[m.Name] = m
}
m.Sink.Add(sample)
for _, sm := range m.Submetrics {
if !sample.Tags.Contains(sm.Tags) {
continue
}
if sm.Metric == nil {
sm.Metric = stats.New(sm.Name, sample.Metric.Type, sample.Metric.Contains)
sm.Metric.Sub = *sm
sm.Metric.Thresholds = e.thresholds[sm.Name]
e.Metrics[sm.Name] = sm.Metric
}
sm.Metric.Sink.Add(sample)
}
}
}
}
func (e *Engine) processSamples(sampleContainers []stats.SampleContainer) {
if len(sampleContainers) == 0 {
return
}
// TODO: optimize this...
e.MetricsLock.Lock()
defer e.MetricsLock.Unlock()
// TODO: run this and the below code in goroutines?
if !(e.NoSummary && e.NoThresholds && !e.SummaryExport) {
e.processSamplesForMetrics(sampleContainers)
}
for _, collector := range e.Collectors {
collector.Collect(sampleContainers)
}
}