Important: This documentation is about an older version. It's relevant only to the release noted, many of the features and functions have been updated or replaced. Please view the current version.
The list of k6 modules natively supported in your k6 scripts.
Init context
Before the k6 starts the test logic, code in the init context prepares the script.
A few functions are available only in init context.
For details about the runtime, refer to the Test lifecycle.
k6/execution provides the capability to get information about the current test execution state inside the test script. You can read in your script the execution state during the test execution and change your script logic based on the current state.
The k6/execution module provides the test execution information with the following properties:
The instance object provides information associated with the load generator instance. You can think of it as the current running k6 process, which will likely be a single process if you are running k6 on your local machine. When running a cloud/distributed test with multiple load generator instances, the values of the following properties can differ across instances.
Property
Type
Description
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.
scenario
Meta information and execution details about the current running scenario.
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. 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.
test
Control the test execution.
Property
Type
Description
abort([String])
function
It aborts the test run with the exit code 108, and an optional string parameter can provide an error message. Aborting the test will not prevent the teardown() execution.
options
Object
It returns an object with all the test options as properties. The options’ values are consolidated following the order of precedence and derived if shortcuts have been used. It returns null for properties where the relative option hasn’t been defined.
vu
Meta information and execution details about the current vu.
Property
Type
Description
iterationInInstance
integer
The identifier of the iteration in the current instance for this VU. This is only unique for current VU and this instance (if multiple instances). This keeps being aggregated if a given VU is reused between multiple scenarios.
iterationInScenario
integer
The identifier of the iteration in the current scenario for this VU. This is only unique for current VU and scenario it is currently executing.
idInInstance
integer
The identifier of the VU across the instance. Not unique across multiple instances.
idInTest
integer
The globally unique (across the whole test run) identifier of the VU.
Returned by the http.* methods that generate HTTP requests.
k6/metrics
The metrics module provides functionality to create custom metrics of various types.
All metrics (both the built-in metrics and the custom ones) have a type.
You can optionally tag all values added to a custom metric, which can be useful when analysing the test results.
Create a WebSocket connection, and provides a Socket client to interact with the service. The method blocks the test finalization until the connection is closed.
Call a function with a delay, if the connection is open.
Error codes
The following specific error codes are currently defined:
1000: A generic error that isn’t any of the ones listed below.
1010: A non-TCP network error - this is a place holder there is no error currently known to trigger it.
1020: An invalid URL was specified.
1050: The HTTP request has timed out.
1100: A generic DNS error that isn’t any of the ones listed below.
1101: No IP for the provided host was found.
1110: Blacklisted IP was resolved or a connection to such was tried to be established.
1111: Blacklisted hostname using The Block Hostnames option.
1200: A generic TCP error that isn’t any of the ones listed below.
1201: A “broken pipe” on write - the other side has likely closed the connection.
1202: An unknown TCP error - We got an error that we don’t recognize but it is from the operating system and has errno set on it. The message in error includes the operation(write,read) and the errno, the OS, and the original message of the error.
1210: General TCP dial error.
1211: Dial timeout error - the timeout for the dial was reached.
1212: Dial connection refused - the connection was refused by the other party on dial.
1213: Dial unknown error.
1220: Reset by peer - the connection was reset by the other party, most likely a server.
1300: General TLS error
1310: Unknown authority - the certificate issuer is unknown.