-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
runner.go
422 lines (363 loc) · 11.2 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
/*
*
* 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"
"time"
"github.com/dop251/goja"
"github.com/loadimpact/k6/js/common"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/netext"
"github.com/loadimpact/k6/stats"
"github.com/oxtoacart/bpool"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/viki-org/dnscache"
"golang.org/x/net/http2"
"golang.org/x/time/rate"
)
var errInterrupt = errors.New("context cancelled")
// Ensure Runner implements the lib.Runner interface
var _ lib.Runner = &Runner{}
type Runner struct {
Bundle *Bundle
Logger *log.Logger
defaultGroup *lib.Group
BaseDialer net.Dialer
Resolver *dnscache.Resolver
RPSLimit *rate.Limiter
setupData interface{}
}
func New(src *lib.SourceData, fs afero.Fs, rtOpts lib.RuntimeOptions) (*Runner, error) {
bundle, err := NewBundle(src, fs, 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: log.StandardLogger(),
defaultGroup: defaultGroup,
BaseDialer: net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
},
Resolver: dnscache.New(0),
}
r.SetOptions(r.Bundle.Options)
return r, nil
}
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,
HTTPTransport: netext.NewHTTPTransport(transport),
Dialer: dialer,
CookieJar: cookieJar,
TLSConfig: tlsConfig,
Console: NewConsole(),
BPool: bpool.NewBufferPool(100),
Samples: samplesOut,
}
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, "setup", nil)
if err != nil {
return errors.Wrap(err, "setup")
}
data, err := json.Marshal(v.Export())
if err != nil {
return errors.Wrap(err, "setup")
}
return json.Unmarshal(data, &r.setupData)
}
// GetSetupData returns the setup data if Setup() was specified and executed, nil otherwise
func (r *Runner) GetSetupData() interface{} {
return r.setupData
}
// SetSetupData saves the externally supplied setup data in the runner, so it can be used in VUs
func (r *Runner) SetSetupData(data interface{}) {
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()
_, err := r.runPart(teardownCtx, out, "teardown", r.setupData)
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) {
r.Bundle.Options = opts
r.RPSLimit = nil
if rps := opts.RPS; rps.Valid {
r.RPSLimit = rate.NewLimiter(rate.Limit(rps.Int64), 1)
}
}
// 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)
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, fn, vu.Runtime.ToValue(arg))
cancel()
return v, err
}
type VU struct {
BundleInstance
Runner *Runner
HTTPTransport *netext.HTTPTransport
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
}
// 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 {
// 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
go func() {
select {
case <-interCtx.Done():
case <-ctx.Done():
u.Runtime.Interrupt(errInterrupt)
}
}()
}
// Lazily JS-ify setupData on first run. This is lightweight enough that we can get away with
// it, and alleviates a problem where setupData wouldn't get populated properly if NewVU() was
// called before Setup(), which is hard to avoid with how the Executor works w/o complicating
// the local executor further by deferring SetVUsMax() calls to within the Run() function.
if u.setupData == nil && u.Runner.setupData != nil {
u.setupData = u.Runtime.ToValue(u.Runner.setupData)
}
// Call the default function.
_, _, err := u.runFn(ctx, u.Runner.defaultGroup, u.Default, u.setupData)
return err
}
func (u *VU) runFn(ctx context.Context, group *lib.Group, fn goja.Callable, args ...goja.Value) (goja.Value, *common.State, error) {
cookieJar, err := cookiejar.New(nil)
if err != nil {
return goja.Undefined(), nil, err
}
if u.Runner.Bundle.Options.NoCookiesReset.Valid && u.Runner.Bundle.Options.NoCookiesReset.Bool {
cookieJar = u.CookieJar
}
state := &common.State{
Logger: u.Runner.Logger,
Options: u.Runner.Bundle.Options,
Group: group,
HTTPTransport: u.HTTPTransport,
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 = common.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()
tags := state.Options.RunTags.CloneTags()
if state.Options.SystemTags["vu"] {
tags["vu"] = strconv.FormatInt(u.ID, 10)
}
if state.Options.SystemTags["iter"] {
tags["iter"] = strconv.FormatInt(iter, 10)
}
if state.Options.SystemTags["group"] {
tags["group"] = group.Path
}
if u.Runner.Bundle.Options.NoVUConnectionReuse.Bool {
u.HTTPTransport.CloseIdleConnections()
}
var isFullIteration bool
select {
case <-ctx.Done():
isFullIteration = false
default:
isFullIteration = true
}
state.Samples <- u.Dialer.GetTrail(startTime, endTime, isFullIteration, stats.IntoSampleTags(&tags))
return v, state, err
}