Load Test your APIs with K6
Load testing is a crucial aspect of software testing, especially for applications and systems that handle many users or transactions simultaneously.
K6 is a modern open-source load-testing tool, which helps you to test the reliability and performance of your application and infrastructure.
In this article, we will focus on
- Setting up K6 on a local machine and writing a simple load-testing script with different scenarios.
- We will also use Grafana Cloud ( free tier ) to run the load testing script on the cloud and visualize the results.
Note: In this article, I am using Linux-based commands. If you’re a Windows user you can find the related commands on the following URL.
To set up K6 on a local machine download and install the tool from the following URL. ( https://k6.io/open-source/ )
Once you’re done with downloading and installation, we will start with the basics of K6. ( make sure K6 is added to your path variable for easy command executions, you can test it by running command k6)
Initialize npm and create a new file index.js in your project directory.
Install the K6 node package
npm i k6
index.js
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
stages: [
{ duration: '30s', target: 20 },
{ duration: '1m30s', target: 10 },
{ duration: '20s', target: 0 },
],
};
export default function () {
const res = http.get('https://httpbin.test.k6.io/');
check(res, { 'status was 200': (r) => r.status == 200 });
sleep(1);
}
To run the load test, execute the following command
k6 run index.js
options is the variable used to configure the k6. In the above example stages parameter configures k6 to run with three stages
- 20 Vus ( virtual users or processes ) which will make get call to the API ‘https://httpbin.test.k6.io/' for 30 sec.
- 10 Vus ( virtual users or processes ) which will make get call to the API ‘https://httpbin.test.k6.io/' for 1min and 30 sec.
- VUs will be brought down to 0 in the next 20 sec.
We can also write custom scenarios with scenarios parameter
export const options = {
scenarios:{
some_scenario_name: {
executor: 'shared-iterations',
startTime: '10s',
gracefulStop: '5s',
env: { EXAMPLEVAR: 'testing' },
tags: { example_tag: 'testing' },
vus: 10,
iterations: 200,
maxDuration: '10s',
exec: "myScenario"
},
some_other_scenario_name: {
}
},
};
export function myScenario () {
//execute some load test
}
executors are very useful for testing different real-life scenarios. The ‘shared-iterations’ executor will execute the load test by sharing 200 iterations across 10 VUs. You can find more about the scenarios and executors here.
exec parameter can be used to execute a custom function, else default function will be executed.
Tip: If you want to visualize ( on the Grafana dashboard ) the load test result by running it on your local machine you can add the following option in the options.
export const options = {
ext: {
loadimpact: {
projectID: project_id, // You can find projectID on grafana dashboard
},
}
}
To save the data and visualize the result run the following command.
k6 run --out cloud index.js
If you want to do some data setup before executing the load test, we can do that with the help of the setup function.
export function setup() {
return { authtoken: 'value' }
}
export default function (data) {
const authtoken = data.authtoken;
const params = {
headers: {
"Content-Type": "application/json",
authtoken,
},
};
const res = http.get('https://httpbin.test.k6.io/',params);
check(res, { 'status was 200': (r) => r.status == 200 });
sleep(1);
}
There is a good chance that our local machine is not enough to test the close to real-life load test. In that case, we can use Grafana Cloud with a very high number of VUs. It provides free and subscription-based plans.
We can run the index.js file on run Grafana cloud with cloud as command line option.
Create a new project under the performance section
Create a new project with a name demo
Note: Free tier account support max 100 VUs.
You can now use this project’s id with local script inside options.
Also, authentication is required before you can use the cloud option.
k6 login cloud --token your_auth_token
You can find a token inside your Grafana project or profile.
To run your load test on the cloud run the following command.
k6 cloud index.js
Here is the overview of the load test
You can download the report in PDF format also.