I have a script with multiple scenarios. Each scenario does CRUD operations on a unique content type. I would like to reuse same script to run all scenarios at same time or single scenario as the need be. How to achieve this?
Yes, you can easily do this with k6 environment variables, like this:
import http from 'k6/http';
let scenarios = {
contacts: {
executor: 'constant-vus',
exec: 'contacts',
vus: 50,
duration: '30s',
},
news: {
executor: 'per-vu-iterations',
exec: 'news',
vus: 50,
iterations: 100,
maxDuration: '1m',
},
};
export let options = {
scenarios: {}, // to be set later
// ... whatever other script options you have
}
if (__ENV.scenario) {
// Use just a single scenario if `--env scenario=whatever` is used
options.scenarios[__ENV.scenario] = scenarios[__ENV.scenario];
} else {
// Use all scenrios
options.scenarios = scenarios;
}
export function contacts() {
http.get('https://test.k6.io/contacts.php');
// ...
}
export function news() {
http.get('https://test.k6.io/news.php');
// ...
}
k6 run script.js
will run the test will all of the scenarios enabled, while k6 run --env scenario=contacts script.js
or k6 run --env scenario=news script.js
will run it with just a single scenario.
4 Likes
Feature request:
Where i should put this ‘if’ block ?
ERRO[0001] ReferenceError: scenarios is not defined
at file:///C:/Dev/k6-scenarios.js:73:38(159) hint="script exception"
Ok. i put the ‘if’ block into ‘default function’,
but I am getting an error
let scenario = __ENV.SCENARIO; //this works
options.scenarios[scenario] = my_scenarios[scenario]; //this doesnt
INFO[0001] scenario: "scenario_one" source=console
INFO[0001] my_scenarios: {"executor":"per-vu-iterations","vus":1,"iterations":1,"exec":"scenario_one"} source=console
INFO[0001] options.scenarios: {} source=console
ERRO[0001] TypeError: Cannot assign to property scenario_one of a host object