Skip to content

Commit

Permalink
Merge pull request #681 from mohanprasaths/feature/613
Browse files Browse the repository at this point in the history
Options will be passed as ENV_variables from command line
  • Loading branch information
na-- authored Jul 13, 2018
2 parents 77152a3 + e794951 commit c7b2516
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 29 deletions.
1 change: 1 addition & 0 deletions js/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func (r *Runner) newVU(samplesOut chan<- stats.SampleContainer) (*VU, error) {
Samples: samplesOut,
}
vu.Runtime.Set("console", common.Bind(vu.Runtime, vu.Console, vu.Context))
vu.Runtime.Set("options", r.GetOptions())
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"))
Expand Down
31 changes: 31 additions & 0 deletions js/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/metrics"
"github.com/loadimpact/k6/lib/testutils"
"github.com/loadimpact/k6/lib/types"
"github.com/loadimpact/k6/stats"
logtest "github.com/sirupsen/logrus/hooks/test"
"github.com/spf13/afero"
Expand Down Expand Up @@ -122,6 +123,36 @@ func TestRunnerOptions(t *testing.T) {
}
}

func TestOptions(t *testing.T) {
fs := afero.NewMemMapFs()

src := &lib.SourceData{
Filename: "/script.js",
Data: []byte(`
export let options = { setupTimeout: "1s" };
export default function() { };
`),
}

r1, err := New(src, fs, lib.RuntimeOptions{IncludeSystemEnvVars: null.BoolFrom(true), Env: map[string]string{"K6_SETUPTIMEOUT": "5s"}})
if !assert.NoError(t, err) {
return
}
r1.SetOptions(lib.Options{SetupTimeout: types.NullDurationFrom(time.Duration(3) * time.Second)})

r2, err := NewFromArchive(r1.MakeArchive(), lib.RuntimeOptions{})
if !assert.NoError(t, err) {
return
}

testdata := map[string]*Runner{"Source": r1, "Archive": r2}
for name, r := range testdata {
t.Run(name, func(t *testing.T) {
assert.Equal(t, lib.Options{SetupTimeout: types.NullDurationFrom(time.Duration(3) * time.Second)}, r.GetOptions())
})
}
}

