K6 results with InfluxDB and Grafana

Reading Time: 5 minutes

Hi folks,

In this blog, we will be learning, how to showcase k6 results statistics in the grafana dashboard. For routing the k6 statistics log to Grafana, we need need to reroute them with a database. In this blog, we will be using InfluxDB for rerouting. So, let’s get going and integrate k6 with Influx DB and Grafana.

Why use Grafana and Influx DB for k6 result visualization?

The reason is quite elementary for this. If we run a k6 load test script, you must have noticed that all the statistics for that load test are in the form of console logs only. Although, the logs are very verbose and with a little effort anyone can get an understanding of them. However, you don’t find it much attractive when you share the result with the stakeholders. Let’s break down this rationale into key points.

  • One may advocate that we can get the whole of the statistics in a CSV or JSON format. You can use the data from the JSON or CSV output, to convert it into a shareable HTML file that has some handsome images and figures to showcase the statistics. This way is much better than a standalone JSON or CSV file
  • Nevertheless, we cannot showcase the real-time data in an HTML file. To make it easier to understand you will have to incorporate graphs and other charts into the HTML file to showcase the stats effectively. This will require much more effort.
  • This is where Grafana gives you an upper hand. Not only we can get awesome graphs to reflect the statistics but also we get to see the real-time data.
  • However, we cannot directly circuit the k6 result stats to grafana, we need to send those stats to a database and from that database, Grafana will pick up the data. As a result, it will get reflected in Grafana’s dashboard. I hope I was able to make it a bit explicit for you

Moving on, we need to set up Influx DB and Grafana on our system. Please refer to the official documentation given below for installation steps.

Sending Test result from k6 to Influx DB

Firstly, we need to have a k6 test script in place before moving ahead. For your reference, this is the k6 test script that I’ve been using for this demo.

import http from "k6/http";

import { check } from "k6";


export let options = {

    vus: 25,

    duration: '60s',

    thresholds:{

        //Define requirements

        'failed requests': ['rate<0.1'], // less than 10 percent failures

        'http_req_duration': ['p(95)<200','p(99)<300'],

        'checks':['rate>0.95']

    }

}

export default function(){

    let response = http.get("https://reqres.in/api/users?page=2");

    check(response, {

        "is status 200": (r) => r.status === 200

    });

}

Moving on, assuming that influx DB is up and running, we need to run these k6 load tests and send the metrics to Influx DB. This is quite easy to do, all we need to do is run the following command inside the test directory where our test script resides. Suppose, the test file name is influxdb.js. Hence, the corresponding test command will be

k6 run influxdb.js --out influxdb=http://127.0.0.1:8086/k6database

We need to specify the port on which influx DB is exposed. And at the very last of this command, there is a keyword “k6database”. It is nothing but the name of the database where we will be sending the test metrics. If the data with this name already exists, then the metrics will get updated there. If no database with this name exists, then influx DB will create a fresh one for you and, hence, test metrics will be redirected to it.

On successful execution of the test, the log will appear in influx DB. It may look something like this.

As you can see some logs have appeared here, but they still are not that readable. Hence, we will be now picking up this data and sending them to grafana.

Sending metrics for Influx DB to Grafana

The next step will be sending test metrics from influx DB to Grafana. Firstly, you have to open port 3000 on your browser and from there you can log in to Grafana. You will need to fill in the username and password to enter. By default, the username and password are admin and admin respectively. Upon successful login, you can change your username and password.

The home page looks something like this

Now we need to add a data source in Grafana.

Moving on, we need to click on “Add your first data source”. After that, search for influx DB from the search bar and select it. You will be prompted to a page where you need to fill in DB details. Refer to the screenshot below.

Firstly, we need to update the URL on which our DB is running. You can put http://localhost:8086/. After that, we can ignore the rest of the options they are optional. Now, scroll down to the bottom where it asks for the database name. Earlier, we created the database by the name “k6database”. Fill in the name and you may leave the rest of the options.

Now, click on “Save & Test”. And voila, your influx DB and grafana are now in sync with each other. Only one thing remains, i.e, Grafana Dashboard.

Creating Grafna Dashboard for K6

If you want, you may create a dashboard from the scratch. However, to save us from the hassle, the k6 dashboard is already in place.

You can get the dashboard from here, https://grafana.com/grafana/dashboards/2587.

Moving on, we need to import this dashboard. For this, click on the import option. Refer to the screenshot below

Provide the grafana dashboard(it’s URL) that needs to be imported and click on “Load”. Once done, your Grafana dashboard will reflect the K6 result metrics. Even, you can see the live results for the test are being executed at that moment. The dashboard will look something like this

And that’s pretty much it. I hope you may have found this useful. Please do check out our other blogs, https://blog.knoldus.com/category/tech-blogs/test-automation/. Thanks!

Reference:

Written by 

Sparsh is a QA Consultant having experience of more than 2 years. He is familiar with the core concepts of manual and automation, Karate, Cypress, Gatling, Rest Assured and Selenium are the tools that Sparsh is familiar with. He is always eager to learn new and advanced concepts in order to upskill himself.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading