How To Load Test Your Node.js App Using K6

Dumindu Buddhika
Code Insights
Published in
4 min readOct 24, 2017

OK you have developed your Node.js application, ran unit tests, ran integration tests now what you should do? You should run a load test! right? In order to see if your application can handle the expected load. So how can you run a load test? What tool are available? In this article I am going to talk about one such tool that you can use to load test your application.

The name of the application is k6. K6 is an open source project aimed to provide the ability to test the performance of your backend infrastructure. Its written using Go and JavaScript. K6 is a modern load testing tool built on the experience of LoadImpact. It is not the first tool that come up in the Google search results. But it’s simplicity and ability to export data to InfluxDB to be visualized by Grafana makes it a powerful tool to load test your application.

K6 uses the concept of virtual users (VU). You can have a multiple number of virtual users which runs the test script in parallel. Test scripts can be written for your application using modern ES6 syntax.

Installation

On mac you can run,

brew tap loadimpact/k6
brew install k6

If you are on another platform, download the binary from here.

Running a load test

In order to run a load test using k6, you need to create a script describing your test. Following is a simple example of a script,

import http from "k6/http";
import { check, sleep } from "k6";
export let options = {
vus: 10,
duration: "10s"
};
export default function() {
let res = http.get("url");
check(res, {
"success": (r) => r.status == 200
});
};

In options, vus defines the number of virtual users you need and duration is the time duration you need to run your test. A detailed list of available options can be found here.

To run the test execute,

k6 run script.js

Results are as follows,

/\      |‾‾|  /‾‾/  /‾/   
/\ / \ | |_/ / / /
/ \/ \ | | / ‾‾\
/ \ | |‾\ \ | (_) |
/ __________ \ |__| \__\ \___/ Welcome to k6 v0.17.1!
execution: local
output: -
script: /opt/k6-v0.17.2-linux64/script.js (js)
duration: 10s, iterations: 0
vus: 10, max: 10
web ui: http://127.0.0.1:6565/[running ] 1s / 10s
[running ] 1.9s / 10s
[running ] 3s / 10s
[running ] 3.9s / 10s
[running ] 5s / 10s
[running ] 5.9s / 10s
[running ] 6.9s / 10s
[running ] 8s / 10s
[running ] 9s / 10s
[running ] 9.9s / 10s
[done ] 10s / 10s
✓ successchecks................: 100.00%
data_received.........: 8.3 kB (828 B/s)
data_sent.............: 1.0 kB (100 B/s)
http_req_blocked......: avg=15.34ms max=460.51ms med=2.26µs min=1.17µs p(90)=4.87µs p(95)=6.37µs
http_req_connecting...: avg=13.65ms max=409.75ms med=0s min=0s p(90)=0s p(95)=0s
http_req_duration.....: avg=316.86ms max=410.34ms med=307.13ms min=263.38ms p(90)=399.14ms p(95)=406.81ms
http_req_receiving....: avg=209.5µs max=9.89ms med=117.57µs min=62.95µs p(90)=228.21µs p(95)=289.2µs
http_req_sending......: avg=21.04µs max=195.85µs med=16.37µs min=7.74µs p(90)=35.21µs p(95)=43.46µs
http_req_waiting......: avg=316.62ms max=410.16ms med=306.97ms min=263.25ms p(90)=398.95ms p(95)=406.69ms
http_reqs.............: 300 (30/s)
vus...................: 10
vus_max...............: 10

Meanings of these metrics can be found here.

Visualizing results with Grafana

Now that we know how toget the metrics by running tests, lets visualize them in Grafana. In order to visualize data in Grafana we need to export them into a format that Grafana understands. K6 supports exporting data to InfluxDB which can be understood by Grafana.

Following commands will install Grafana and InfluxDB,

Ubuntu 16.04
sudo apt-get install grafana
sudo apt-get install influxdb
Mac
brew install grafana
brew install influxdb

By default, InfluxDB server runs on port 8086 and Grafana server runs on port 3000.

Now lets run the load test and export data to InfluxDB,

k6 run --out influxdb=http://localhost:8086/resultsdb script.js

Here resultsdb is your database to be created in InfluxDB.

Now head over to http://localhost:3000. Then create a data source.

Setting up data source

Set the values as above. Select access as proxy if you want to access InfluxDB from server side (for example if your InfluxDB is running in localhost in Grafana server)

Now create a new dashboard (main menu -> dashboards -> new), and create a graph.

Graph

Go to the edit mode in the graph and select the created data source in Metrics. You can select a metric as follows. When you select a metric, data will be loaded to your graph.

Edit metrics

To make our lives easier, users have already created comprehensive Grafana dashboards. In order to import them go to,

Main menu -> Dashboards -> Import

Now you need to type in the dashboard id. To import this dashboard type 2587.

Preconfigured Grafana dashboard

Ok folks, happy load testing!!!

--

--

Dumindu Buddhika
Code Insights

Full-stack developer — Golang, React, Node, Kubernetes