-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
options.go
416 lines (363 loc) · 12.5 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
/*
*
* 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"
"github.com/loadimpact/k6/lib/types"
"github.com/loadimpact/k6/stats"
"github.com/pkg/errors"
"gopkg.in/guregu/null.v3"
)
// DefaultSystemTagList includes all of the system tags emitted with metrics by default.
// Other tags that are not enabled by default include: iter, vu, ocsp_status, ip
var DefaultSystemTagList = []string{
"proto", "subproto", "status", "method", "url", "name", "group", "check", "error", "tls_version",
}
// TagSet is a string to bool map (for lookup efficiency) that is used to keep track
// which system tags should be included with with metrics.
type TagSet map[string]bool
// GetTagSet converts a the passed string tag names into the expected string to bool map.
func GetTagSet(tags ...string) TagSet {
result := TagSet{}
for _, tag := range tags {
result[tag] = true
}
return result
}
// MarshalJSON converts the tags map to a list (JS array).
func (t TagSet) MarshalJSON() ([]byte, error) {
var tags []string
for tag := range t {
tags = append(tags, tag)
}
return json.Marshal(tags)
}
// UnmarshalJSON converts the tag list back to a the expected set (string to bool map).
func (t *TagSet) UnmarshalJSON(data []byte) error {
var tags []string
if err := json.Unmarshal(data, &tags); err != nil {
return err
}
if len(tags) != 0 {
*t = GetTagSet(tags...)
}
return nil
}
// 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
}
// 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
}
type Options struct {
// Should the test start in a paused state?
Paused null.Bool `json:"paused" envconfig:"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:"vus"`
VUsMax null.Int `json:"vusMax" envconfig:"vus_max"`
Duration types.NullDuration `json:"duration" envconfig:"duration"`
Iterations null.Int `json:"iterations" envconfig:"iterations"`
Stages []Stage `json:"stages" envconfig:"stages"`
// Timeouts for the setup() and teardown() functions
SetupTimeout types.NullDuration `json:"setupTimeout" envconfig:"setup_timeout"`
TeardownTimeout types.NullDuration `json:"teardownTimeout" envconfig:"teardown_timeout"`
// Limit HTTP requests per second.
RPS null.Int `json:"rps" envconfig:"rps"`
// How many HTTP redirects do we follow?
MaxRedirects null.Int `json:"maxRedirects" envconfig:"max_redirects"`
// Default User Agent string for HTTP requests.
UserAgent null.String `json:"userAgent" envconfig:"user_agent"`
// How many batch requests are allowed in parallel, in total and per host?
Batch null.Int `json:"batch" envconfig:"batch"`
BatchPerHost null.Int `json:"batchPerHost" envconfig:"batch_per_host"`
// Should all HTTP requests and responses be logged (excluding body)?
HttpDebug null.String `json:"httpDebug" envconfig:"http_debug"`
// Accept invalid or untrusted TLS certificates.
InsecureSkipTLSVerify null.Bool `json:"insecureSkipTLSVerify" envconfig:"insecure_skip_tls_verify"`
// Specify TLS versions and cipher suites, and present client certificates.
TLSCipherSuites *TLSCipherSuites `json:"tlsCipherSuites" envconfig:"tls_cipher_suites"`
TLSVersion *TLSVersions `json:"tlsVersion" envconfig:"tls_version"`
TLSAuth []*TLSAuth `json:"tlsAuth" envconfig:"tlsauth"`
// Throw warnings (eg. failed HTTP requests) as errors instead of simply logging them.
Throw null.Bool `json:"throw" envconfig:"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:"thresholds"`
// Blacklist IP ranges that tests may not contact. Mainly useful in hosted setups.
BlacklistIPs []*net.IPNet `json:"blacklistIPs" envconfig:"blacklist_ips"`
// Hosts overrides dns entries for given hosts
Hosts map[string]net.IP `json:"hosts" envconfig:"hosts"`
// Disable keep-alive connections
NoConnectionReuse null.Bool `json:"noConnectionReuse" envconfig:"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:"no_vu_connection_reuse"`
// 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:"summary_trend_stats"`
// Summary time unit for summary metrics (response times) in CLI output
SummaryTimeUnit null.String `json:"summaryTimeUnit" envconfig:"summary_time_unit"`
// Which system tags to include with metrics ("method", "vu" etc.)
SystemTags TagSet `json:"systemTags" envconfig:"system_tags"`
// Tags to be applied to all samples for this running
RunTags *stats.SampleTags `json:"tags" envconfig:"tags"`
// Buffer size of the channel for metric samples; 0 means unbuffered
MetricSamplesBufferSize null.Int `json:"metricSamplesBufferSize" envconfig:"metric_samples_buffer_size"`
// Do not reset cookies after a VU iteration
NoCookiesReset null.Bool `json:"noCookiesReset" envconfig:"no_cookies_reset"`
// Discard Http Responses Body
DiscardResponseBodies null.Bool `json:"discardResponseBodies" envconfig:"discard_response_bodies"`
}
// 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
}
if opts.Duration.Valid {
o.Duration = opts.Duration
}
if opts.Iterations.Valid {
o.Iterations = opts.Iterations
}
if len(opts.Stages) > 0 {
for _, s := range opts.Stages {
if s.Duration.Valid {
o.Stages = append(o.Stages, s)
}
}
}
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 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.Hosts != nil {
o.Hosts = opts.Hosts
}
if opts.NoConnectionReuse.Valid {
o.NoConnectionReuse = opts.NoConnectionReuse
}
if opts.NoVUConnectionReuse.Valid {
o.NoVUConnectionReuse = opts.NoVUConnectionReuse
}
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
}
return o
}
// ForEachValid 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) ForEachValid(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)
shouldCall := false
switch fieldType.Type.Kind() {
case reflect.Struct:
shouldCall = fieldVal.FieldByName("Valid").Bool()
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, fieldVal.Interface())
}
}
}