Menu
DocumentationGrafana k6Using k6Execution context variables
Open source

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.

JavaScript
import exec from 'k6/execution';

export default function () {
  console.log(`Execution context

Instance info
-------------
Vus active: ${exec.instance.vusActive}
Iterations completed: ${exec.instance.iterationsCompleted}
Iterations interrupted:  ${exec.instance.iterationsInterrupted}
Iterations completed:  ${exec.instance.iterationsCompleted}
Iterations active:  ${exec.instance.vusActive}
Initialized vus:  ${exec.instance.vusInitialized}
Time passed from start of run(ms):  ${exec.instance.currentTestRunDuration}

Scenario info
-------------
Name of the running scenario: ${exec.scenario.name}
Executor type: ${exec.scenario.executor}
Scenario start timestamp: ${exec.scenario.startTime}
Percenatage complete: ${exec.scenario.progress}
Iteration in instance: ${exec.scenario.iterationInInstance}
Iteration in test: ${exec.scenario.iterationInTest}

Test info
---------
All test options: ${exec.test.options}

VU info
-------
Iteration id: ${exec.vu.iterationInInstance}
Iteration in scenario: ${exec.vu.iterationInScenario}
VU ID in instance: ${exec.vu.idInInstance}
VU ID in test: ${exec.vu.idInTest}
VU tags: ${exec.vu.tags}`);
}

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

Examples and use cases

Grafana Cloud k6 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.