-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
local.go
400 lines (349 loc) · 13 KB
/
local.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
/*
*
* 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 local
import (
"context"
"fmt"
"runtime"
"sync/atomic"
"time"
"github.com/loadimpact/k6/ui/pb"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/stats"
"github.com/sirupsen/logrus"
)
// ExecutionScheduler is the local implementation of lib.ExecutionScheduler
type ExecutionScheduler struct {
runner lib.Runner
options lib.Options
logger *logrus.Logger
initProgress *pb.ProgressBar
executors []lib.Executor // sorted by (startTime, ID)
executionPlan []lib.ExecutionStep
maxDuration time.Duration // cached value derived from the execution plan
maxPossibleVUs uint64 // cached value derived from the execution plan
state *lib.ExecutionState
}
// Check to see if we implement the lib.ExecutionScheduler interface
var _ lib.ExecutionScheduler = &ExecutionScheduler{}
// NewExecutionScheduler creates and returns a new local lib.ExecutionScheduler
// instance, without initializing it beyond the bare minimum. Specifically, it
// creates the needed executor instances and a lot of state placeholders, but it
// doesn't initialize the executors and it doesn't initialize or run VUs.
func NewExecutionScheduler(runner lib.Runner, logger *logrus.Logger) (*ExecutionScheduler, error) {
options := runner.GetOptions()
executionPlan := options.Execution.GetFullExecutionRequirements(options.ExecutionSegment)
maxPlannedVUs := lib.GetMaxPlannedVUs(executionPlan)
maxPossibleVUs := lib.GetMaxPossibleVUs(executionPlan)
executionState := lib.NewExecutionState(options, maxPlannedVUs, maxPossibleVUs)
maxDuration, _ := lib.GetEndOffset(executionPlan) // we don't care if the end offset is final
executorConfigs := options.Execution.GetSortedConfigs()
executors := make([]lib.Executor, len(executorConfigs))
for i, sc := range executorConfigs {
s, err := sc.NewExecutor(executionState, logger.WithField("executor", sc.GetName()))
if err != nil {
return nil, err
}
executors[i] = s
}
if options.Paused.Bool {
if err := executionState.Pause(); err != nil {
return nil, err
}
}
return &ExecutionScheduler{
runner: runner,
logger: logger,
options: options,
initProgress: pb.New(pb.WithConstLeft("Init")),
executors: executors,
executionPlan: executionPlan,
maxDuration: maxDuration,
maxPossibleVUs: maxPossibleVUs,
state: executionState,
}, nil
}
// GetRunner returns the wrapped lib.Runner instance.
func (e *ExecutionScheduler) GetRunner() lib.Runner {
return e.runner
}
// GetState returns a pointer to the execution state struct for the local
// execution scheduler. It's guaranteed to be initialized and present, though
// see the documentation in lib/execution.go for caveats about its usage. The
// most important one is that none of the methods beyond the pause-related ones
// should be used for synchronization.
func (e *ExecutionScheduler) GetState() *lib.ExecutionState {
return e.state
}
// GetExecutors returns the slice of configured executor instances, sorted by
// their (startTime, name) in an ascending order.
func (e *ExecutionScheduler) GetExecutors() []lib.Executor {
return e.executors
}
// GetInitProgressBar returns the progress bar associated with the Init
// function. After the Init is done, it is "hijacked" to display real-time
// execution statistics as a text bar.
func (e *ExecutionScheduler) GetInitProgressBar() *pb.ProgressBar {
return e.initProgress
}
// GetExecutionPlan is a helper method so users of the local execution scheduler
// don't have to calculate the execution plan again.
func (e *ExecutionScheduler) GetExecutionPlan() []lib.ExecutionStep {
return e.executionPlan
}
// initVU is just a helper method that's used to both initialize the planned VUs
// in the Init() method, and also passed to executors so they can initialize
// any unplanned VUs themselves.
//TODO: actually use the context...
func (e *ExecutionScheduler) initVU(
_ context.Context, logger *logrus.Entry, engineOut chan<- stats.SampleContainer,
) (lib.VU, error) {
vu, err := e.runner.NewVU(engineOut)
if err != nil {
return nil, fmt.Errorf("error while initializing a VU: '%s'", err)
}
// Get the VU ID here, so that the VUs are (mostly) ordered by their
// number in the channel buffer
vuID := e.state.GetUniqueVUIdentifier()
if err := vu.Reconfigure(int64(vuID)); err != nil {
return nil, fmt.Errorf("error while reconfiguring VU #%d: '%s'", vuID, err)
}
logger.Debugf("Initialized VU #%d", vuID)
return vu, nil
}
// getRunStats is a helper function that can be used as the execution
// scheduler's progressbar substitute (i.e. hijack).
func (e *ExecutionScheduler) getRunStats() string {
status := "running"
if e.state.IsPaused() {
status = "paused"
}
if e.state.HasStarted() {
dur := e.state.GetCurrentTestRunDuration()
status = fmt.Sprintf("%s (%s)", status, pb.GetFixedLengthDuration(dur, e.maxDuration))
}
vusFmt := pb.GetFixedLengthIntFormat(int64(e.maxPossibleVUs))
return fmt.Sprintf(
"%s, "+vusFmt+"/"+vusFmt+" VUs, %d complete and %d interrupted iterations",
status, e.state.GetCurrentlyActiveVUsCount(), e.state.GetInitializedVUsCount(),
e.state.GetFullIterationCount(), e.state.GetPartialIterationCount(),
)
}
// Init concurrently initializes all of the planned VUs and then sequentially
// initializes all of the configured executors.
func (e *ExecutionScheduler) Init(ctx context.Context, engineOut chan<- stats.SampleContainer) error {
logger := e.logger.WithField("phase", "local-execution-scheduler-init")
vusToInitialize := lib.GetMaxPlannedVUs(e.executionPlan)
logger.WithFields(logrus.Fields{
"neededVUs": vusToInitialize,
"executorsCount": len(e.executors),
}).Debugf("Start of initialization")
// Initialize VUs concurrently
doneInits := make(chan error, vusToInitialize) // poor man's early-return waitgroup
//TODO: make this an option?
initConcurrency := runtime.NumCPU()
limiter := make(chan struct{})
subctx, cancel := context.WithCancel(ctx)
defer cancel()
for i := 0; i < initConcurrency; i++ {
go func() {
for range limiter {
newVU, err := e.initVU(ctx, logger, engineOut)
if err == nil {
e.state.AddInitializedVU(newVU)
}
doneInits <- err
}
}()
}
go func() {
defer close(limiter)
for vuNum := uint64(0); vuNum < vusToInitialize; vuNum++ {
select {
case limiter <- struct{}{}:
case <-subctx.Done():
return
}
}
}()
initializedVUs := new(uint64)
vusFmt := pb.GetFixedLengthIntFormat(int64(vusToInitialize))
e.initProgress.Modify(
pb.WithProgress(func() (float64, string) {
doneVUs := atomic.LoadUint64(initializedVUs)
return float64(doneVUs) / float64(vusToInitialize),
fmt.Sprintf(vusFmt+"/%d VUs initialized", doneVUs, vusToInitialize)
}),
)
for vuNum := uint64(0); vuNum < vusToInitialize; vuNum++ {
select {
case err := <-doneInits:
if err != nil {
logger.WithError(err).Debug("VU initialization returned with an error, aborting...")
// the context's cancel() is called in a defer above and will
// abort any in-flight VU initializations
return err
}
atomic.AddUint64(initializedVUs, 1)
case <-ctx.Done():
return ctx.Err()
}
}
e.state.SetInitVUFunc(func(ctx context.Context, logger *logrus.Entry) (lib.VU, error) {
return e.initVU(ctx, logger, engineOut)
})
logger.Debugf("Finished initializing needed VUs, start initializing executors...")
for _, exec := range e.executors {
executorConfig := exec.GetConfig()
if err := exec.Init(ctx); err != nil {
return fmt.Errorf("error while initializing executor %s: %s", executorConfig.GetName(), err)
}
logger.Debugf("Initialized executor %s", executorConfig.GetName())
}
logger.Debugf("Initialization completed")
return nil
}
// runExecutor gets called by the public Run() method once per configured
// executor, each time in a new goroutine. It is responsible for waiting out the
// configured startTime for the specific executor and then running its Run()
// method.
func (e *ExecutionScheduler) runExecutor(
runCtx context.Context, runResults chan<- error, engineOut chan<- stats.SampleContainer, executor lib.Executor,
) {
executorConfig := executor.GetConfig()
executorStartTime := executorConfig.GetStartTime()
executorLogger := e.logger.WithFields(logrus.Fields{
"executor": executorConfig.GetName(),
"type": executorConfig.GetType(),
"startTime": executorStartTime,
})
executorProgress := executor.GetProgress()
// Check if we have to wait before starting the actual executor execution
if executorStartTime > 0 {
startTime := time.Now()
executorProgress.Modify(pb.WithProgress(func() (float64, string) {
remWait := (executorStartTime - time.Since(startTime))
return 0, fmt.Sprintf("waiting %s", pb.GetFixedLengthDuration(remWait, executorStartTime))
}))
executorLogger.Debugf("Waiting for executor start time...")
select {
case <-runCtx.Done():
runResults <- nil // no error since executor hasn't started yet
return
case <-time.After(executorStartTime):
// continue
}
}
executorProgress.Modify(pb.WithConstProgress(0, "started"))
executorLogger.Debugf("Starting executor")
err := executor.Run(runCtx, engineOut) // executor should handle context cancel itself
if err == nil {
executorLogger.Debugf("Executor finished successfully")
} else {
executorLogger.WithField("error", err).Errorf("Executor error")
}
runResults <- err
}
// Run the ExecutionScheduler, funneling all generated metric samples through the supplied
// out channel.
func (e *ExecutionScheduler) Run(ctx context.Context, engineOut chan<- stats.SampleContainer) error {
executorsCount := len(e.executors)
logger := e.logger.WithField("phase", "local-execution-scheduler-run")
e.initProgress.Modify(pb.WithConstLeft("Run"))
if e.state.IsPaused() {
logger.Debug("Execution is paused, waiting for resume or interrupt...")
e.initProgress.Modify(pb.WithConstProgress(1, "paused"))
select {
case <-e.state.ResumeNotify():
// continue
case <-ctx.Done():
return nil
}
}
e.state.MarkStarted()
defer e.state.MarkEnded()
e.initProgress.Modify(pb.WithConstProgress(1, "running"))
logger.WithFields(logrus.Fields{"executorsCount": executorsCount}).Debugf("Start of test run")
runResults := make(chan error, executorsCount) // nil values are successful runs
runCtx, cancel := context.WithCancel(ctx)
defer cancel() // just in case, and to shut up go vet...
// Run setup() before any executors, if it's not disabled
if !e.options.NoSetup.Bool {
logger.Debug("Running setup()")
e.initProgress.Modify(pb.WithConstProgress(1, "setup()"))
if err := e.runner.Setup(runCtx, engineOut); err != nil {
logger.WithField("error", err).Debug("setup() aborted by error")
return err
}
}
e.initProgress.Modify(pb.WithHijack(e.getRunStats))
// Start all executors at their particular startTime in a separate goroutine...
logger.Debug("Start all executors...")
for _, exec := range e.executors {
go e.runExecutor(runCtx, runResults, engineOut, exec)
}
// Wait for all executors to finish
var firstErr error
for range e.executors {
err := <-runResults
if err != nil && firstErr == nil {
logger.WithError(err).Debug("Executor returned with an error, cancelling test run...")
firstErr = err
cancel()
}
}
// Run teardown() after all executors are done, if it's not disabled
if !e.options.NoTeardown.Bool {
logger.Debug("Running teardown()")
if err := e.runner.Teardown(ctx, engineOut); err != nil {
logger.WithField("error", err).Debug("teardown() aborted by error")
return err
}
}
return firstErr
}
// SetPaused pauses a test, if called with true. And if called with false, tries
// to start/resume it. See the lib.ExecutionScheduler interface documentation of
// the methods for the various caveats about its usage.
func (e *ExecutionScheduler) SetPaused(pause bool) error {
if !e.state.HasStarted() && e.state.IsPaused() {
if pause {
return fmt.Errorf("execution is already paused")
}
e.logger.Debug("Starting execution")
return e.state.Resume()
}
for _, exec := range e.executors {
pausableExecutor, ok := exec.(lib.PausableExecutor)
if !ok {
return fmt.Errorf(
"%s executor '%s' doesn't support pause and resume operations after its start",
exec.GetConfig().GetType(), exec.GetConfig().GetName(),
)
}
if err := pausableExecutor.SetPaused(pause); err != nil {
return err
}
}
if pause {
return e.state.Pause()
}
return e.state.Resume()
}