How to load test using k6?

K6 is an open-source load testing tool for testing the performance of APIs, micro-services or any web applications. Now, you will say what is load testing and why do we need to load test our application?

What is load testing?

Load testing is a process in which a web application is tested under specific load or conditions. It determines how the application behaves while being accessed by multiple users simultaneously.

Goal of load testing is to find bottlenecks of the web application or API and improve the performance of the application for smooth functioning.

What are the benefits of load testing?

Followings are some of the benefits of load testing.

  • Find number of concurent users an application supports at highest peak load
  • To find out if application can be scalled easily
  • Find the limitation of web application
  • Minimizing downtime by identifying and improving bottlenecks
  • Identifying hidden bugs or memory leaks

How to install k6?

K6 can be installed on different operating systems depending on your requirements. Let's install k6 on different environment.

Debian/Ubuntu

# add repo address
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C5AD17C747E3415A3642D57D77C6C491D6AC1D69
echo "deb https://dl.k6.io/deb stable main" | sudo tee /etc/apt/sources.list.d/k6.list

# update repo and install
sudo apt-get update
sudo apt-get install k6

MacOS

# install k6 using brew
brew install k6

Fedora/CentOS

sudo dnf install https://dl.k6.io/rpm/repo.rpm
sudo dnf install k6

Windows

# using chocolatey package manager
choco install k6

# or using windows package manager
winget install k6

Writing your first load test with k6?

K6 works with the concept of virtual users (VUs). These virtual users run the script until duration specified until which test should run.

Script must contain, at the vary least, a default function which is an entry point of the test. Let's look at the sample test file:

import{ sleep } from 'k6';
import http from 'k6/http';
  
export let options = {
    duration : '1m',
    vus : 50,
};
  
export default function() {
    http.get('https://your-web-app.com');
    sleep(1);
}

K6 http library allows you to make following(s) rest calls:

  • GET -> http.get()
  • POST. -> http.post()
  • DELETE. ->http.del()
  • PUT -> http.put()
  • HEAD -> http.head()
  • PATCH -> http.patch()

Using above function you can call your appropriate endpoint and pass data to endpoint. You can sleep for 1 sec after each test depends on your requirements.

In above example, we want our test to run for 1 minute of time for 50 virtual users concurrently.

How to run k6 test?

Once you create your first script you can issue following command to run the test output on your terminal window.

k6 run test.js

Once you run above script you will see some important metrics shown in table below:

Metric Defination
http_reqs Total numbers of request generated by k6 test
http_req_connecting Time taken to establish remote connection
vus Number of active virtual users
vus_max Max virtual users
iterations Total num of times script was run by VUs
iteration_duration The time it took to complete one full iteration.