The Power of K6: Uncover the Secrets to Efficient Load and Performance Testing!
Written by Milan Žuža, Software Developer in Test Softray Solutions
K6 is an open-source service that provides load/performance testing for automation QA engineers. The main benefit of using k6 is super easy setup, writing new tests, executing them, and parametrizing.
It is simple to set up and create a basic test (the most basic one will be presented in this blog).
To run it locally, k6 has to be installed: https://k6.io/docs/get-started/installation/. It has multiple OS support, so you can choose which suits you.
The second thing that should be done is to create a javascript file containing the code you want to execute/test.
For this blog, we’ll create a load test for public API, which is getting content from the server, and verify that a specific attribute is present in the response body (to assure the server will be responding with regular data when some kind of load is present) and use 50 iterations with 10 simultaneous calls.
Create .js file (eg ApiLoadTestExample.js) and paste the content:
import http from 'k6/http';
import { sleep } from 'k6';
import { check } from 'k6';
export let options = {
vus: 10,
iterations: 50,
};
export default function () {
const url = 'https://jsonplaceholder.typicode.com/todos/1';
const response = http.get(url);
console.log(`Response status code: ${response.status}`);
console.log(`Response body: ${response.body}`);
// Verification
const responseBody = JSON.parse(response.body);
const title = responseBody.title;
check(title, { 'Response contains title field': (value) => value !== undefined });
sleep(1);
}
To explain the code above, the first few lines are just imports from k6 used to make http calls, verify methods for checking the response, and sleep method for the test to wait a bit before ending.
The options object contains two running parameters, vus (virtual users) and iterations. This way, we’re saying we want a total of 50 iterations with 10 users simultaneously running.
Url is a public url used for this example, where we can use the GET method to retrieve some data. We’re calling it and storing the response in the response object. For debugging purposes, we’re logging the response status and body.
As part of asserting the response, we’re parsing the JSON body to the response body object and taking the attribute with “title.” The next step is to check that value is not an undefined value, and after that, we wait for 1 second before ending the test.
After each test, the script waits for 1 second before finalizing the test.
Iterations and a number of simultaneous calls can be added both from code and from running the command:
k6 run — vus 10 — iterations 50 ApiLoadTestExample.js
Since we have it already defined in the script, we can run the following:
k6 run ApiLoadTestExample.js
Logs from k6 console:
Result stats:
As written in the logs, we see that 50 iterations are completed using 10 VUS (virtual users).
In the log table above, different metrics can be conducted, like how much time it takes for the slowest response, average response, etc. Using this information, it’s pretty easy to find bottlenecks in the API calls and to determine what is the reason why it happens.
Summary:
As seen, k6 is a very intuitive framework for performance/load testing and analysis. With some coding skills, it is relatively easy to write tests that will check outputs and the time spent on specific actions and compare it to the threshold needed for the service to work correctly in a production environment.