Menu
Grafana Cloud

Use the CLI

There are several ways you can use the Grafana k6 command-line tool (CLI) to run a k6 performance test. In this page, you’ll learn how to use each method and how they differ from each other.

Before you begin

To run a CLI test, you’ll need:

To run CLI tests in Grafana Cloud k6, you’ll also need:

Run a test on your local machine

Running tests locally is a great way to incrementally test your script as you write it. For example, you can make sure that your checks and thresholds are configured correctly before you run a test with more VUs in Grafana Cloud k6.

To run a test locally, use the k6 run command:

Bash
k6 run script.js

Run a test on Grafana Cloud k6

To run a cloud test from the CLI, you’ll need an API token for authenticating the k6 CLI with the Grafana Cloud k6 application.

With the CLI authentication configured, you can run cloud tests by using the k6 cloud command:

Bash
k6 cloud run cloud_demo.js

Run a test locally and stream the results to Grafana Cloud k6

You can also run a test from a local machine and stream the results for storage and visualization on Grafana Cloud. A common use case for this feature is to run performance tests on networks that aren’t accessible from the public internet.

To run a test locally and stream the results to Grafana Cloud k6, use the k6 cloud run --local-execution:

Bash
k6 cloud run --local-execution cloud_demo.js

Run a test in Grafana Cloud k6 and delete sensitive data

When executing a test in Grafana Cloud k6 with the k6 cloud run command, the k6 CLI runs and uploads the test script to Grafana Cloud.

You can use the deleteSensitiveData option in your test script to have Grafana Cloud k6 delete the script and any data files after the test is executed.

To use the options, include the following snippet in your test script:

js
export const options = {
  cloud: {
    deleteSensitiveData: true,
  },
};

And run the test with:

Bash
k6 cloud run cloud_demo.js

Note

The deleteSensitiveData option is unavailable in default subscriptions. If you want to activate it, contact support.

Run a test in your infrastructure with the k6 Operator

k6 Operator is a Kubernetes operator that you can use to run distributed k6 tests in your Kubernetes cluster.

A couple of reasons why you might want to do this:

  • You run your application in Kubernetes and would like k6 to be executed in the same fashion as all your other infrastructure components.
  • You want to run your tests within your private network for security or privacy reasons.

To set up and run a test using the k6 Operator, refer to Set up distributed k6.

Run a test in your infrastructure with the k6 Operator and stream the results to Grafana Cloud k6

Similar to how you can run a test locally using the k6 CLI and stream results to Grafana Cloud, you can also do the same with the k6 Operator. k6 Operator is a Kubernetes operator that you can use to run distributed k6 tests in your Kubernetes cluster.

A couple of reasons why you might want to do this:

  • You run your application in Kubernetes and would like k6 to be executed in the same fashion as all your other infrastructure components.
  • You want to run your tests within your private network for security or privacy reasons.

When using this execution method:

  • You can visualize the results of the test using the Grafana Cloud UI.
  • Grafana Cloud doesn’t have access to the script in the cloud, and you can’t start tests using the Grafana Cloud UI.

Refer to Use the k6 Operator with Grafana Cloud k6 for more details.

Run a test locally with a Private Load Zone

Private Load Zones (PLZ) are load zones that you can host inside your network by using the k6 Operator. That means that the test execution happens in your infrastructure, but your team can view the test result data in Grafana Cloud. PLZs also appear in Grafana Cloud in the same way as any default load zone location, and you can start tests in the PLZ by using the Grafana Cloud UI or by referencing the PLZ name in your test script.

Refer to Set up Private Load Zones for more details.

Run a CLI test in a specific project

By default, when you run a test from the CLI, the test runs in your default project. To create and run tests in a specific project, you can set the Project ID when running your script.

In Grafana Cloud, you can find the Project ID value below the project name on the project view:

k6 Project ID

You can pass the Project ID to k6 in two ways:

JavaScript
export const options = {
  cloud: {
    projectID: 3479144,
  },
};
  • Set the K6_CLOUD_PROJECT_ID environment variable when running your test. For example:
Bash
K6_CLOUD_PROJECT_ID=PROJECT_ID k6 cloud run test.js

Upload a test script to Grafana Cloud k6

You can upload a test script to Grafana Cloud k6 by using the k6 cloud upload command. For example:

Bash
k6 cloud upload cloud_demo.js

That command uploads the test script to the default project, or the project ID specified in your test script configuration, without starting a new test run.

This can be useful if you want to update a test that’s running on a schedule, but don’t want it to run at the same time.