No results for ""

Powered byAlgolia

Cloud tests from the CLI

suggest edits

Running tests within the web app is helpful when getting a feel for the tool or building a proof of concept. However, many users will find great flexibility when using k6 to trigger cloud tests from the command line.

Reasons for triggering cloud tests from the k6 CLI include:

  • Storing test scripts in local version control.
  • Modularization of scripts for collaboration and easier maintenance.
  • Preference to work in your local environment.
  • Integrating testing in CI/CD pipelines.

Instructions

  1. First, you need to have a k6 Cloud account. If you don't have one, sign up here and get 50 cloud tests with the Free Trial.

  2. Install k6 using the instructions here.

  3. Authenticate to k6 Cloud from the CLI. Log in using your username and password or your API token.

    $ k6 login cloud
    $ k6 login cloud --token <YOUR_K6_CLOUD_API_TOKEN>

    k6 login stores your API Token in a local config file to authenticate to k6 Cloud when running cloud commands.

  4. Run your test in the cloud.

    $ k6 cloud script.js
    # Note the difference in specifying the `K6_CLOUD_TOKEN` environment variable
    # using the `docker run -e` option.
    
    $ docker run -i -e K6_CLOUD_TOKEN=<API_TOKEN> loadimpact/k6 cloud - <script.js
    
    # When passing the script via stdin there is no way for the containerized k6 process
    
    # to get the script filename, which is required for cloud execution. To solve this issue:
    
    # a) specify the `options.ext.loadimpact.name` property in the script:
    
    # export let options = {
    
    # ext: {
    
    # loadimpact: {
    
    # name: 'My awesome test',
    
    # },
    
    # },
    
    # };
    
    # b) mount the local filesystem as a Docker volume:
    
    $ docker run -i -e ... -v "$PWD/script.js:/script.js" loadimpact/k6 cloud /script.js
  5. You'll see k6 print some information and the URL of your test results.

            /\      |‾‾|  /‾‾/  /‾/
        /\  /  \     |  |_/  /  / /
      /  \/    \    |      |  /  ‾‾\
      /          \   |  |\  \ | (_) |
    / __________ \  |__|  \__\ \___/ .io
    
      execution: cloud
      script: test.js
      output: https://app.k6.io/runs/TEST_RUN_ID
  6. Navigate to the URL to check your test results. When the test is running, the test result page is shown.

k6 Cloud Test Results

Learn more about the different test result sections on the k6 Cloud Results docs.

Cloud execution options

All the k6 Options, like --vus and --duration are the same between the k6 run and k6 cloud commands. k6 aims to run the same script in different execution modes without making any script modifications.

Optionally, you can define some cloud options in your k6 script.

export let options = {
  ext: {
    loadimpact: {
      name: 'Hello k6 cloud!',
      projectID: 123456,
      distribution: {
        distributionLabel1: { loadZone: 'amazon:us:ashburn', percent: 50 },
        distributionLabel2: { loadZone: 'amazon:ie:dublin', percent: 50 },
      },
    },
  },
};
NameDefaultDescription
name (string)Optional. The name of the main script file, so something like "script.js".The name of the test in the k6 Cloud UI. Test runs with the same name will be grouped together.
projectID (number)Optional. It is empty by default.The ID of the project in which the test is assigned in the k6 Cloud UI. By default, the default project of the user default organization.
distribution (object)Optional. The equivalent of someDefaultLabel: { loadZone: "amazon:us:ashburn", percent: 100 }.How the traffic should be distributed. The keys are string labels that will be injected as environment variables on the appropriate nodes (matching the loadZone): __ENV["someDefaultLabel"]

List of supported load zones

LocationID
Tokyoamazon:jp:tokyo
Seoulamazon:kr:seoul
Mumbaiamazon:in:mumbai
Singaporeamazon:sg:singapore
Sydneyamazon:au:sydney
Montrealamazon:ca:montreal
Frankfurtamazon:de:frankfurt
Irelandamazon:ie:dublin
Londonamazon:gb:london
Parisamazon:fr:paris
Stockholmamazon:se:stockholm
N. Virginia (Default)amazon:us:ashburn
Ohioamazon:us:columbus
N. Californiaamazon:us:palo alto
Oregonamazon:us:portland
Hong Kongamazon:cn:hong kong
São Pauloamazon:br:sao paulo

