Getting Started with k6: A Beginner’s Guide to Load Testing

Wavda Aniva
4 min readApr 1, 2023

--

Load testing is an important step in ensuring that an application can handle the expected amount of traffic and user load. k6 is a popular open-source tool that provides extensive functionality for testing web applications, APIs, and microservices sources and is very easy to use. This post will provide an introduction to load testing with k6.

Step 1: Installation

The first step is to install k6 locally on your machine. k6 has packages for Linux, Mac, and Windows. Alternatively, you can use a Docker container or a standalone binary.

To install k6, refer to the following instructions: https://k6.io/docs/get-started/installation/. You can use a package installer or unzip the downloaded binary file and add the k6 executable to your PATH.

Step 2: Writing a Simple Load Test Script

Once k6 is installed, we can start creating our first load test script which will be written in JavaScript. Within the script, we’re using the http module to send the requests to multiple APIs from https://reqres.in and we’ll name the file as script.js

import http from 'k6/http';
import { check, sleep } from 'k6';
import { htmlReport } from "https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js";
import { textSummary } from "https://jslib.k6.io/k6-summary/0.0.1/index.js";

export let options = {
stages: [
{ duration: '1m', target: 5 }, // simulate ramp-up of traffic from 0 to 5 users over 1 minute
{ duration: '1m30s', target: 10}, // stay at 10 users for 1minutes 30 seconds
{ duration: '1m', target: 0 }, // ramp-down to 0 users over 1 minute
],
thresholds: {
http_req_duration: ['p(99)<500'], // 99% of requests must complete within 500ms
http_req_failed: ['rate<0.01'] // request failure rate must be below 1%
},
};

export default function () {
const get_request = {
method: 'GET',
url: 'https://reqres.in/api/users?page=2',
};

const post_request = {
method: 'POST',
url: 'https://reqres.in/api/users',
body: {
name: 'Wavda',
job: 'QA',
},
params: {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
},
};

const put_request = {
method: 'PUT',
url: 'https://reqres.in/api/users/2',
body: {
name: 'Aniva',
job: 'Engineer',
},
params: {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
},
};

const delete_request = {
method: 'DELETE',
url: 'https://reqres.in/api/users/2'
};

const responses = http.batch([get_request, post_request, put_request, delete_request]);
check(responses[0], {
'GET status is 200': (r) => r.status === 200,
});

check(responses[1], {
'POST status is 201': (r) => r.status === 201,
});

check(responses[2], {
'PUT status is 200': (r) => r.status === 200,
});

check(responses[3], {
'DELETE status is 204': (r) => r.status === 204,
});
}

export function handleSummary(data) {
return {
"summary.json": JSON.stringify(data),
"result.html": htmlReport(data),
stdout: textSummary(data, { indent: " ", enableColors: true })
};
}

From the load test script above, we’ve implemented:

Requests

Traffic Stages

  1. For 1 minute, we’ll simulate the ramp-up of traffic from 0 to 5 users
  2. Stay at 10 users for 1 minute and 30 seconds
  3. At the end of the test, we’ll ramp down to 0 users over 1 minute

Test Scenarios and Checks

  • 99% of requests’ response time must complete within 500ms
  • request failure rate must be below 1%
  • response status code for each request is correct by checking it using a check function

Test Report

  • a handleSummary function was also added which will generate a test report in a form of JSON and HTML files.

Step 3: Run the Load Test

Now that we have a load test script called script.js, we can run the load test using k6 with the following steps:

  1. Open a terminal or command prompt and navigate to the folder containing the test script.
  2. Run the following command and specify the script file name.
k6 run script.js

Once the tests are completed, you will be able to see the test results, including the number of requests, response time, and error rate within the terminal.

Execution Result

and also in an HTML file called result.html.

Request Metrics
Other Stats
Checks & Groups

Step 4: Analyzing Load Test Results

k6 includes several integrations for visually analyzing load test results. Two popular integrations are Grafana and InfluxDB. Grafana enables the creation of interactive dashboards that visualize load test data. InfluxDB is a time-series database capable of storing and analyzing load test data.

Conclusion

Load testing with k6 is a powerful and easy-to-use way to ensure that your application can handle the expected traffic and user load. By following these steps, you should have a good understanding of how to write load test scripts, run load tests, and analyze load test results with k6.

--

--

Wavda Aniva
Wavda Aniva

Written by Wavda Aniva

A curious potato exploring new things on software quality

No responses yet