-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
runner.go
509 lines (439 loc) · 13.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
/*
*
* 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 js
import (
"context"
"crypto/tls"
"encoding/json"
"net"
"net/http"
"net/http/cookiejar"
"strconv"
"sync"
"time"
"github.com/dop251/goja"
"github.com/oxtoacart/bpool"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/viki-org/dnscache"
"golang.org/x/net/http2"
"golang.org/x/time/rate"
"github.com/loadimpact/k6/js/common"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/netext"
"github.com/loadimpact/k6/loader"
"github.com/loadimpact/k6/stats"
)
//nolint:gochecknoglobals
var (
errInterrupt = errors.New("context cancelled")
stageSetup = "setup"
stageTeardown = "teardown"
)
// Ensure Runner implements the lib.Runner interface
var _ lib.Runner = &Runner{}
type Runner struct {
Bundle *Bundle
Logger *logrus.Logger
defaultGroup *lib.Group
BaseDialer net.Dialer
Resolver *dnscache.Resolver
RPSLimit *rate.Limiter
console *console
setupData []byte
}
// New returns a new Runner for the provide source
func New(src *loader.SourceData, filesystems map[string]afero.Fs, rtOpts lib.RuntimeOptions) (*Runner, error) {
bundle, err := NewBundle(src, filesystems, rtOpts)
if err != nil {
return nil, err
}
return NewFromBundle(bundle)
}
func NewFromArchive(arc *lib.Archive, rtOpts lib.RuntimeOptions) (*Runner, error) {
bundle, err := NewBundleFromArchive(arc, rtOpts)
if err != nil {
return nil, err
}
return NewFromBundle(bundle)
}
func NewFromBundle(b *Bundle) (*Runner, error) {
defaultGroup, err := lib.NewGroup("", nil)
if err != nil {
return nil, err
}
r := &Runner{
Bundle: b,
Logger: logrus.StandardLogger(),
defaultGroup: defaultGroup,
BaseDialer: net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
},
console: newConsole(),
Resolver: dnscache.New(0),
}
err = r.SetOptions(r.Bundle.Options)
return r, err
}
func (r *Runner) MakeArchive() *lib.Archive {
return r.Bundle.makeArchive()
}
func (r *Runner) NewVU(samplesOut chan<- stats.SampleContainer) (lib.VU, error) {
vu, err := r.newVU(samplesOut)
if err != nil {
return nil, err
}
return lib.VU(vu), nil
}
func (r *Runner) newVU(samplesOut chan<- stats.SampleContainer) (*VU, error) {
// Instantiate a new bundle, make a VU out of it.
bi, err := r.Bundle.Instantiate()
if err != nil {
return nil, err
}
var cipherSuites []uint16
if r.Bundle.Options.TLSCipherSuites != nil {
cipherSuites = *r.Bundle.Options.TLSCipherSuites
}
var tlsVersions lib.TLSVersions
if r.Bundle.Options.TLSVersion != nil {
tlsVersions = *r.Bundle.Options.TLSVersion
}
tlsAuth := r.Bundle.Options.TLSAuth
certs := make([]tls.Certificate, len(tlsAuth))
nameToCert := make(map[string]*tls.Certificate)
for i, auth := range tlsAuth {
for _, name := range auth.Domains {
cert, err := auth.Certificate()
if err != nil {
return nil, err
}
certs[i] = *cert
nameToCert[name] = &certs[i]
}
}
dialer := &netext.Dialer{
Dialer: r.BaseDialer,
Resolver: r.Resolver,
Blacklist: r.Bundle.Options.BlacklistIPs,
Hosts: r.Bundle.Options.Hosts,
}
tlsConfig := &tls.Config{
InsecureSkipVerify: r.Bundle.Options.InsecureSkipTLSVerify.Bool,
CipherSuites: cipherSuites,
MinVersion: uint16(tlsVersions.Min),
MaxVersion: uint16(tlsVersions.Max),
Certificates: certs,
NameToCertificate: nameToCert,
Renegotiation: tls.RenegotiateFreelyAsClient,
}
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: tlsConfig,
DialContext: dialer.DialContext,
DisableCompression: true,
DisableKeepAlives: r.Bundle.Options.NoConnectionReuse.Bool,
MaxIdleConns: int(r.Bundle.Options.Batch.Int64),
MaxIdleConnsPerHost: int(r.Bundle.Options.BatchPerHost.Int64),
}
_ = http2.ConfigureTransport(transport)
cookieJar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}
vu := &VU{
BundleInstance: *bi,
Runner: r,
Transport: transport,
Dialer: dialer,
CookieJar: cookieJar,
TLSConfig: tlsConfig,
Console: r.console,
BPool: bpool.NewBufferPool(100),
Samples: samplesOut,
m: &sync.Mutex{},
}
vu.Runtime.Set("console", common.Bind(vu.Runtime, vu.Console, vu.Context))
common.BindToGlobal(vu.Runtime, map[string]interface{}{
"open": func() {
common.Throw(vu.Runtime, errors.New("\"open\" function is only available to the init code (aka global scope), see https://docs.k6.io/docs/test-life-cycle for more information"))
},
})
// Give the VU an initial sense of identity.
if err := vu.Reconfigure(0); err != nil {
return nil, err
}
return vu, nil
}
func (r *Runner) Setup(ctx context.Context, out chan<- stats.SampleContainer) error {
setupCtx, setupCancel := context.WithTimeout(
ctx,
time.Duration(r.Bundle.Options.SetupTimeout.Duration),
)
defer setupCancel()
v, err := r.runPart(setupCtx, out, stageSetup, nil)
if err != nil {
return errors.Wrap(err, stageSetup)
}
// r.setupData = nil is special it means undefined from this moment forward
if goja.IsUndefined(v) {
r.setupData = nil
return nil
}
r.setupData, err = json.Marshal(v.Export())
if err != nil {
return errors.Wrap(err, stageSetup)
}
var tmp interface{}
return json.Unmarshal(r.setupData, &tmp)
}
// GetSetupData returns the setup data as json if Setup() was specified and executed, nil otherwise
func (r *Runner) GetSetupData() []byte {
return r.setupData
}
// SetSetupData saves the externally supplied setup data as json in the runner, so it can be used in VUs
func (r *Runner) SetSetupData(data []byte) {
r.setupData = data
}
func (r *Runner) Teardown(ctx context.Context, out chan<- stats.SampleContainer) error {
teardownCtx, teardownCancel := context.WithTimeout(
ctx,
time.Duration(r.Bundle.Options.TeardownTimeout.Duration),
)
defer teardownCancel()
var data interface{}
if r.setupData != nil {
if err := json.Unmarshal(r.setupData, &data); err != nil {
return errors.Wrap(err, stageTeardown)
}
} else {
data = goja.Undefined()
}
_, err := r.runPart(teardownCtx, out, stageTeardown, data)
return err
}
func (r *Runner) GetDefaultGroup() *lib.Group {
return r.defaultGroup
}
func (r *Runner) GetOptions() lib.Options {
return r.Bundle.Options
}
func (r *Runner) SetOptions(opts lib.Options) error {
r.Bundle.Options = opts
r.RPSLimit = nil
if rps := opts.RPS; rps.Valid {
r.RPSLimit = rate.NewLimiter(rate.Limit(rps.Int64), 1)
}
//TODO: validate that all exec values are either nil or valid exported methods (or HTTP requests in the future)
if opts.ConsoleOutput.Valid {
c, err := newFileConsole(opts.ConsoleOutput.String)
if err != nil {
return err
}
r.console = c
}
return nil
}
// Runs an exported function in its own temporary VU, optionally with an argument. Execution is
// interrupted if the context expires. No error is returned if the part does not exist.
func (r *Runner) runPart(ctx context.Context, out chan<- stats.SampleContainer, name string, arg interface{}) (goja.Value, error) {
vu, err := r.newVU(out)
if err != nil {
return goja.Undefined(), err
}
exp := vu.Runtime.Get("exports").ToObject(vu.Runtime)
if exp == nil {
return goja.Undefined(), nil
}
fn, ok := goja.AssertFunction(exp.Get(name))
if !ok {
return goja.Undefined(), nil
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
go func() {
<-ctx.Done()
vu.Runtime.Interrupt(errInterrupt)
}()
group, err := lib.NewGroup(name, r.GetDefaultGroup())
if err != nil {
return goja.Undefined(), err
}
v, _, _, err := vu.runFn(ctx, group, false, fn, vu.Runtime.ToValue(arg))
// deadline is reached so we have timeouted but this might've not been registered correctly
if deadline, ok := ctx.Deadline(); ok && time.Now().After(deadline) {
// we could have an error that is not errInterrupt in which case we should return it instead
if err, ok := err.(*goja.InterruptedError); ok && v != nil && err.Value() != errInterrupt {
//TODO: silence this error?
return v, err
}
// otherwise we have timeouted
return v, lib.NewTimeoutError(name, r.timeoutErrorDuration(name))
}
return v, err
}
// timeoutErrorDuration returns the timeout duration for given stage.
func (r *Runner) timeoutErrorDuration(stage string) time.Duration {
d := time.Duration(0)
switch stage {
case stageSetup:
return time.Duration(r.Bundle.Options.SetupTimeout.Duration)
case stageTeardown:
return time.Duration(r.Bundle.Options.TeardownTimeout.Duration)
}
return d
}
type VU struct {
BundleInstance
Runner *Runner
Transport *http.Transport
Dialer *netext.Dialer
CookieJar *cookiejar.Jar
TLSConfig *tls.Config
ID int64
Iteration int64
Console *console
BPool *bpool.BufferPool
Samples chan<- stats.SampleContainer
setupData goja.Value
// A VU will track the last context it was called with for cancellation.
// Note that interruptTrackedCtx is the context that is currently being tracked, while
// interruptCancel cancels an unrelated context that terminates the tracking goroutine
// without triggering an interrupt (for if the context changes).
// There are cleaner ways of handling the interruption problem, but this is a hot path that
// needs to be called thousands of times per second, which rules out anything that spawns a
// goroutine per call.
interruptTrackedCtx context.Context
interruptCancel context.CancelFunc
m *sync.Mutex
}
// Verify that VU implements lib.VU
var _ lib.VU = &VU{}
func (u *VU) Reconfigure(id int64) error {
u.ID = id
u.Iteration = 0
u.Runtime.Set("__VU", u.ID)
return nil
}
func (u *VU) RunOnce(ctx context.Context) error {
u.m.Lock()
defer u.m.Unlock()
// Track the context and interrupt JS execution if it's cancelled.
if u.interruptTrackedCtx != ctx {
interCtx, interCancel := context.WithCancel(context.Background())
if u.interruptCancel != nil {
u.interruptCancel()
}
u.interruptCancel = interCancel
u.interruptTrackedCtx = ctx
defer interCancel()
go func() {
select {
case <-interCtx.Done():
case <-ctx.Done():
u.Runtime.Interrupt(errInterrupt)
}
}()
}
// Unmarshall the setupData only the first time for each VU so that VUs are isolated but we
// still don't use too much CPU in the middle test
if u.setupData == nil {
if u.Runner.setupData != nil {
var data interface{}
if err := json.Unmarshal(u.Runner.setupData, &data); err != nil {
return errors.Wrap(err, "RunOnce")
}
u.setupData = u.Runtime.ToValue(data)
} else {
u.setupData = goja.Undefined()
}
}
// Call the default function.
_, isFullIteration, totalTime, err := u.runFn(ctx, u.Runner.defaultGroup, true, u.Default, u.setupData)
// If MinIterationDuration is specified and the iteration wasn't cancelled
// and was less than it, sleep for the remainder
if isFullIteration && u.Runner.Bundle.Options.MinIterationDuration.Valid {
durationDiff := time.Duration(u.Runner.Bundle.Options.MinIterationDuration.Duration) - totalTime
if durationDiff > 0 {
time.Sleep(durationDiff)
}
}
return err
}
func (u *VU) runFn(
ctx context.Context, group *lib.Group, isDefault bool, fn goja.Callable, args ...goja.Value,
) (goja.Value, bool, time.Duration, error) {
cookieJar := u.CookieJar
if !u.Runner.Bundle.Options.NoCookiesReset.ValueOrZero() {
var err error
cookieJar, err = cookiejar.New(nil)
if err != nil {
return goja.Undefined(), false, time.Duration(0), err
}
}
state := &lib.State{
Logger: u.Runner.Logger,
Options: u.Runner.Bundle.Options,
Group: group,
Transport: u.Transport,
Dialer: u.Dialer,
TLSConfig: u.TLSConfig,
CookieJar: cookieJar,
RPSLimit: u.Runner.RPSLimit,
BPool: u.BPool,
Vu: u.ID,
Samples: u.Samples,
Iteration: u.Iteration,
}
newctx := common.WithRuntime(ctx, u.Runtime)
newctx = lib.WithState(newctx, state)
*u.Context = newctx
u.Runtime.Set("__ITER", u.Iteration)
iter := u.Iteration
u.Iteration++
startTime := time.Now()
v, err := fn(goja.Undefined(), args...) // Actually run the JS script
endTime := time.Now()
var isFullIteration bool
select {
case <-ctx.Done():
isFullIteration = false
default:
isFullIteration = true
}
tags := state.Options.RunTags.CloneTags()
if state.Options.SystemTags.Has(stats.TagVU) {
tags["vu"] = strconv.FormatInt(u.ID, 10)
}
if state.Options.SystemTags.Has(stats.TagIter) {
tags["iter"] = strconv.FormatInt(iter, 10)
}
if state.Options.SystemTags.Has(stats.TagGroup) {
tags["group"] = group.Path
}
if u.Runner.Bundle.Options.NoVUConnectionReuse.Bool {
u.Transport.CloseIdleConnections()
}
state.Samples <- u.Dialer.GetTrail(startTime, endTime, isFullIteration, isDefault, stats.IntoSampleTags(&tags))
return v, isFullIteration, endTime.Sub(startTime), err
}