forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
589 lines (507 loc) · 18 KB
/
options.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
/*
*
* 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 lib
import (
"crypto/tls"
"encoding/json"
"fmt"
"net"
"reflect"
"regexp"
"strings"
"github.com/loadimpact/k6/lib/scheduler"
"github.com/loadimpact/k6/lib/types"
"github.com/loadimpact/k6/stats"
"github.com/pkg/errors"
"gopkg.in/guregu/null.v3"
)
// DefaultSchedulerName is used as the default key/ID of the scheduler config entries
// that were created due to the use of the shortcut execution control options (i.e. duration+vus,
// iterations+vus, or stages)
const DefaultSchedulerName = "default"
// DefaultSummaryTrendStats are the default trend columns shown in the test summary output
// nolint: gochecknoglobals
var DefaultSummaryTrendStats = []string{"avg", "min", "med", "max", "p(90)", "p(95)"}
// Describes a TLS version. Serialised to/from JSON as a string, eg. "tls1.2".
type TLSVersion int
func (v TLSVersion) MarshalJSON() ([]byte, error) {
return []byte(`"` + SupportedTLSVersionsToString[v] + `"`), nil
}
func (v *TLSVersion) UnmarshalJSON(data []byte) error {
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}
if str == "" {
*v = 0
return nil
}
ver, ok := SupportedTLSVersions[str]
if !ok {
return errors.Errorf("unknown TLS version: %s", str)
}
*v = ver
return nil
}
// Fields for TLSVersions. Unmarshalling hack.
type TLSVersionsFields struct {
Min TLSVersion `json:"min"` // Minimum allowed version, 0 = any.
Max TLSVersion `json:"max"` // Maximum allowed version, 0 = any.
}
// Describes a set (min/max) of TLS versions.
type TLSVersions TLSVersionsFields
func (v *TLSVersions) UnmarshalJSON(data []byte) error {
var fields TLSVersionsFields
if err := json.Unmarshal(data, &fields); err != nil {
var ver TLSVersion
if err2 := json.Unmarshal(data, &ver); err2 != nil {
return err
}
fields.Min = ver
fields.Max = ver
}
*v = TLSVersions(fields)
return nil
}
func (v *TLSVersions) isTLS13() bool {
return v.Min == TLSVersion13 || v.Max == TLSVersion13
}
// A list of TLS cipher suites.
// Marshals and unmarshals from a list of names, eg. "TLS_ECDHE_RSA_WITH_RC4_128_SHA".
// BUG: This currently doesn't marshal back to JSON properly!!
type TLSCipherSuites []uint16
func (s *TLSCipherSuites) UnmarshalJSON(data []byte) error {
var suiteNames []string
if err := json.Unmarshal(data, &suiteNames); err != nil {
return err
}
var suiteIDs []uint16
for _, name := range suiteNames {
if suiteID, ok := SupportedTLSCipherSuites[name]; ok {
suiteIDs = append(suiteIDs, suiteID)
} else {
return errors.New("Unknown cipher suite: " + name)
}
}
*s = suiteIDs
return nil
}
// Fields for TLSAuth. Unmarshalling hack.
type TLSAuthFields struct {
// Certificate and key as a PEM-encoded string, including "-----BEGIN CERTIFICATE-----".
Cert string `json:"cert"`
Key string `json:"key"`
// Domains to present the certificate to. May contain wildcards, eg. "*.example.com".
Domains []string `json:"domains"`
}
// Defines a TLS client certificate to present to certain hosts.
type TLSAuth struct {
TLSAuthFields
certificate *tls.Certificate
}
func (c *TLSAuth) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &c.TLSAuthFields); err != nil {
return err
}
if _, err := c.Certificate(); err != nil {
return err
}
return nil
}
func (c *TLSAuth) Certificate() (*tls.Certificate, error) {
if c.certificate == nil {
cert, err := tls.X509KeyPair([]byte(c.Cert), []byte(c.Key))
if err != nil {
return nil, err
}
c.certificate = &cert
}
return c.certificate, nil
}
// IPNet is a wrapper around net.IPNet for JSON unmarshalling
type IPNet net.IPNet
func (ipnet *IPNet) String() string {
return (*net.IPNet)(ipnet).String()
}
// UnmarshalText populates the IPNet from the given CIDR
func (ipnet *IPNet) UnmarshalText(b []byte) error {
newIPNet, err := ParseCIDR(string(b))
if err != nil {
return errors.Wrap(err, "Failed to parse CIDR")
}
*ipnet = *newIPNet
return nil
}
// ParseCIDR creates an IPNet out of a CIDR string
func ParseCIDR(s string) (*IPNet, error) {
_, ipnet, err := net.ParseCIDR(s)
if err != nil {
return nil, err
}
parsedIPNet := IPNet(*ipnet)
return &parsedIPNet, nil
}
// HostnameTrie is a tree-structured list of hostname matches with support
// for wildcards exclusively at the start of the pattern. Items may only
// be inserted and searched. Internationalized hostnames are valid.
type HostnameTrie struct {
children map[rune]*HostnameTrie
}
// Regex description of hostname pattern to enforce blocks by. Global var
// to avoid compilation penalty at runtime.
// Matches against strings composed entirely of letters, numbers, or '.'s
// with an optional wildcard at the start.
//nolint:gochecknoglobals
var legalHostnamePattern *regexp.Regexp = regexp.MustCompile(`^\*?(\pL|[0-9\.])*`)
func legalHostname(s string) error {
if len(legalHostnamePattern.FindString(s)) != len(s) {
return errors.Errorf("invalid hostname pattern %s", s)
}
return nil
}
// UnmarshalJSON forms a HostnameTrie from the provided hostname pattern
// list.
func (t *HostnameTrie) UnmarshalJSON(data []byte) error {
m := make([]string, 0)
if err := json.Unmarshal(data, &m); err != nil {
return err
}
for _, h := range m {
if insertErr := t.Insert(h); insertErr != nil {
return insertErr
}
}
return nil
}
// UnmarshalText forms a HostnameTrie from a comma-delimited list
// of hostname patterns.
func (t *HostnameTrie) UnmarshalText(b []byte) error {
for _, s := range strings.Split(string(b), ",") {
if err := t.Insert(s); err != nil {
return err
}
}
return nil
}
// Insert a hostname pattern into the given HostnameTrie. Returns an error
// if hostname pattern is illegal.
func (t *HostnameTrie) Insert(s string) error {
s = strings.ToLower(s)
if len(s) == 0 {
return nil
}
if err := legalHostname(s); err != nil {
return err
}
// mask creation of the trie by initializing the root here
if t.children == nil {
t.children = make(map[rune]*HostnameTrie)
}
rStr := []rune(s) // need to iterate by runes for intl' names
last := len(rStr) - 1
if c, ok := t.children[rStr[last]]; ok {
return c.Insert(string(rStr[:last]))
}
t.children[rStr[last]] = &HostnameTrie{make(map[rune]*HostnameTrie)}
return t.children[rStr[last]].Insert(string(rStr[:last]))
}
// Contains returns whether s matches a pattern in the HostnameTrie
// along with the matching pattern, if one was found.
func (t *HostnameTrie) Contains(s string) (matchedPattern string, matchFound bool) {
s = strings.ToLower(s)
if len(s) == 0 {
return s, len(t.children) == 0
}
rStr := []rune(s)
last := len(rStr) - 1
if c, ok := t.children[rStr[last]]; ok {
if match, matched := c.Contains(string(rStr[:last])); matched {
return match + string(rStr[last]), true
}
}
if _, wild := t.children['*']; wild {
return "*", true
}
return "", false
}
type Options struct {
// Should the test start in a paused state?
Paused null.Bool `json:"paused" envconfig:"K6_PAUSED"`
// Initial values for VUs, max VUs, duration cap, iteration cap, and stages.
// See the Runner or Executor interfaces for more information.
VUs null.Int `json:"vus" envconfig:"K6_VUS"`
//TODO: deprecate this? or reuse it in the manual control "scheduler"?
VUsMax null.Int `json:"vusMax" envconfig:"K6_VUS_MAX"`
Duration types.NullDuration `json:"duration" envconfig:"K6_DURATION"`
Iterations null.Int `json:"iterations" envconfig:"K6_ITERATIONS"`
Stages []Stage `json:"stages" envconfig:"K6_STAGES"`
Execution scheduler.ConfigMap `json:"execution,omitempty" envconfig:"-"`
// Timeouts for the setup() and teardown() functions
SetupTimeout types.NullDuration `json:"setupTimeout" envconfig:"K6_SETUP_TIMEOUT"`
TeardownTimeout types.NullDuration `json:"teardownTimeout" envconfig:"K6_TEARDOWN_TIMEOUT"`
// Limit HTTP requests per second.
RPS null.Int `json:"rps" envconfig:"K6_RPS"`
// How many HTTP redirects do we follow?
MaxRedirects null.Int `json:"maxRedirects" envconfig:"K6_MAX_REDIRECTS"`
// Default User Agent string for HTTP requests.
UserAgent null.String `json:"userAgent" envconfig:"K6_USER_AGENT"`
// How many batch requests are allowed in parallel, in total and per host?
Batch null.Int `json:"batch" envconfig:"K6_BATCH"`
BatchPerHost null.Int `json:"batchPerHost" envconfig:"K6_BATCH_PER_HOST"`
// Should all HTTP requests and responses be logged (excluding body)?
HTTPDebug null.String `json:"httpDebug" envconfig:"K6_HTTP_DEBUG"`
// Accept invalid or untrusted TLS certificates.
InsecureSkipTLSVerify null.Bool `json:"insecureSkipTLSVerify" envconfig:"K6_INSECURE_SKIP_TLS_VERIFY"`
// Specify TLS versions and cipher suites, and present client certificates.
TLSCipherSuites *TLSCipherSuites `json:"tlsCipherSuites" envconfig:"K6_TLS_CIPHER_SUITES"`
TLSVersion *TLSVersions `json:"tlsVersion" envconfig:"K6_TLS_VERSION"`
TLSAuth []*TLSAuth `json:"tlsAuth" envconfig:"K6_TLSAUTH"`
// Throw warnings (eg. failed HTTP requests) as errors instead of simply logging them.
Throw null.Bool `json:"throw" envconfig:"K6_THROW"`
// Define thresholds; these take the form of 'metric=["snippet1", "snippet2"]'.
// To create a threshold on a derived metric based on tag queries ("submetrics"), create a
// metric on a nonexistent metric named 'real_metric{tagA:valueA,tagB:valueB}'.
Thresholds map[string]stats.Thresholds `json:"thresholds" envconfig:"K6_THRESHOLDS"`
// Blacklist IP ranges that tests may not contact. Mainly useful in hosted setups.
BlacklistIPs []*IPNet `json:"blacklistIPs" envconfig:"K6_BLACKLIST_IPS"`
// Block hostname patterns that tests may not contact.
BlockedHostnames *HostnameTrie `json:"blockHostnames" envconfig:"K6_BLOCK_HOSTNAMES"`
// Hosts overrides dns entries for given hosts
Hosts map[string]net.IP `json:"hosts" envconfig:"K6_HOSTS"`
// Disable keep-alive connections
NoConnectionReuse null.Bool `json:"noConnectionReuse" envconfig:"K6_NO_CONNECTION_REUSE"`
// Do not reuse connections between VU iterations. This gives more realistic results (depending
// on what you're looking for), but you need to raise various kernel limits or you'll get
// errors about running out of file handles or sockets, or being unable to bind addresses.
NoVUConnectionReuse null.Bool `json:"noVUConnectionReuse" envconfig:"K6_NO_VU_CONNECTION_REUSE"`
// MinIterationDuration can be used to force VUs to pause between iterations if a specific
// iteration is shorter than the specified value.
MinIterationDuration types.NullDuration `json:"minIterationDuration" envconfig:"K6_MIN_ITERATION_DURATION"`
// These values are for third party collectors' benefit.
// Can't be set through env vars.
External map[string]json.RawMessage `json:"ext" ignored:"true"`
// Summary trend stats for trend metrics (response times) in CLI output
SummaryTrendStats []string `json:"summaryTrendStats" envconfig:"K6_SUMMARY_TREND_STATS"`
// Summary time unit for summary metrics (response times) in CLI output
SummaryTimeUnit null.String `json:"summaryTimeUnit" envconfig:"K6_SUMMARY_TIME_UNIT"`
// Which system tags to include with metrics ("method", "vu" etc.)
// Use pointer for identifying whether user provide any tag or not.
SystemTags *stats.SystemTagSet `json:"systemTags" envconfig:"K6_SYSTEM_TAGS"`
// Tags to be applied to all samples for this running
RunTags *stats.SampleTags `json:"tags" envconfig:"K6_TAGS"`
// Buffer size of the channel for metric samples; 0 means unbuffered
MetricSamplesBufferSize null.Int `json:"metricSamplesBufferSize" envconfig:"K6_METRIC_SAMPLES_BUFFER_SIZE"`
// Do not reset cookies after a VU iteration
NoCookiesReset null.Bool `json:"noCookiesReset" envconfig:"K6_NO_COOKIES_RESET"`
// Discard Http Responses Body
DiscardResponseBodies null.Bool `json:"discardResponseBodies" envconfig:"K6_DISCARD_RESPONSE_BODIES"`
// Redirect console logging to a file
ConsoleOutput null.String `json:"-" envconfig:"K6_CONSOLE_OUTPUT"`
}
// Returns the result of overwriting any fields with any that are set on the argument.
//
// Example:
// a := Options{VUs: null.IntFrom(10), VUsMax: null.IntFrom(10)}
// b := Options{VUs: null.IntFrom(5)}
// a.Apply(b) // Options{VUs: null.IntFrom(5), VUsMax: null.IntFrom(10)}
func (o Options) Apply(opts Options) Options {
if opts.Paused.Valid {
o.Paused = opts.Paused
}
if opts.VUs.Valid {
o.VUs = opts.VUs
}
if opts.VUsMax.Valid {
o.VUsMax = opts.VUsMax
}
// Specifying duration, iterations, stages, or execution in a "higher" config tier
// will overwrite all of the the previous execution settings (if any) from any
// "lower" config tiers
// Still, if more than one of those options is simultaneously specified in the same
// config tier, they will be preserved, so the validation after we've consolidated
// all of the options can return an error.
if opts.Duration.Valid || opts.Iterations.Valid || opts.Stages != nil || opts.Execution != nil {
//TODO: uncomment this after we start using the new schedulers
/*
o.Duration = types.NewNullDuration(0, false)
o.Iterations = null.NewInt(0, false)
o.Stages = nil
*/
o.Execution = nil
}
if opts.Duration.Valid {
o.Duration = opts.Duration
}
if opts.Iterations.Valid {
o.Iterations = opts.Iterations
}
if opts.Stages != nil {
o.Stages = []Stage{}
for _, s := range opts.Stages {
if s.Duration.Valid {
o.Stages = append(o.Stages, s)
}
}
}
// o.Execution can also be populated by the duration/iterations/stages config shortcuts, but
// that happens after the configuration from the different sources is consolidated. It can't
// happen here, because something like `K6_ITERATIONS=10 k6 run --vus 5 script.js` wont't
// work correctly at this level.
if opts.Execution != nil {
o.Execution = opts.Execution
}
if opts.SetupTimeout.Valid {
o.SetupTimeout = opts.SetupTimeout
}
if opts.TeardownTimeout.Valid {
o.TeardownTimeout = opts.TeardownTimeout
}
if opts.RPS.Valid {
o.RPS = opts.RPS
}
if opts.MaxRedirects.Valid {
o.MaxRedirects = opts.MaxRedirects
}
if opts.UserAgent.Valid {
o.UserAgent = opts.UserAgent
}
if opts.Batch.Valid {
o.Batch = opts.Batch
}
if opts.BatchPerHost.Valid {
o.BatchPerHost = opts.BatchPerHost
}
if opts.HTTPDebug.Valid {
o.HTTPDebug = opts.HTTPDebug
}
if opts.InsecureSkipTLSVerify.Valid {
o.InsecureSkipTLSVerify = opts.InsecureSkipTLSVerify
}
if opts.TLSCipherSuites != nil {
o.TLSCipherSuites = opts.TLSCipherSuites
}
if opts.TLSVersion != nil {
o.TLSVersion = opts.TLSVersion
if o.TLSVersion.isTLS13() {
enableTLS13()
}
}
if opts.TLSAuth != nil {
o.TLSAuth = opts.TLSAuth
}
if opts.Throw.Valid {
o.Throw = opts.Throw
}
if opts.Thresholds != nil {
o.Thresholds = opts.Thresholds
}
if opts.BlacklistIPs != nil {
o.BlacklistIPs = opts.BlacklistIPs
}
if opts.BlockedHostnames != nil {
o.BlockedHostnames = opts.BlockedHostnames
}
if opts.Hosts != nil {
o.Hosts = opts.Hosts
}
if opts.NoConnectionReuse.Valid {
o.NoConnectionReuse = opts.NoConnectionReuse
}
if opts.NoVUConnectionReuse.Valid {
o.NoVUConnectionReuse = opts.NoVUConnectionReuse
}
if opts.MinIterationDuration.Valid {
o.MinIterationDuration = opts.MinIterationDuration
}
if opts.NoCookiesReset.Valid {
o.NoCookiesReset = opts.NoCookiesReset
}
if opts.External != nil {
o.External = opts.External
}
if opts.SummaryTrendStats != nil {
o.SummaryTrendStats = opts.SummaryTrendStats
}
if opts.SummaryTimeUnit.Valid {
o.SummaryTimeUnit = opts.SummaryTimeUnit
}
if opts.SystemTags != nil {
o.SystemTags = opts.SystemTags
}
if !opts.RunTags.IsEmpty() {
o.RunTags = opts.RunTags
}
if opts.MetricSamplesBufferSize.Valid {
o.MetricSamplesBufferSize = opts.MetricSamplesBufferSize
}
if opts.DiscardResponseBodies.Valid {
o.DiscardResponseBodies = opts.DiscardResponseBodies
}
if opts.ConsoleOutput.Valid {
o.ConsoleOutput = opts.ConsoleOutput
}
return o
}
// Validate checks if all of the specified options make sense
func (o Options) Validate() []error {
//TODO: validate all of the other options... that we should have already been validating...
//TODO: maybe integrate an external validation lib: https://github.com/avelino/awesome-go#validation
return o.Execution.Validate()
}
// ForEachSpecified enumerates all struct fields and calls the supplied function with each
// element that is valid. It panics for any unfamiliar or unexpected fields, so make sure
// new fields in Options are accounted for.
func (o Options) ForEachSpecified(structTag string, callback func(key string, value interface{})) {
structType := reflect.TypeOf(o)
structVal := reflect.ValueOf(o)
for i := 0; i < structType.NumField(); i++ {
fieldType := structType.Field(i)
fieldVal := structVal.Field(i)
value := fieldVal.Interface()
shouldCall := false
switch fieldType.Type.Kind() {
case reflect.Struct:
// Unpack any guregu/null values
shouldCall = fieldVal.FieldByName("Valid").Bool()
valOrZero := fieldVal.MethodByName("ValueOrZero")
if shouldCall && valOrZero.IsValid() {
value = valOrZero.Call([]reflect.Value{})[0].Interface()
if v, ok := value.(types.Duration); ok {
value = v.String()
}
}
case reflect.Slice:
shouldCall = fieldVal.Len() > 0
case reflect.Map:
shouldCall = fieldVal.Len() > 0
case reflect.Ptr:
shouldCall = !fieldVal.IsNil()
default:
panic(fmt.Sprintf("Unknown Options field %#v", fieldType))
}
if shouldCall {
key, ok := fieldType.Tag.Lookup(structTag)
if !ok {
key = fieldType.Name
}
callback(key, value)
}
}
}