How to use the command `k6` (with examples)

How to use the command `k6` (with examples)

k6 is an open source load testing tool and SaaS (Software as a Service) platform designed for engineering teams. It allows you to simulate thousands of virtual users to test the performance and scalability of your applications. In this article, we will explore different use cases of the k6 command and provide examples for each use case.

Use case 1: Run load test locally

Code:

k6 run script.js

Motivation: The motivation for using this example is to run a load test locally without explicitly specifying the number of virtual users or the duration. This can be useful when you want to quickly execute a load test with default settings.

Explanation:

  • k6 run starts a load test using the script.js file.
  • script.js is the name of the script that contains the load test configuration and logic.

Example output:

...

running (0m01.0s), 0/10 VUs, 0 complete and 0 interrupted iterations
running (0m02.0s), 0/10 VUs, 0 complete and 0 interrupted iterations
running (0m03.0s), 0/10 VUs, 0 complete and 0 interrupted iterations
...

Use case 2: Run load test locally with a given number of virtual users and duration

Code:

k6 run --vus 10 --duration 30s script.js

Motivation: The motivation for using this example is to run a load test locally with a specific number of virtual users and duration. This allows you to simulate real-world scenarios and validate the performance of your application under different loads.

Explanation:

  • --vus 10 specifies the number of virtual users to simulate during the load test. In this example, we set it to 10.
  • --duration 30s specifies the duration of the load test. In this example, we set it to 30 seconds.

Example output:

...

running (0m01.0s), 10/10 VUs, 22 complete and 0 interrupted iterations
running (0m02.0s), 10/10 VUs, 56 complete and 0 interrupted iterations
running (0m03.0s), 10/10 VUs, 82 complete and 0 interrupted iterations
...

Use case 3: Run load test locally with a given environment variable

Code:

k6 run -e HOSTNAME=example.com script.js

Motivation: The motivation for using this example is to run a load test locally using a specific environment variable. Environment variables can be used to parameterize your load test script and make it more flexible.

Explanation:

  • -e HOSTNAME=example.com sets the HOSTNAME environment variable to example.com.

Example output:

...

INFO[0001] Environment variable                                   key=HOSTNAME value=example.com
running (0m01.0s), 0/10 VUs, 0 complete and 0 interrupted iterations
running (0m02.0s), 0/10 VUs, 0 complete and 0 interrupted iterations
running (0m03.0s), 0/10 VUs, 0 complete and 0 interrupted iterations
...

Use case 4: Run load test locally using InfluxDB to store results

Code:

k6 run --out influxdb=http://localhost:8086/k6db script.js

Motivation: The motivation for using this example is to run a load test locally and store the results in an InfluxDB database. This allows you to analyze and visualize the load test results using tools like Grafana.

Explanation:

  • --out influxdb=http://localhost:8086/k6db specifies the InfluxDB endpoint where the load test results will be stored. In this example, we use localhost:8086 as the InfluxDB address and k6db as the database name.

Example output:

...

running (0m01.0s), 0/10 VUs, 0 complete and 0 interrupted iterations
running (0m02.0s), 0/10 VUs, 0 complete and 0 interrupted iterations
running (0m03.0s), 0/10 VUs, 0 complete and 0 interrupted iterations
...

Use case 5: Run load test locally and discard response bodies

Code:

k6 run --discard-response-bodies script.js

Motivation: The motivation for using this example is to run a load test locally and discard the response bodies. This can significantly improve the performance of the load test as it removes the need to buffer and store the response bodies.

Explanation:

  • --discard-response-bodies flag instructs k6 to not buffer or store the response bodies.

Example output:

...

running (0m01.0s), 0/10 VUs, 0 complete and 0 interrupted iterations
running (0m02.0s), 0/10 VUs, 0 complete and 0 interrupted iterations
running (0m03.0s), 0/10 VUs, 0 complete and 0 interrupted iterations
...

Use case 6: Run load test locally using the base JavaScript compatibility mode

Code:

k6 run --compatibility-mode=base script.js

Motivation: The motivation for using this example is to run a load test locally using the base JavaScript compatibility mode. The base compatibility mode is significantly faster than the full compatibility mode, but it may have some limitations in terms of feature support.

Explanation:

  • --compatibility-mode=base flag sets the compatibility mode to base JavaScript compatibility mode.

Example output:

...

running (0m01.0s), 0/10 VUs, 0 complete and 0 interrupted iterations
running (0m02.0s), 0/10 VUs, 0 complete and 0 interrupted iterations
running (0m03.0s), 0/10 VUs, 0 complete and 0 interrupted iterations
...

Use case 7: Log in to cloud service using secret token

Code:

k6 login cloud --token secret

Motivation: The motivation for using this example is to log in to the k6 cloud service using a secret access token. This allows you to leverage the cloud infrastructure to run load tests and gather detailed insights and analytics.

Explanation:

  • login cloud logs in to the k6 cloud service.
  • --token secret specifies the access token to authenticate with the k6 cloud service.

Example output:

Successfully logged in to the k6 cloud service!

Use case 8: Run load test on cloud infrastructure

Code:

k6 cloud script.js

Motivation: The motivation for using this example is to run a load test on the k6 cloud infrastructure. By utilizing the cloud infrastructure, you can simulate high loads and analyze the results with ease.

Explanation:

  • cloud starts a load test on the k6 cloud infrastructure.
  • script.js is the name of the script that contains the load test configuration and logic.

Example output:

...

running (0m01.0s), 0/10 VUs, 0 complete and 0 interrupted iterations
running (0m02.0s), 0/10 VUs, 0 complete and 0 interrupted iterations
running (0m03.0s), 0/10 VUs, 0 complete and 0 interrupted iterations
...

Conclusion:

In this article, we explored various use cases of the k6 command, a powerful open source load testing tool. We learned how to run load tests locally with different configurations, use environment variables, store results in InfluxDB, and leverage the k6 cloud service for load testing. By mastering these use cases, you can effectively test the performance and scalability of your applications to ensure they can handle expected loads.

Related Posts

Understanding and Utilizing the "glab pipeline" Command (with Examples)

Understanding and Utilizing the "glab pipeline" Command (with Examples)

Introduction GitLab CI/CD pipelines are an integral part of modern software development, allowing developers to automate the build, test, and deployment processes.

Read More
How to use the command 'brctl' (with examples)

How to use the command 'brctl' (with examples)

The ‘brctl’ command is a utility for managing Ethernet bridges in Linux.

Read More
How to use the command 'view' (with examples)

How to use the command 'view' (with examples)

The ‘view’ command is a read-only version of the popular text editor ‘vim’.

Read More