Load Test your NodeJS app using K6
Tags: loadtesting, k6, node
Published: 2023-09-12 11:59:15

---
title: Load Test your NodeJS app using K6
published: true
date: 2020-12-06 23:40:27 UTC
tags: loadtesting,k6,nodejs
canonical_url: https://levelup.gitconnected.com/load-test-your-nodejs-app-using-k6-f7b2bd8fa5cf
---

![](https://cdn-images-1.medium.com/max/1024/1*Za7giJ_9j5Gybk6ZNpM3hw.png)
_Load test using k6_

### What is load testing?

A **load test** is a type of software **testing** which is conducted to understand the behavior of the application under a specific expected **load**. **Load testing** is performed to determine a system’s behavior under both normal and at peak conditions([reference](http://tryqa.com/what-is-load-testing-in-software/#:~:text=A%20load%20test%20is%20type,normal%20and%20at%20peak%20conditions.)).

If you are not familiar with this type of test you should know that we always want to know the capability of our software and the number of online users that we can handle. The goal of load tests is giving us an estimation that how we can handle the users and maybe we expect 10'000 users for a special event like _Black Friday but_ we are not sure if we can handle all of them with the resources we have right now or with the structure we have.

**So what is the remedy?**  
The best remedy is to load testing your app before the disaster happened.

### What is k6?

k6 is a developer-centric, free, and open-source load testing tool built for making performance testing a productive and enjoyable experience.

Using k6, you’ll be able to catch performance regression and problems earlier, allowing you to build resilient systems and robust applications ([reference](https://k6.io/docs/)).

### What are the other alternatives?

[**Artillery**](https://artillery.io/) is a common load test tool for NodeJS applications. There are some other powerful tools for load testing and you can take a look at [this article](https://www.softwaretestinghelp.com/performance-testing-tools-load-testing-tools/) for more details.

### Why K6?

- Scripting in JavaScript ES2015/ES6 — with support for [local and remote modules](https://k6.io/docs/using-k6/modules)
- [Checks](https://k6.io/docs/using-k6/checks) and [Thresholds](https://k6.io/docs/using-k6/thresholds) — for goal-oriented, automation-friendly load testing
- In the case of performance, I read [this issue](https://gitlab.com/gitlab-org/quality/performance/-/issues/56)of Gitlab and why they are using K6 instead of Artillery

Now we are going to create a simple express app and test it using K6.

### Create a simple Express App
```javascript
const app = require('express')();

app.get("/:id", (req, res) => {
    const id = req.params.id;
    res.json({
        id
    })
});

app.listen(3000);
```

This is the simplest possible app we can create using ExpressJS. We instantiated an express app in line 1 and after that, we implemented requests of this format: _localhost:3000/someId_

### K6 Installation

If you are using mac os you can simply install it using : brew install k6

if you are using Ubuntu you can install it using :

```
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 379CE192D401AB61
echo "deb [https://dl.bintray.com/loadimpact/deb](https://dl.bintray.com/loadimpact/deb) stable main" | sudo tee -a /etc/apt/sources.list
sudo apt-get update
sudo apt-get install k6
```

Also, the _K6 docker container_ is available and we can use it in the CI pipelines.

If you are using another OS check the [K6 Installation guide](https://k6.io/docs/getting-started/installation).

### Implement a Load Test Script
```javascript
import http from 'k6/http';
import { check } from 'k6';

export default function () {
    const rnd = Math.floor(Math.random() * 1000);
    const response = http.get(`http://localhost:3000/${rnd}`);
    check(response, {
        "is status 200": (r) => r.status === 200,
        "is id the same we asked": (r) => {
            const id = Number(r.json("id"))
            return id === rnd
        }
    })
}

export let options = {
    vus: 1,
    duration: '5s',
    // thresholds: {
    //     'failed requests': ['rate<0.02'],
    //     http_req_duration: ['p(95)<500'],
    //     http_reqs: ['count>6000']
    // },
};
```

This is the implementation of a simple load test using K6:

**Export a default function** : This is the function that going to be called each time K6 wants to send a request.

**Sending HTTP Request:** for sending requests we use the http module from k6/http . In line 6 we have sent an HTTP request to the endpoint we have in our express application.

**Implement Checks:** If you want to be sure that you receive the response you expect you can use check module. The first argument is the response and the second argument is an object that keys of the object are the **_checks_** and the values are **_the implementation of the checks which are functions that should return a boolean_**.  
The first check we implemented is checking the status of response using the response.status .  
In the second check, we checked the id from the response body. By using json method we can access the response body. The format of using json method is like this: response.json('fieldSelector') . here we used the id field and since it is a string we cast this string to a number to compare it with the value we expect.

#### Options:

We can pass the options in the command line or we can export the options in the script. our options contain vus that means the **number of virtual users** we can send requests as different users or we can send requests like that this is only one person who sends these requests. duration is the duration of this load test. Also, we have another useful option named thresholds that is really practical for using K6 in the CI. It fails if the result does not meet the thresholds. For more information about thresholds take a look at [this part of k6 docs](https://k6.io/docs/using-k6/thresholds).

#### Running the load test

For running the load test we can use this command:

```
k6 run load-test.js
```

This is the result on my local machine:

![K6 Test Result](https://cdn-images-1.medium.com/max/1024/1*PLV8Y4Jb1ux8D-vw5_W5OA.png)
_K6 test result_

As you can see in the results the Express App handled _22962_ requests and approximately _4574_ requests per second.

### Conclusion

Using K6 was really easy since we can implement tests using Javascript and the K6 is developer-friendly.

* * *