No results for

Powered byAlgolia

Execution context variables

In some cases, it's really useful to have information about the script's current test-execution state. For example, you might want to

  • Have different VUs run different test logic
  • Use different data for each VU and iteration
  • Figure out the stage that a test is in

To solve these issues, you can use execution context variables.

k6/execution

The k6/execution module exposes details about the current execution state, such as the name of the currently executed scenario, how many VUs are currently active, and more. The module provides test-execution information via three properties:

PropertyMeta-information and execution details about
instanceThe current running k6 instance
scenarioThe current running scenario
vuThe current VU and iteration

k6 v0.34.0 introduced the k6/execution module. If you are using a version k6 that does not have this module, refer to the __VU and __ITER section.

Example: log all context variables

If you want to experiment with what each context variable looks like as a test runs, you can copy this template literal into one of your test scripts.

Note that this omits the abort variable, since that function would abort the test.

Log all execution variables
1import exec from 'k6/execution';
2
3export default function () {
4 console.log(`Execution context
5
6Instance info
7-------------
8Vus active: ${exec.instance.vusActive}
9Iterations completed: ${exec.instance.iterationsCompleted}
10Iterations interrupted: ${exec.instance.iterationsInterrupted}
11Iterations completed: ${exec.instance.iterationsCompleted}
12Iterations active: ${exec.instance.vusActive}
13Initialized vus: ${exec.instance.vusInitialized}
14Time passed from start of run(ms): ${exec.instance.currentTestRunDuration}
15
16Scenario info
17-------------
18Name of the running scenario: ${exec.scenario.name}
19Executor type: ${exec.scenario.executor}
20Scenario start timestamp: ${exec.scenario.startTime}
21Percenatage complete: ${exec.scenario.progress}
22Iteration in instance: ${exec.scenario.iterationInInstance}
23Iteration in test: ${exec.scenario.iterationInTest}
24
25Test info
26---------
27All test options: ${exec.test.options}
28
29VU info
30-------
31Iteration id: ${exec.vu.iterationInInstance}
32Iteration in scenario: ${exec.vu.iterationInScenario}
33VU ID in instance: ${exec.vu.idInInstance}
34VU ID in test: ${exec.vu.idInTest}
35VU tags: ${exec.vu.tags}`);
36}

For detailed reference, refer to the k6/execution module.

Examples and use cases

_VU and _ITER (discouraged)

⚠️ __VU and __ITER are both global variables with execution-context information that k6 makes available to the test script.

__ITER

A numeric counter with the current iteration number for a specific VU. Zero-based.

__VU

Current VU number in use. k6 assigns the value incrementally for each new VU instance, starting from one. The variable is 0 when executing the setup and teardown functions.

Running in k6 Cloud

When you run tests in k6 Cloud, the __VU value is per server/load generator. Read the details in the cloud docs.

Examples

1import http from 'k6/http';
2import { sleep } from 'k6';
3export default function () {
4 http.get('http://test.k6.io');
5 console.log(`VU: ${__VU} - ITER: ${__ITER}`);
6 sleep(1);
7}

You can use execution-context variables to configure different test behaviors and parameterizations. A typical use case is a load test that simulates different users performing a login flow.

1import http from 'k6/http';
2import { sleep } from 'k6';
3export default function () {
4 const email = `user+${__VU}@mail.com`;
5 const payload = JSON.stringify({ email: email, password: 'test' });
6 const params = { headers: { 'Content-Type': 'application/json' } };
7 http.post('http://test.k6.io/login', payload, params);
8 console.log(email);
9 // .. continue the user flow
10 sleep(1);
11}

k6 Cloud environment variables

If you run tests in k6 Cloud, you have additional environment variables that tell you the server, load zone, and distribution of the currently running test. Read about cloud environment variables.