-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
run.go
366 lines (323 loc) · 10.8 KB
/
run.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
package cmd
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"runtime"
"sync"
"time"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.k6.io/k6/api"
"go.k6.io/k6/core"
"go.k6.io/k6/core/local"
"go.k6.io/k6/errext"
"go.k6.io/k6/errext/exitcodes"
"go.k6.io/k6/js/common"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/consts"
"go.k6.io/k6/ui/pb"
)
// cmdRun handles the `k6 run` sub-command
type cmdRun struct {
gs *globalState
}
// TODO: split apart some more
//nolint:funlen,gocognit,gocyclo,cyclop
func (c *cmdRun) run(cmd *cobra.Command, args []string) error {
printBanner(c.gs)
test, err := loadAndConfigureTest(c.gs, cmd, args, getConfig)
if err != nil {
return err
}
// Write the full consolidated *and derived* options back to the Runner.
conf := test.derivedConfig
testRunState, err := test.buildTestRunState(conf.Options)
if err != nil {
return err
}
// We prepare a bunch of contexts:
// - The runCtx is cancelled as soon as the Engine's run() lambda finishes,
// and can trigger things like the usage report and end of test summary.
// Crucially, metrics processing by the Engine will still work after this
// context is cancelled!
// - The lingerCtx is cancelled by Ctrl+C, and is used to wait for that
// event when k6 was ran with the --linger option.
// - The globalCtx is cancelled only after we're completely done with the
// test execution and any --linger has been cleared, so that the Engine
// can start winding down its metrics processing.
globalCtx, globalCancel := context.WithCancel(c.gs.ctx)
defer globalCancel()
lingerCtx, lingerCancel := context.WithCancel(globalCtx)
defer lingerCancel()
runCtx, runCancel := context.WithCancel(lingerCtx)
defer runCancel()
logger := testRunState.Logger
// Create a local execution scheduler wrapping the runner.
logger.Debug("Initializing the execution scheduler...")
execScheduler, err := local.NewExecutionScheduler(testRunState)
if err != nil {
return err
}
// This is manually triggered after the Engine's Run() has completed,
// and things like a single Ctrl+C don't affect it. We use it to make
// sure that the progressbars finish updating with the latest execution
// state one last time, after the test run has finished.
progressCtx, progressCancel := context.WithCancel(globalCtx)
defer progressCancel()
initBar := execScheduler.GetInitProgressBar()
progressBarWG := &sync.WaitGroup{}
progressBarWG.Add(1)
go func() {
pbs := []*pb.ProgressBar{execScheduler.GetInitProgressBar()}
for _, s := range execScheduler.GetExecutors() {
pbs = append(pbs, s.GetProgress())
}
showProgress(progressCtx, c.gs, pbs, logger)
progressBarWG.Done()
}()
// Create all outputs.
executionPlan := execScheduler.GetExecutionPlan()
outputs, err := createOutputs(c.gs, test, executionPlan)
if err != nil {
return err
}
// TODO: create a MetricsEngine here and add its ingester to the list of
// outputs (unless both NoThresholds and NoSummary were enabled)
// TODO: remove this completely
// Create the engine.
initBar.Modify(pb.WithConstProgress(0, "Init engine"))
engine, err := core.NewEngine(testRunState, execScheduler, outputs)
if err != nil {
return err
}
// Spin up the REST API server, if not disabled.
if c.gs.flags.address != "" {
initBar.Modify(pb.WithConstProgress(0, "Init API server"))
go func() {
logger.Debugf("Starting the REST API server on %s", c.gs.flags.address)
// TODO: send the ExecutionState and MetricsEngine instead of the Engine
if aerr := api.ListenAndServe(c.gs.flags.address, engine, logger); aerr != nil {
// Only exit k6 if the user has explicitly set the REST API address
if cmd.Flags().Lookup("address").Changed {
logger.WithError(aerr).Error("Error from API server")
c.gs.osExit(int(exitcodes.CannotStartRESTAPI))
} else {
logger.WithError(aerr).Warn("Error from API server")
}
}
}()
}
// We do this here so we can get any output URLs below.
initBar.Modify(pb.WithConstProgress(0, "Starting outputs"))
// TODO: directly create the MutputManager here, not in the Engine
err = engine.OutputManager.StartOutputs()
if err != nil {
return err
}
defer engine.OutputManager.StopOutputs()
printExecutionDescription(
c.gs, "local", args[0], "", conf, execScheduler.GetState().ExecutionTuple, executionPlan, outputs,
)
// Trap Interrupts, SIGINTs and SIGTERMs.
gracefulStop := func(sig os.Signal) {
logger.WithField("sig", sig).Debug("Stopping k6 in response to signal...")
lingerCancel() // stop the test run, metric processing is cancelled below
}
onHardStop := func(sig os.Signal) {
logger.WithField("sig", sig).Error("Aborting k6 in response to signal")
globalCancel() // not that it matters, given the following command...
}
stopSignalHandling := handleTestAbortSignals(c.gs, gracefulStop, onHardStop)
defer stopSignalHandling()
// Initialize the engine
initBar.Modify(pb.WithConstProgress(0, "Init VUs..."))
engineRun, engineWait, err := engine.Init(globalCtx, runCtx)
if err != nil {
err = common.UnwrapGojaInterruptedError(err)
// Add a generic engine exit code if we don't have a more specific one
return errext.WithExitCodeIfNone(err, exitcodes.GenericEngine)
}
// Init has passed successfully, so unless disabled, make sure we send a
// usage report after the context is done.
if !conf.NoUsageReport.Bool {
reportDone := make(chan struct{})
go func() {
<-runCtx.Done()
_ = reportUsage(execScheduler)
close(reportDone)
}()
defer func() {
select {
case <-reportDone:
case <-time.After(3 * time.Second):
}
}()
}
// Start the test run
initBar.Modify(pb.WithConstProgress(0, "Starting test..."))
var interrupt error
err = engineRun()
if err != nil {
err = common.UnwrapGojaInterruptedError(err)
if errext.IsInterruptError(err) {
// Don't return here since we need to work with --linger,
// show the end-of-test summary and exit cleanly.
interrupt = err
}
if !conf.Linger.Bool && interrupt == nil {
return errext.WithExitCodeIfNone(err, exitcodes.GenericEngine)
}
}
runCancel()
logger.Debug("Engine run terminated cleanly")
progressCancel()
progressBarWG.Wait()
executionState := execScheduler.GetState()
// Warn if no iterations could be completed.
if executionState.GetFullIterationCount() == 0 {
logger.Warn("No script iterations finished, consider making the test duration longer")
}
// Handle the end-of-test summary.
if !testRunState.RuntimeOptions.NoSummary.Bool {
engine.MetricsEngine.MetricsLock.Lock() // TODO: refactor so this is not needed
summaryResult, err := test.initRunner.HandleSummary(globalCtx, &lib.Summary{
Metrics: engine.MetricsEngine.ObservedMetrics,
RootGroup: execScheduler.GetRunner().GetDefaultGroup(),
TestRunDuration: executionState.GetCurrentTestRunDuration(),
NoColor: c.gs.flags.noColor,
UIState: lib.UIState{
IsStdOutTTY: c.gs.stdOut.isTTY,
IsStdErrTTY: c.gs.stdErr.isTTY,
},
})
engine.MetricsEngine.MetricsLock.Unlock()
if err == nil {
err = handleSummaryResult(c.gs.fs, c.gs.stdOut, c.gs.stdErr, summaryResult)
}
if err != nil {
logger.WithError(err).Error("failed to handle the end-of-test summary")
}
}
if conf.Linger.Bool {
select {
case <-lingerCtx.Done():
// do nothing, we were interrupted by Ctrl+C already
default:
logger.Debug("Linger set; waiting for Ctrl+C...")
if !c.gs.flags.quiet {
printToStdout(c.gs, "Linger set; waiting for Ctrl+C...")
}
<-lingerCtx.Done()
logger.Debug("Ctrl+C received, exiting...")
}
}
globalCancel() // signal the Engine that it should wind down
logger.Debug("Waiting for engine processes to finish...")
engineWait()
logger.Debug("Everything has finished, exiting k6!")
if test.keyLogger != nil {
if err := test.keyLogger.Close(); err != nil {
logger.WithError(err).Warn("Error while closing the SSLKEYLOGFILE")
}
}
if interrupt != nil {
return interrupt
}
if engine.IsTainted() {
return errext.WithExitCodeIfNone(errors.New("some thresholds have failed"), exitcodes.ThresholdsHaveFailed)
}
return nil
}
func (c *cmdRun) flagSet() *pflag.FlagSet {
flags := pflag.NewFlagSet("", pflag.ContinueOnError)
flags.SortFlags = false
flags.AddFlagSet(optionFlagSet())
flags.AddFlagSet(runtimeOptionFlagSet(true))
flags.AddFlagSet(configFlagSet())
return flags
}
func getCmdRun(gs *globalState) *cobra.Command {
c := &cmdRun{
gs: gs,
}
runCmd := &cobra.Command{
Use: "run",
Short: "Start a load test",
Long: `Start a load test.
This also exposes a REST API to interact with it. Various k6 subcommands offer
a commandline interface for interacting with it.`,
Example: `
# Run a single VU, once.
k6 run script.js
# Run a single VU, 10 times.
k6 run -i 10 script.js
# Run 5 VUs, splitting 10 iterations between them.
k6 run -u 5 -i 10 script.js
# Run 5 VUs for 10s.
k6 run -u 5 -d 10s script.js
# Ramp VUs from 0 to 100 over 10s, stay there for 60s, then 10s down to 0.
k6 run -u 0 -s 10s:100 -s 60s -s 10s:0
# Send metrics to an influxdb server
k6 run -o influxdb=http://1.2.3.4:8086/k6`[1:],
Args: exactArgsWithMsg(1, "arg should either be \"-\", if reading script from stdin, or a path to a script file"),
RunE: c.run,
}
runCmd.Flags().SortFlags = false
runCmd.Flags().AddFlagSet(c.flagSet())
return runCmd
}
func reportUsage(execScheduler *local.ExecutionScheduler) error {
execState := execScheduler.GetState()
executorConfigs := execScheduler.GetExecutorConfigs()
executors := make(map[string]int)
for _, ec := range executorConfigs {
executors[ec.GetType()]++
}
body, err := json.Marshal(map[string]interface{}{
"k6_version": consts.Version,
"executors": executors,
"vus_max": execState.GetInitializedVUsCount(),
"iterations": execState.GetFullIterationCount(),
"duration": execState.GetCurrentTestRunDuration().String(),
"goos": runtime.GOOS,
"goarch": runtime.GOARCH,
})
if err != nil {
return err
}
res, err := http.Post("https://reports.k6.io/", "application/json", bytes.NewBuffer(body)) //nolint:noctx
defer func() {
if err == nil {
_ = res.Body.Close()
}
}()
return err
}
func handleSummaryResult(fs afero.Fs, stdOut, stdErr io.Writer, result map[string]io.Reader) error {
var errs []error
getWriter := func(path string) (io.Writer, error) {
switch path {
case "stdout":
return stdOut, nil
case "stderr":
return stdErr, nil
default:
return fs.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o666)
}
}
for path, value := range result {
if writer, err := getWriter(path); err != nil {
errs = append(errs, fmt.Errorf("could not open '%s': %w", path, err))
} else if n, err := io.Copy(writer, value); err != nil {
errs = append(errs, fmt.Errorf("error saving summary to '%s' after %d bytes: %w", path, n, err))
}
}
return consolidateErrorMessage(errs, "Could not save some summary information:")
}