K6 load tests — Part I

Kavita Jadhav
3 min readNov 13, 2023

--

Writing the working tests

https://artoftesting.com/load-testing

K6 is a testing product owned by Grafana, used for load testing but not limited to it. It comes in two editions, Open Source and Grafana Cloud K6. I am using the Open Source edition for this blog. It

To install k6 on Mac, run the command:

brew install k6

Create a new folder. I am using the K6 test endpoint(http://test.k6.io) to run tests against it. You can choose to use the endpoint of your application.

Now create a new test file test.js to start accessing the endpoint with the below content:

import http from 'k6/http';
import {check} from 'k6';


export default function () {
let res = http.get('http://test.k6.io');
};

Run the test with the command:

k6 run test.js

After execution of the file, it will print information about the test and test results.

execution: local - Tests are running on the local machine. The alternative execution environment is cloud. K6 cloud automatically uploads your script and any dependencies to our cloud. Need to configure and log in to the cloud environment for it. The command is:

k6 cloud test.js

script: test.js - Name of the test file.

output: - Here output is blank as output options are not defined. Test summary can be stored in the JSON file by mentioning out param in the test run command.

k6 run test.js - out json=test.json

scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop): By default, the test will be executed once with one virtual user(vus). These values can be provided as input to the command or can be configured in the test itself with options variable.

k6 run test.js - iterations 10 - vus 2
export let options = {
vus: 10,
iterations: 100
};

The next section will display the test metrics including the amount of data sent and received. number of iterations and HTTP requests made, number of failed requests, virtual users(vus), and patterns (min, max, med, avg, 90%, 95%) time of requests. More information about these metrics is available here.

After applying all the parameters mentioned above, the summary will look like below:

It had made 20 HTTP requests (10 iterations * 2 vus). Out of them, 0 requests have failed. The average request time is 346.51ms, the maximum it took was 614.8ms to respond etc. Output summary information can be published to Prometheus to plot Grafana dashboards.

Here, none of the API calls failed as configurations were quite low. Here I am configuring 5000 iterations with 2000 vus, the API calls will me made for 30 seconds with a default grace period of 30 seconds(60 seconds total). It may not be able to complete all given requests within 60 seconds. Also, some of the requests might fail as the server will not be able to process 2000 requests in parallel.

k6 run test.js --out json=test.json --iterations 5000 --vus 2000 --duration 30s

The test execution summary looks like below (I have omitted a few error lines):

Until now, test configurations have been provided as part of the test run command. These can be configured in the file to provide it as the input to the command. More details here in Part II of the series.

Note: Subsequent parts of the series are available here.

Happy testing!

--

--