-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
runner.go
728 lines (630 loc) · 20.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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/*
*
* 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 (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/http/cookiejar"
"runtime/debug"
"strconv"
"time"
"github.com/dop251/goja"
"github.com/oxtoacart/bpool"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"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/consts"
"github.com/loadimpact/k6/lib/netext"
"github.com/loadimpact/k6/lib/types"
"github.com/loadimpact/k6/loader"
"github.com/loadimpact/k6/stats"
)
// 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 netext.Resolver
// TODO: Remove ActualResolver, it's a hack to simplify mocking in tests.
ActualResolver netext.MultiResolver
RPSLimit *rate.Limiter
console *console
setupData []byte
}
// New returns a new Runner for the provide source
func New(
logger *logrus.Logger, src *loader.SourceData, filesystems map[string]afero.Fs, rtOpts lib.RuntimeOptions,
) (*Runner, error) {
bundle, err := NewBundle(logger, src, filesystems, rtOpts)
if err != nil {
return nil, err
}
return newFromBundle(logger, bundle)
}
// NewFromArchive returns a new Runner from the source in the provided archive
func NewFromArchive(logger *logrus.Logger, arc *lib.Archive, rtOpts lib.RuntimeOptions) (*Runner, error) {
bundle, err := NewBundleFromArchive(logger, arc, rtOpts)
if err != nil {
return nil, err
}
return newFromBundle(logger, bundle)
}
func newFromBundle(logger *logrus.Logger, b *Bundle) (*Runner, error) {
defaultGroup, err := lib.NewGroup("", nil)
if err != nil {
return nil, err
}
defDNS := types.DefaultDNSConfig()
r := &Runner{
Bundle: b,
Logger: logger,
defaultGroup: defaultGroup,
BaseDialer: net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
},
console: newConsole(logger),
Resolver: netext.NewResolver(
net.LookupIP, 0, defDNS.Select.DNSSelect, defDNS.Policy.DNSPolicy),
ActualResolver: net.LookupIP,
}
err = r.SetOptions(r.Bundle.Options)
return r, err
}
func (r *Runner) MakeArchive() *lib.Archive {
return r.Bundle.makeArchive()
}
// NewVU returns a new initialized VU.
func (r *Runner) NewVU(id int64, samplesOut chan<- stats.SampleContainer) (lib.InitializedVU, error) {
vu, err := r.newVU(id, samplesOut)
if err != nil {
return nil, err
}
return lib.InitializedVU(vu), nil
}
// nolint:funlen
func (r *Runner) newVU(id int64, samplesOut chan<- stats.SampleContainer) (*VU, error) {
// Instantiate a new bundle, make a VU out of it.
bi, err := r.Bundle.Instantiate(r.Logger, id)
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,
BlockedHostnames: r.Bundle.Options.BlockedHostnames.Trie,
Hosts: r.Bundle.Options.Hosts,
}
if r.Bundle.Options.LocalIPs.Valid {
var ipIndex uint64
if id > 0 {
ipIndex = uint64(id - 1)
}
dialer.Dialer.LocalAddr = &net.TCPAddr{IP: r.Bundle.Options.LocalIPs.Pool.GetIP(ipIndex)}
}
tlsConfig := &tls.Config{
InsecureSkipVerify: r.Bundle.Options.InsecureSkipTLSVerify.Bool, //nolint:gosec
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{
ID: id,
BundleInstance: *bi,
Runner: r,
Transport: transport,
Dialer: dialer,
CookieJar: cookieJar,
TLSConfig: tlsConfig,
Console: r.console,
BPool: bpool.NewBufferPool(100),
Samples: samplesOut,
}
vu.state = &lib.State{
Logger: vu.Runner.Logger,
Options: vu.Runner.Bundle.Options,
Transport: vu.Transport,
Dialer: vu.Dialer,
TLSConfig: vu.TLSConfig,
CookieJar: cookieJar,
RPSLimit: vu.Runner.RPSLimit,
BPool: vu.BPool,
Vu: vu.ID,
Samples: vu.Samples,
Iteration: vu.Iteration,
Tags: vu.Runner.Bundle.Options.RunTags.CloneTags(),
Group: r.defaultGroup,
}
vu.Runtime.Set("console", common.Bind(vu.Runtime, vu.Console, vu.Context))
// This is here mostly so if someone tries they get a nice message
// instead of "Value is not an object: undefined ..."
common.BindToGlobal(vu.Runtime, map[string]interface{}{
"open": func() {
common.Throw(vu.Runtime, errors.New(openCantBeUsedOutsideInitContextMsg))
},
})
return vu, nil
}
// Setup runs the setup function if there is one and sets the setupData to the returned value
func (r *Runner) Setup(ctx context.Context, out chan<- stats.SampleContainer) error {
setupCtx, setupCancel := context.WithTimeout(ctx, r.getTimeoutFor(consts.SetupFn))
defer setupCancel()
v, err := r.runPart(setupCtx, out, consts.SetupFn, nil)
if err != nil {
return err
}
// 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 fmt.Errorf("error marshaling setup() data to JSON: %w", err)
}
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, r.getTimeoutFor(consts.TeardownFn))
defer teardownCancel()
var data interface{}
if r.setupData != nil {
if err := json.Unmarshal(r.setupData, &data); err != nil {
return fmt.Errorf("error unmarshaling setup data for teardown() from JSON: %w", err)
}
} else {
data = goja.Undefined()
}
_, err := r.runPart(teardownCtx, out, consts.TeardownFn, data)
return err
}
func (r *Runner) GetDefaultGroup() *lib.Group {
return r.defaultGroup
}
func (r *Runner) GetOptions() lib.Options {
return r.Bundle.Options
}
// IsExecutable returns whether the given name is an exported and
// executable function in the script.
func (r *Runner) IsExecutable(name string) bool {
_, exists := r.Bundle.exports[name]
return exists
}
// HandleSummary calls the specified summary callback, if supplied.
func (r *Runner) HandleSummary(ctx context.Context, summary *lib.Summary) (map[string]io.Reader, error) {
summaryDataForJS := summarizeMetricsToObject(summary, r.Bundle.Options)
out := make(chan stats.SampleContainer, 100)
defer close(out)
go func() { // discard all metrics
for range out {
}
}()
vu, err := r.newVU(0, out)
if err != nil {
return nil, err
}
handleSummaryFn := goja.Undefined()
if exported := vu.Runtime.Get("exports").ToObject(vu.Runtime); exported != nil {
fn := exported.Get(consts.HandleSummaryFn)
if _, ok := goja.AssertFunction(fn); ok {
handleSummaryFn = fn
} else if fn != nil {
return nil, fmt.Errorf("exported identfier %s must be a function", consts.HandleSummaryFn)
}
}
ctx = common.WithRuntime(ctx, vu.Runtime)
ctx = lib.WithState(ctx, vu.state)
ctx, cancel := context.WithTimeout(ctx, r.getTimeoutFor(consts.HandleSummaryFn))
defer cancel()
go func() {
<-ctx.Done()
vu.Runtime.Interrupt(context.Canceled)
}()
*vu.Context = ctx
handleSummaryWrapperRaw, err := vu.Runtime.RunString(summaryWrapperLambdaCode)
if err != nil {
return nil, fmt.Errorf("unexpected error while getting the summary wrapper: %w", err)
}
handleSummaryWrapper, ok := goja.AssertFunction(handleSummaryWrapperRaw)
if !ok {
return nil, fmt.Errorf("unexpected error did not get a callable summary wrapper")
}
wrapperArgs := []goja.Value{
handleSummaryFn,
vu.Runtime.ToValue(r.Bundle.RuntimeOptions.SummaryExport.String),
vu.Runtime.ToValue(summaryDataForJS),
vu.Runtime.ToValue(getOldTextSummaryFunc(summary, r.Bundle.Options)), // TODO: remove
}
rawResult, _, _, err := vu.runFn(ctx, false, handleSummaryWrapper, wrapperArgs...)
// TODO: refactor the whole JS runner to avoid copy-pasting these complicated bits...
// 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 context.Canceled in which case we should return it instead
if err, ok := err.(*goja.InterruptedError); ok && rawResult != nil && err.Value() != context.Canceled {
// TODO: silence this error?
return nil, err
}
// otherwise we have timeouted
return nil, lib.NewTimeoutError(consts.HandleSummaryFn, r.getTimeoutFor(consts.HandleSummaryFn))
}
if err != nil {
return nil, fmt.Errorf("unexpected error while generating the summary: %w", err)
}
return getSummaryResult(rawResult)
}
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, r.Logger.Formatter)
if err != nil {
return err
}
r.console = c
}
// FIXME: Resolver probably shouldn't be reset here...
// It's done because the js.Runner is created before the full
// configuration has been processed, at which point we don't have
// access to the DNSConfig, and need to wait for this SetOptions
// call that happens after all config has been assembled.
// We could make DNSConfig part of RuntimeOptions, but that seems
// conceptually wrong since the JS runtime doesn't care about it
// (it needs the actual resolver, not the config), and it would
// require an additional field on Bundle to pass the config through,
// which is arguably worse than this.
if err := r.setResolver(opts.DNS); err != nil {
return err
}
return nil
}
func (r *Runner) setResolver(dns types.DNSConfig) error {
ttl, err := parseTTL(dns.TTL.String)
if err != nil {
return err
}
dnsSel := dns.Select
if !dnsSel.Valid {
dnsSel = types.DefaultDNSConfig().Select
}
dnsPol := dns.Policy
if !dnsPol.Valid {
dnsPol = types.DefaultDNSConfig().Policy
}
r.Resolver = netext.NewResolver(
r.ActualResolver, ttl, dnsSel.DNSSelect, dnsPol.DNSPolicy)
return nil
}
func parseTTL(ttlS string) (time.Duration, error) {
ttl := time.Duration(0)
switch ttlS {
case "inf":
// cache "infinitely"
ttl = time.Hour * 24 * 365
case "0":
// disable cache
case "":
ttlS = types.DefaultDNSConfig().TTL.String
fallthrough
default:
var err error
ttl, err = types.ParseExtendedDuration(ttlS)
if ttl < 0 || err != nil {
return ttl, fmt.Errorf("invalid DNS TTL: %s", ttlS)
}
}
return ttl, 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(0, 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 = common.WithRuntime(ctx, vu.Runtime)
ctx = lib.WithState(ctx, vu.state)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
go func() {
<-ctx.Done()
vu.Runtime.Interrupt(context.Canceled)
}()
*vu.Context = ctx
group, err := r.GetDefaultGroup().Group(name)
if err != nil {
return goja.Undefined(), err
}
if r.Bundle.Options.SystemTags.Has(stats.TagGroup) {
vu.state.Tags["group"] = group.Path
}
vu.state.Group = group
v, _, _, err := vu.runFn(ctx, 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 context.Canceled in which case we should return it instead
if err, ok := err.(*goja.InterruptedError); ok && v != nil && err.Value() != context.Canceled {
// TODO: silence this error?
return v, err
}
// otherwise we have timeouted
return v, lib.NewTimeoutError(name, r.getTimeoutFor(name))
}
return v, err
}
// getTimeoutFor returns the timeout duration for given special script function.
func (r *Runner) getTimeoutFor(stage string) time.Duration {
d := time.Duration(0)
switch stage {
case consts.SetupFn:
return time.Duration(r.Bundle.Options.SetupTimeout.Duration)
case consts.TeardownFn:
return time.Duration(r.Bundle.Options.TeardownTimeout.Duration)
case consts.HandleSummaryFn:
return 2 * time.Minute // TODO: make configurable
}
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
state *lib.State
}
// Verify that interfaces are implemented
var (
_ lib.ActiveVU = &ActiveVU{}
_ lib.InitializedVU = &VU{}
)
// ActiveVU holds a VU and its activation parameters
type ActiveVU struct {
*VU
*lib.VUActivationParams
busy chan struct{}
}
// GetID returns the unique VU ID.
func (u *VU) GetID() int64 {
return u.ID
}
// Activate the VU so it will be able to run code.
func (u *VU) Activate(params *lib.VUActivationParams) lib.ActiveVU {
u.Runtime.ClearInterrupt()
if params.Exec == "" {
params.Exec = consts.DefaultFn
}
// Override the preset global env with any custom env vars
env := make(map[string]string, len(u.env)+len(params.Env))
for key, value := range u.env {
env[key] = value
}
for key, value := range params.Env {
env[key] = value
}
u.Runtime.Set("__ENV", env)
opts := u.Runner.Bundle.Options
// TODO: maybe we can cache the original tags only clone them and add (if any) new tags on top ?
u.state.Tags = opts.RunTags.CloneTags()
for k, v := range params.Tags {
u.state.Tags[k] = v
}
if opts.SystemTags.Has(stats.TagVU) {
u.state.Tags["vu"] = strconv.FormatInt(u.ID, 10)
}
if opts.SystemTags.Has(stats.TagIter) {
u.state.Tags["iter"] = strconv.FormatInt(u.Iteration, 10)
}
if opts.SystemTags.Has(stats.TagGroup) {
u.state.Tags["group"] = u.state.Group.Path
}
if opts.SystemTags.Has(stats.TagScenario) {
u.state.Tags["scenario"] = params.Scenario
}
params.RunContext = common.WithRuntime(params.RunContext, u.Runtime)
params.RunContext = lib.WithState(params.RunContext, u.state)
*u.Context = params.RunContext
avu := &ActiveVU{
VU: u,
VUActivationParams: params,
busy: make(chan struct{}, 1),
}
go func() {
// Wait for the run context to be over
<-params.RunContext.Done()
// Interrupt the JS runtime
u.Runtime.Interrupt(context.Canceled)
// Wait for the VU to stop running, if it was, and prevent it from
// running again for this activation
avu.busy <- struct{}{}
if params.DeactivateCallback != nil {
params.DeactivateCallback(u)
}
}()
return avu
}
// RunOnce runs the configured Exec function once.
func (u *ActiveVU) RunOnce() error {
select {
case <-u.RunContext.Done():
return u.RunContext.Err() // we are done, return
case u.busy <- struct{}{}:
// nothing else can run now, and the VU cannot be deactivated
}
defer func() {
<-u.busy // unlock deactivation again
}()
// 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 fmt.Errorf("error unmarshaling setup data for the iteration from JSON: %w", err)
}
u.setupData = u.Runtime.ToValue(data)
} else {
u.setupData = goja.Undefined()
}
}
fn, ok := u.exports[u.Exec]
if !ok {
// Shouldn't happen; this is validated in cmd.validateScenarioConfig()
panic(fmt.Sprintf("function '%s' not found in exports", u.Exec))
}
// Call the exported function.
_, isFullIteration, totalTime, err := u.runFn(u.RunContext, true, fn, u.setupData)
// If MinIterationDuration is specified and the iteration wasn't canceled
// 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, isDefault bool, fn goja.Callable, args ...goja.Value,
) (v goja.Value, isFullIteration bool, t time.Duration, err error) {
if !u.Runner.Bundle.Options.NoCookiesReset.ValueOrZero() {
u.state.CookieJar, err = cookiejar.New(nil)
if err != nil {
return goja.Undefined(), false, time.Duration(0), err
}
}
opts := &u.Runner.Bundle.Options
if opts.SystemTags.Has(stats.TagIter) {
u.state.Tags["iter"] = strconv.FormatInt(u.Iteration, 10)
}
// TODO: this seems like the wrong place for the iteration incrementation
// also this means that teardown and setup have __ITER defined
// maybe move it to RunOnce ?
u.Runtime.Set("__ITER", u.Iteration)
u.Iteration++
defer func() {
if r := recover(); r != nil {
gojaStack := u.Runtime.CaptureCallStack(20, nil)
err = fmt.Errorf("a panic occurred in VU code but was caught: %s", r)
// TODO figure out how to use PanicLevel without panicing .. this might require changing
// the logger we use see
// https://github.com/sirupsen/logrus/issues/1028
// https://github.com/sirupsen/logrus/issues/993
b := new(bytes.Buffer)
for _, s := range gojaStack {
s.Write(b)
}
u.state.Logger.Log(logrus.ErrorLevel, "panic: ", r, "\n", string(debug.Stack()), "\nGoja stack:\n", b.String())
}
}()
startTime := time.Now()
v, err = fn(goja.Undefined(), args...) // Actually run the JS script
endTime := time.Now()
select {
case <-ctx.Done():
isFullIteration = false
default:
isFullIteration = true
}
if u.Runner.Bundle.Options.NoVUConnectionReuse.Bool {
u.Transport.CloseIdleConnections()
}
u.state.Samples <- u.Dialer.GetTrail(startTime, endTime, isFullIteration, isDefault, stats.NewSampleTags(u.state.Tags))
return v, isFullIteration, endTime.Sub(startTime), err
}