How to Use the Command 'k6' (with examples)
k6 is a powerful open-source load testing tool that serves to assess and improve the performance of your applications. It is widely used by engineering teams to simulate and test the robustness of systems under significant load. k6 offers the flexibility to run tests both locally and in the cloud, with options for comprehensive reporting and integration with various analytics tools.
Use case 1: Run Load Test Locally
Code:
k6 run script.js
Motivation:
Running load tests locally is a crucial step for developers who want to quickly evaluate the performance of their applications before taking the tests to more extensive environments such as the cloud. It allows for instant feedback and adjustments during the development phase.
Explanation:
k6
: This invokes the k6 command line tool.run
: This argument tells k6 to execute a load test defined in the specified script.script.js
: This is the JavaScript file detailing the load test scenarios, including requests, metrics, and user behavior.
Example Output:
/\ |‾‾| /‾‾/ /‾/
/\ / \ | |/ / / /
/ \/ \ | ( / ‾‾\
/ \ | |\ \ | (_) |
/ __________ \ |__| \__\ \___/ .io
execution: local
script: script.js
output: -
scenarios: (100.00%) 1 executors, 10 max VUs, 30s max duration (incl. graceful stop):
* default: 10 looping VUs for 30s (gracefulStop: 30s)
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:
Optimizing resource allocation for load tests requires precise control over the number of virtual users and the test duration. Specifying these parameters allows testers to replicate different usage levels and analyze system behavior under anticipated traffic conditions.
Explanation:
--vus 10
: This sets the number of virtual users to 10, simulating 10 concurrent users interacting with the system.--duration 30s
: This specifies that the test should run for 30 seconds, allowing for short-term stress testing to observe immediate performance under heavy usage.
Example Output:
running (0m30.0s), 0/10 VUs, 212 complete and 0 incomplete iterations
default ✓ [======================================] 10 VUs 30s
✓ http_reqs........................................ 212
Use case 3: Run Load Test Locally with a Given Environment Variable
Code:
k6 run -e HOSTNAME=example.com script.js
Motivation:
Environment variables are instrumental in configuring tests for different situations without altering the core test script. By using environment variables, you can flexibly switch targets and tweak settings based on different environmental conditions.
Explanation:
-e HOSTNAME=example.com
: This flag sets an environment variableHOSTNAME
with the valueexample.com
, allowing dynamic referencing within the script for URL endpoints or other configuration needs.
Example Output:
execution: local
script: script.js
output: -
scenarios: (100.00%) 1 executors, 10 max VUs, 30s max duration (incl. graceful stop):
* default: 1 looping VUs for 30s (gracefulStop: 30s)
running (0m30.0s), 0/1 VUs, 30 complete and 0 incomplete 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:
Persisting load test results in a database like InfluxDB enables comprehensive post-test analyses and historical comparisons. This setup is invaluable for monitoring performance trends and ensuring consistent application performance over time.
Explanation:
--out influxdb=http://localhost:8086/k6db
: This configuration directs k6 to output results to an InfluxDB instance running atlocalhost:8086
, storing data in thek6db
database. It allows developers to leverage InfluxDB’s powerful querying and data visualization capabilities.
Example Output:
/\ |‾‾| /‾‾/ /‾/
/\ / \ | |/ / / /
/ \/ \ | ( / ‾‾\
/ \ | |\ \ | (_) |
/ __________ \ |__| \__\ \___/ .io
execution: local
script: script.js
output: http://localhost:8086/k6db
data points written: 2400 (approx)
Use case 5: Run Load Test Locally and Discard Response Bodies
Code:
k6 run --discard-response-bodies script.js
Motivation:
When the test focus is on request performance or server-side metrics rather than client-side content, discarding response bodies can speed up the load test execution significantly. This method is ideal for scenarios emphasizing throughput and server response times.
Explanation:
--discard-response-bodies
: This flag tells k6 to avoid storing response bodies in memory, enhancing execution speed and minimizing resource usage during load tests.
Example Output:
/\ |‾‾| /‾‾/ /‾/
/\ / \ | |/ / / /
/ \/ \ | ( / ‾‾\
/ \ | |\ \ | (_) |
/ __________ \ |__| \__\ \___/ .io
execution: local
script: script.js
output: -
duration: 30s, 11 max VUs
iterations: 1000
Use case 6: Run Load Test Locally Using the Base JavaScript Compatibility Mode
Code:
k6 run --compatibility-mode=base script.js
Motivation:
Using the base JavaScript compatibility mode can provide performance benefits. It’s particularly useful when script complexity is high, and there’s no need for advanced ECMAScript features, ensuring a faster runtime and execution.
Explanation:
--compatibility-mode=base
: This flag sets the JavaScript runtime to base compatibility mode, which uses a subset of JavaScript features to optimize performance.
Example Output:
/\ |‾‾| /‾‾/ /‾/
/\ / \ | |/ / / /
/ \/ \ | ( / ‾‾\
/ \ | |\ \ | (_) |
/ __________ \ |__| \__\ \___/ .io
execution: local
script: script.js
output: -
js runtime: base
Use case 7: Log in to Cloud Service Using Secret Token
Code:
k6 login cloud --token secret
Motivation:
Logging in to the k6 cloud service with a token simplifies authentication processes and expedites transitioning from local to cloud-based testing. It facilitates centralized management and broader test execution capabilities.
Explanation:
login cloud
: This command logs the user into the k6 cloud service.--token secret
: This flag uses a secret token for authentication, replacing the need for username and password credentials for streamlined access.
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:
Running tests on k6’s cloud infrastructure allows for large-scale, distributed load testing that can more accurately simulate geographically diverse user bases. It’s perfect for testing applications that need to handle extensive user load from different parts of the world, fulfilling scalability requirements.
Explanation:
cloud
: This instructs k6 to execute the load test using its cloud-based services.script.js
: Specifies the test script that contains the scenarios and workflows to be run in the cloud environment.
Example Output:
/\ |‾‾| /‾‾/ /‾/
/\ / \ | |/ / / /
/ \/ \ | ( / ‾‾\
/ \ | |\ \ | (_) |
/ __________ \ |__| \__\ \___/ .io
execution: cloud
script: script.js
output: https://app.k6.io/load-tests/ab123cd-report
status: completed
Conclusion
These use cases demonstrate the versatility and power of the k6 command for executing load tests in both local and cloud environments. By leveraging these commands, developers can tailor their testing strategies to match specific requirements and receive insightful performance metrics, aiding in the continuous optimization of their applications.