Advanced Examples
You can use multiple scenarios in one script, and these scenarios can be run in sequence or in parallel. Some ways that you can combine scenarios include the following:
- Have different start times to sequence workloads
- Add per-scenario tags and environment variables
- Make scenario-specific thresholds.
- Use multiple scenarios to run different test logic, so that VUs don’t run only the
default
function.
Combine scenarios
With the startTime
property, you can configure your script to start some scenarios later than others.
To sequence your scenarios, you can combine startTime
with the duration options specific to the executor.
(this is easiest to do with executors with set durations, like the arrival-rate executors).
This script has two scenarios, contacts
and news
, which run in sequence:
- At the beginning of the test, k6 starts the
contacts
scenario. 50 VUs try to run as many iterations as possible for 30 seconds. - After 30 seconds, k6 starts the
news
scenario. 50 VUs each try to run 100 iterations in one minute.
Along with startTime
, duration
, and maxDuration
, note the different test logic for each scenario.
import http from 'k6/http';
export const options = {
discardResponseBodies: true,
scenarios: {
contacts: {
executor: 'constant-vus',
exec: 'contacts',
vus: 50,
duration: '30s',
},
news: {
executor: 'per-vu-iterations',
exec: 'news',
vus: 50,
iterations: 100,
startTime: '30s',
maxDuration: '1m',
},
},
};
export function contacts() {
http.get('https://test.k6.io/contacts.php', {
tags: { my_custom_tag: 'contacts' },
});
}
export function news() {
http.get('https://test.k6.io/news.php', { tags: { my_custom_tag: 'news' } });
}
Use different environment variables and tags per scenario.
The previous example sets tags on individual HTTP request metrics. But, you can also set tags per scenario, which applies them to other taggable objects as well.
Note
By default, k6 applies a
scenario
tag to all metrics in each scenario, whose value is the scenario name. You can combine these tags with thresholds, or use them to simplify results filtering.To disable scenario tags, use the
--system-tags
option.
Run multiple scenario functions, with different thresholds
You can also set different thresholds for different scenario functions. To do this:
- Set scenario-specific tags
- Set thresholds for these tags.
This test has 3 scenarios, each with different exec
functions, tags and environment variables, and thresholds:
Run specific scenario via environment variable
k6 runs all scenarios listed in a test script by default. But, with some small code changes and using environment variables, you can tell k6 to only run a specific scenario via the command-line.
The following example shows a test script that uses a SCENARIO
environment variable, if it exists, to choose which scenario to execute:
import http from 'k6/http';
const scenarios = {
my_web_test: {
executor: 'per-vu-iterations',
vus: 1,
iterations: 1,
maxDuration: '30s',
},
my_api_test: {
executor: 'per-vu-iterations',
vus: 1,
iterations: 1,
maxDuration: '30s',
},
};
const { SCENARIO } = __ENV;
export const options = {
// if a scenario is passed via a CLI env variable, then run that scenario. Otherwise, run
// using the pre-configured scenarios above.
scenarios: SCENARIO ? { [SCENARIO]: scenarios[SCENARIO] } : scenarios,
discardResponseBodies: true,
thresholds: {
http_req_duration: ['p(95)<250', 'p(99)<350'],
},
};
export default function () {
const response = http.get('https://test-api.k6.io/public/crocodiles/');
}
Then from the command line, you could run the test script and only execute the my_web_test
scenario by running:
$ SCENARIO=my_web_test k6 run script.js