k6

A modern load testing tool, using Go and JavaScript

"like unit testing, for performance"

k6 is a modern load testing tool, building on Load Impact's years of experience. It provides a clean, approachable JavaScript scripting API, distributed and cloud execution, and orchestration via a REST API.

Get Started    Guides

Environment variables

k6 and environment variables

There are two ways system environment variables can be used with k6:

  • You can access any environment variables from your k6 script code, and use this to supply your VUs with configuration information

  • There are also a couple of environment variables that k6 will automatically try to read upon startup, and which will affect its behaviour

Accessing environment variables from a script

Often times scripts will only need minor tweaking to be run in different contexts. Rather than having to create several separate scripts for these different contexts or environments, you can use environment variables to make parts of your script tweakable.

In k6, the environment variables are exposed through a global __ENV variable, a JS object. The source of the environment variables can be twofold. They could come from the local system and/or be explicitly passed to k6 using one or more -e NAME=VALUE CLI flags.

The primary difference between the two is that only k6 run passes the actual system environment variables to the script code by default, while k6 archive, k6 cloud and k6 inspect do not. So unless you explicitly specify --include-system-env-vars, only the variables passed using the -e CLI flag will be persisted when creating an archive (k6 archive script.js). You can also disable the default passing of system environment variables when running scripts by using --include-system-env-vars=false.

An environment variable could for example be specified like this on the command line:

MY_HOSTNAME=test.loadimpact.com k6 run script.js
c:\k6> set MY_HOSTNAME="test.loadimpact.com"
c:\k6> k6 run script.js
c:\k6> $env:MY_HOSTNAME = "test.loadimpact.com"
c:\k6> k6 run script.js

or using an -e CLI flag (which will be the same for all platforms):

k6 run -e MY_HOSTNAME=test.loadimpact.com script.js

The environment variable could then be used as follows in a script:

import { check, sleep } from "k6";
import http from "k6/http";

export default function() {
    var r = http.get(`http://${__ENV.MY_HOSTNAME}/`);
    check(r, {
        "status is 200": (r) => r.status === 200
    });
    sleep(5);
}

Environment variables specified with the -e CLI flag take precedence over (overwrite) actual system environment variables with the same name.

Environment variables k6 will read automatically

k6 will also try to read some specific environment variables that the user can set in order to change the behaviour of k6:

Name
Description

K6_CLOUD_HOST

A URL to connect to, when the --out=cloud results output option is specified.

K6_CLOUD_TOKEN

An authentication token to use in API calls to the cloud service, when the --cloud results output option is specified.

K6_NO_USAGE_REPORT

Define this in order to disable usage reports.

K6_OUT

Specify results output, same as --out command line option.

Environment variables


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.