Running tests under a different project than your default one

By default tests and test runs will be created and run under your default project, in your default organization.

To create and run tests under a different project, whether under your default organization or one you've been invited to, you have to pass the Project ID to k6.

Select the project on the sidebar menu and you will find the Project ID in the header of the Project Dashboard page.

k6 Cloud Project ID

You have two options to pass the Project ID to k6:

  1. Specify it in the script options:

    export let options = {
      ext: {
        loadimpact: {
          projectID: 123456,
        },
      },
    };
  2. Set the K6_CLOUD_PROJECT_ID environment variable when running the test.

Cloud execution tags

Tags is a powerful concept in k6 as it opens up for great flexibility in how you can slice and dice the result data.

When running a k6 test in the cloud we add two tags to all metrics:

Tag nameTypeDescription
load_zonestringThe load zone from where the the metric was collected. Values will be of the form: amazon:us :ashburn.
instance_idnumberA unique number representing the ID of a load generator server taking part in the test.

The cloud tags are automatically added when collecting the test metrics, and they work as regular tags.

For example, you can filter the results for a particular load zone on the k6 Cloud Results view.

filter tags

Or define a Threshold based on the results of a load zone.

import http from 'k6/http';

export let options = {
  vus: 50,
  duration: '30s',
  thresholds: {
    'http_req_duration{load_zone:amazon:us:ashburn}': ['p(95)<500'],
    'http_req_duration{load_zone:amazon:ie:dublin}': ['p(95)<800'],
  },
  ext: {
    loadimpact: {
      distribution: {
        ashburnDistribution: { loadZone: 'amazon:us:ashburn', percent: 50 },
        dublinDistribution: { loadZone: 'amazon:ie:dublin', percent: 50 },
      },
    },
  },
};
export default function () {
  http.get('https://test.k6.io/');
}

Environment variables

Environment variables set in the local terminal before executing k6 won't be forwarded to the k6 cloud service, and thus won't be available to your script when executing in the cloud.

With cloud execution, you must use the CLI flags (-e/--env) to set environment variables like -e KEY=VALUE or --env KEY=VALUE.

For example, given the script below, which reads the MY_HOSTNAME environment variable.

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);
}

You'd execute it using the command like:

$ k6 run -e MY_HOSTNAME=test.k6.io script.js

Injected environment variables on the cloud execution

When running in the k6 Cloud there will be three additional environment variables that can be used to find out in which load zone, server instance, and distribution label the given script is currently running.

NameValueDescription
LI_LOAD_ZONEstringThe load zone from where the the metric was collected. Values will be of the form: amazon:us :ashburn (see list above).
LI_INSTANCE_IDnumberA sequential number representing the unique ID of a load generator server taking part in the test, starts at 0.
LI_DISTRIBUTIONstringThe value of the "distribution label" that you used in ext.loadimpact.distribution corresponding to the load zone the script is currently executed in.

You can read the values of these variables in your k6 script as usual.

export let options = {
  vus: 50,
  duration: '30s',
  ext: {
    loadimpact: {
      distribution: {
        ashburnDistribution: { loadZone: 'amazon:us:ashburn', percent: 50 },
        dublinDistribution: { loadZone: 'amazon:ie:dublin', percent: 50 },
      },
    },
  },
};
export default function () {
  if (__ENV.LI_DISTRIBUTION === 'ashburnDistribution') {
    // do something
  } else if (__ENV.LI_DISTRIBUTION == 'dublinDistribution') {
    // do something
  }
}

Differences between local and cloud execution

Iterations

Local execution has support for iterations based test length (-i or --iterations on CLI, and iterations in script options) which is not yet supported by the cloud execution mode.

Using setup/teardown life-cycle functions

Your setup and teardown life cycle functions are executed as normal when running cloud tests. Depending on the size of your test, it will execute from one or more cloud servers, but the setup and teardown will only execute from one server, so execute once for each test run. There's no guarantee though that the same cloud server that executed the setup() will execute the teardown().