Load Test with K6 and Visualize with InfluxDB and Grafana

Naoko
4 min readJun 29, 2019

--

My goto load testing tool Locust but I was introduced to k6 recently. On their home page, it says “Like unit-testing, for performance”. This sounds fascinating so I decided to play with it.

Install k6 on Ubuntu (or Debian)

sudo apt-key adv — keyserver hkp://keyserver.ubuntu.com:80 — recv-keys 379CE192D401AB61
echo “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

Create script.js

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

export default function() {
let res = http.get("https://test.loadimpact.com/");
check(res, {
"is status 200": (r) => r.status === 200
});
};

Run

k6 run script.js

Run for 30 sec with 10 virtual user load test

k6 run — vus 10 — duration 30s script.js

More check to measure success/failure

$ cat script.js 
import http from "k6/http";
import { check, sleep } from "k6";
// `options.stages to configure ramp up/down VU level
export let options = {
stages: [
{ duration: "30s", target: 20 },
{ duration: "1m30s", target: 10 },
{ duration: "20s", target: 0 },
]
}
// this defines the entry point for your VUs
// similar to the main() function in many other language
export default function() {
let res = http.get("http://test.loadimpact.com");

// check() function to verify status code, transaction time etc
check(res, {
"status was 200": (r) => r.status == 200,
"transaction time OK": (r) => r.timings.duration < 200
});
sleep(1);
}

Pretty cool. So yeah, I can definitely add this to my setup.py test command or maybe just on CI to measure success and failure of the load test result.

Next: Results visualization with InfluxDB and Grafana

Install InfluxDB

sudo apt-get install influxdb

Install Grafana

# Add gpg key
sudo wget -q -O — https://packages.grafana.com/gpg.key | apt-key add -
# install the repostiroty for stable release
sudo add-apt-repository “deb https://packages.grafana.com/oss/deb stable main”
sudo apt-get updatesudo apt-get install grafana

Start Grafana service

sudo service grafana-server start# This will start the `grafana-server` process.
# The default port is 3000.
# Default login and password `admin`/`admin`

Run k6 to store results to InfluxDB

k6 run — out influxdb=http://localhost:8086/myk6db script.js

# The above command line makes k6 connect to a local influxdb
# instance and send results data from the test to
# a database named myk6db.
# If this database does not exist, k6 will create it automatically.

Open http://localhost:3000

Login with a default username and password admin/admin

First, add Data Source. Click InfluxDB

and mostly default. Set Database to myk6db

Next, you want to add a Dashboard to show test results but configuring everything can be tedious so we will use Grafana dashboard configurations contributed by users. Go to https://grafana.com/dashboards/2587 and download JSON config.

Check InfluxDB version

$ curl -sL -I localhost:8086/ping
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: 19e52986-9a9c-11e9-b5fd-000000000000
X-Influxdb-Version: 1.1.1
Date: Sat, 29 Jun 2019 18:31:25 GMT

Edit download JSON k6-load-testing-results_rev3.json

$ cat k6-load-testing-results_rev3.json | jq '.__requires | .[] | select(.type=="datasource")'
{
"type": "datasource",
"id": "influxdb",
"name": "InfluxDB",
"version": "1.1.1"
}

You see the “version” should match to the version of your InfluxDB otherwise you will get “source not found” error. Now let’s import this JSON config file.

Mouse over “+” on the left menu and you will see “Import”. Click that.

Paste JSON config file you downloaded (and possibly edited)

Now you have an amazing dashboard look like this. On and run this to see more data!

k6 run -vu 20 --out influxdb=http://localhost:8086/myk6db script.js

--

--

Naoko

Software and Data Engineer & Machine Learning Practitioner