k6 is currently one of the best and easiest-to-set-up load testing frameworks. There are tons of articles and guides on how to get started with k6 for load testing, but after a point, they aren’t much help when the complexity starts increasing. In this article, I am going to discuss a few things that saved me a lot of time. You’re probably familiar with the next few sections, but let’s go over them once more.

What is k6?

Grafana k6 is an open-source load testing tool that makes performance testing easy and productive for engineering teams. k6 is free, developer-centric, and extensible. You can use Grafana Cloud with k6 if you want to save resources and time, as it will configure and handle multiple things for you. However, Grafana Cloud is paid.

k6 is developed by Grafana Labs and the community.

Key features

k6 is packed with features, which you can learn all about in the documentation. Key features include:

Read more about it from the official doc.

Good to know: You don’t need to install k6 if you are using VS Code. You can use the executable file directly to run the tests from the terminal.

What will be included in the results output?

The results generated by k6 are quite descriptive. For quick analysis, you can check the summary metrics displayed in the terminal output. For more in-depth analysis, granular time-series data is available. Both metrics and result summaries can be customized as needed. Outputs can be saved in JSON or CSV format, or any other built-in output format.

Read more about it from the official doc.

The best way to save and share output is in PDF format, but officially, it is only available on annual Pro and Enterprise plans. However, there is an alternative solution: you can use k6-reporter. It is one of the best and easiest to set up; you just need to add two imports to your tests and you are done. If you have basic knowledge of EJS and JS, you can tailor it to your needs.

This is all you need to do:

// This will export to HTML as filename "result.html" AND also stdout using the text summary
import { htmlReport } from "https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js";
import { textSummary } from "https://jslib.k6.io/k6-summary/0.0.1/index.js";

export function handleSummary(data) {
  return {
    "result.html": htmlReport(data),
    stdout: textSummary(data, { indent: " ", enableColors: true }),
  };
}

In many cases, running a test for a long period can be time-consuming, especially if waiting for the results. To address this, you can stream live test results using various extensions provided by k6. One such extension is the xk6-dashboard, which, although a bit tricky to set up initially, becomes straightforward afterward.

There are two methods to use it: either by utilizing pre-built k6 binaries from the Releases page, or by rebuilding the installed k6 executable file with this extension. The latter option may require a few extra steps but offers improved usage. The choice is yours. I opted for the second option and generated a new executable. Below, I’ll share the command used and some info on the parameters I’m currently using.

.\k6.exe run --out 'dashboard=period=10s&report=.\reports\loadTestCustomReport.html' main.js
  • Period: This parameter defines how frequently the live tests should be updated. For tests that run for several hours, it’s advisable to set it around 20 seconds. However, for shorter tests, a period of around 10 seconds is recommended for better analysis.
  • Report: If you provide the path where your results need to be stored, they will be saved in an HTML file. This file allows you to share and review the details of each second of the test.

The best approach would be to add an extension for PDF output while using the dashboard. The PDF can serve as a summary, while HTML can be considered for deep analysis.

To make the best use and avoid typing lengthy commands repeatedly, create a build file. This will make it easier for non-technical team leads or anyone to run a quick test and obtain the results. A sample is shared below:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Run k6 Load Test",
      "type": "shell",
      "command": ".\\k6.exe",
      "args": [
        "run",
        "--out",
        "'dashboard=period=10s&report=.\\reports\\loadTestCustomReport.html'",
        "main.js"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "presentation": {
        "reveal": "always",
        "panel": "dedicated"
      }
    }
  ]
}

Ending this article here. In the next one, we will be talking about how to structure and write your tests. Thank you for your time.