k6 v0.34.0 introduced the capability for getting information about the current test execution state, from inside of the test's script.
It can be used by importing the k6/execution module.
The module has a default export that has the following keys: scenario, instance and vu, which in themselves expose information specific to the context in which they are being accessed.
All unique identifiers are sequentially generated starting from a base of zero (iterations) or one (VU IDs). In distributed/cloud test runs, the test-wide iteration numbers and VU identifiers are still going to be unique across instances, though there might be gaps in the sequences when, for example, some instances execute faster iterations than others or allocate more VUs mid-test.
|name||string||The assigned name of the running scenario.|
|executor||string||The name of the running Executor type.|
|startTime||integer||The Unix timestamp in milliseconds when the scenario started.|
|progress||float||Percentage in a 0 to 1 interval of the scenario progress.|
|iterationInInstance||integer||The unique and zero-based sequential number of the current iteration in the scenario, across the current instance.|
|iterationInTest||integer||The unique and zero-based sequential number of the current iteration in the scenario, across the entire test. It is unique in all k6 execution modes - in local, cloud and distributed/segmented test runs. However, while every instance will get non-overlapping index values in cloud/distributed tests, they might iterate over them at different speeds, so the values won't be sequential across them.|
|iterationsInterrupted||integer||The number of prematurely interrupted iterations in the current instance.|
|iterationsCompleted||integer||The number of completed iterations in the current instance.|
|vusActive||integer||The number of active VUs.|
|vusInitialized||integer||The number of currently initialized VUs.|
|currentTestRunDuration||float||The time passed from the start of the current test run in milliseconds.|
|iterationInInstance||integer||The identifier of the iteration in the current instance.|
|iterationInScenario||integer||The identifier of the iteration in the current scenario.|
|idInInstance||integer||The identifier of the VU across the instance.|
|idInTest||integer||The globally unique (across the whole test run) identifier of the VU.|
A common use case is getting a unique and incremental index for accessing a dataset's item only once per test.
The scenario.iterationInTest field can be used for this case. Let's show an example:
The startTime property from the scenario object can be used to time operations.
The name property can be used for executing the logic based on which script is currently running.