func TestSetupTeardown(t *testing.T) {
r1, err := New(&lib.SourceData{
Filename: "/script.js",
Expand Down
58 changes: 29 additions & 29 deletions lib/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,80 +187,80 @@ type Options struct {

// 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"`
VUs null.Int `json:"vus" envconfig:"vus" js:"vus"`
VUsMax null.Int `json:"vusMax" envconfig:"vus_max" js:"vusMax"`
Duration types.NullDuration `json:"duration" envconfig:"duration" js:"duration"`
Iterations null.Int `json:"iterations" envconfig:"iterations" js:"iterations"`
Stages []Stage `json:"stages" envconfig:"stages" js:"stages"`

// Timeouts for the setup() and teardown() functions
SetupTimeout types.NullDuration `json:"setupTimeout" envconfig:"setup_timeout"`
TeardownTimeout types.NullDuration `json:"teardownTimeout" envconfig:"teardown_timeout"`
SetupTimeout types.NullDuration `json:"setupTimeout" envconfig:"setup_timeout" js:"setupTimeout"`
TeardownTimeout types.NullDuration `json:"teardownTimeout" envconfig:"teardown_timeout" js:"teardownTimeout"`

// Limit HTTP requests per second.
RPS null.Int `json:"rps" envconfig:"rps"`
RPS null.Int `json:"rps" envconfig:"rps" js:"rps"`

// How many HTTP redirects do we follow?
MaxRedirects null.Int `json:"maxRedirects" envconfig:"max_redirects"`
MaxRedirects null.Int `json:"maxRedirects" envconfig:"max_redirects" js:"maxRedirects"`

// Default User Agent string for HTTP requests.
UserAgent null.String `json:"userAgent" envconfig:"user_agent"`
UserAgent null.String `json:"userAgent" envconfig:"user_agent" js:"userAgent"`

// 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"`
Batch null.Int `json:"batch" envconfig:"batch" js:"batch"`
BatchPerHost null.Int `json:"batchPerHost" envconfig:"batch_per_host" js:"batchPerHost"`

// Should all HTTP requests and responses be logged (excluding body)?
HttpDebug null.String `json:"httpDebug" envconfig:"http_debug"`
HttpDebug null.String `json:"httpDebug" envconfig:"http_debug" js:"httpDebug"`

// Accept invalid or untrusted TLS certificates.
InsecureSkipTLSVerify null.Bool `json:"insecureSkipTLSVerify" envconfig:"insecure_skip_tls_verify"`
InsecureSkipTLSVerify null.Bool `json:"insecureSkipTLSVerify" envconfig:"insecure_skip_tls_verify" js:"insecureSkipTLSVerify"`

// 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"`
TLSCipherSuites *TLSCipherSuites `json:"tlsCipherSuites" envconfig:"tls_cipher_suites" js:"tlsCipherSuites"`
TLSVersion *TLSVersions `json:"tlsVersion" envconfig:"tls_version" js:"tlsVersion"`
TLSAuth []*TLSAuth `json:"tlsAuth" envconfig:"tlsauth" js:"tlsAuth"`

// Throw warnings (eg. failed HTTP requests) as errors instead of simply logging them.
Throw null.Bool `json:"throw" envconfig:"throw"`
Throw null.Bool `json:"throw" envconfig:"throw" js:"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"`
Thresholds map[string]stats.Thresholds `json:"thresholds" envconfig:"thresholds" js:"thresholds"`

// Blacklist IP ranges that tests may not contact. Mainly useful in hosted setups.
BlacklistIPs []*net.IPNet `json:"blacklistIPs" envconfig:"blacklist_ips"`
BlacklistIPs []*net.IPNet `json:"blacklistIPs" envconfig:"blacklist_ips" js:"blacklistIPs"`

// Hosts overrides dns entries for given hosts
Hosts map[string]net.IP `json:"hosts" envconfig:"hosts"`
Hosts map[string]net.IP `json:"hosts" envconfig:"hosts" js:"hosts"`

// Disable keep-alive connections
NoConnectionReuse null.Bool `json:"noConnectionReuse" envconfig:"no_connection_reuse"`
NoConnectionReuse null.Bool `json:"noConnectionReuse" envconfig:"no_connection_reuse" js:"noConnectionReuse"`

// 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"`
NoVUConnectionReuse null.Bool `json:"noVUConnectionReuse" envconfig:"no_vu_connection_reuse" js:"noVUConnectionReuse"`

// These values are for third party collectors' benefit.
// Can't be set through env vars.
External map[string]json.RawMessage `json:"ext" ignored:"true"`
External map[string]json.RawMessage `json:"ext" ignored:"true" js:"ext"`

// Summary trend stats for trend metrics (response times) in CLI output
SummaryTrendStats []string `json:"summaryTrendStats" envconfig:"summary_trend_stats"`
SummaryTrendStats []string `json:"summaryTrendStats" envconfig:"summary_trend_stats" js:"summaryTrendStats"`

// Summary time unit for summary metrics (response times) in CLI output
SummaryTimeUnit null.String `json:"summaryTimeUnit" envconfig:"summary_time_unit"`
SummaryTimeUnit null.String `json:"summaryTimeUnit" envconfig:"summary_time_unit" js:"summaryTimeUnit"`

// Which system tags to include with metrics ("method", "vu" etc.)
SystemTags TagSet `json:"systemTags" envconfig:"system_tags"`
SystemTags TagSet `json:"systemTags" envconfig:"system_tags" js:"systemTags"`

// Tags to be applied to all samples for this running
RunTags *stats.SampleTags `json:"tags" envconfig:"tags"`
RunTags *stats.SampleTags `json:"tags" envconfig:"tags" js:"tags"`

// Buffer size of the channel for metric samples; 0 means unbuffered
MetricSamplesBufferSize null.Int `json:"metricSamplesBufferSize" envconfig:"metric_samples_buffer_size"`
MetricSamplesBufferSize null.Int `json:"metricSamplesBufferSize" envconfig:"metric_samples_buffer_size" js:"metricSamplesBufferSize"`
}

// Returns the result of overwriting any fields with any that are set on the argument.
Expand Down

0 comments on commit c7b2516

Please sign in to comment.