A Quick Start with the K6

Berkay Kırmızıoğlu
3 min readJan 5, 2023

Performance testing is an essential part of the software development process, as it helps ensure that an application can handle the expected load and user traffic. There are many tools available for performance testing, and one popular choice is k6.

k6 is an open-source load testing tool that allows developers to test the performance of their applications. It is written in Go and can run tests from the command line or through cloud services such as LoadImpact.

In this article, we will go over how to use k6 to perform performance testing on a web application. We will cover the following topics:

  • Installing k6
  • Writing a k6 test script
  • Running a k6 test
  • Analyzing k6 test results

Installing k6

To use k6, you will first need to install it. There are several ways to install k6, including using a package manager such as Homebrew (on macOS) or Scoop (on Windows), downloading a binary release from the k6 website, or building k6 from source.

Here is an example of how to install k6 using Homebrew:

brew install k6

Once k6 is installed, you can verify the installation by running the following command:

k6 version

This should print the version number of k6.

Writing a k6 test script

k6 test scripts are written in JavaScript and define the actions that k6 should take during the performance test.

Here is an example k6 test script that sends HTTP requests to a web application:

import http from "k6/http";

export default function() {
let res = http.get("http://medium.com/");
check(res, {
"status was 200": (r) => r.status == 200,
"transaction time OK": (r) => r.timings.duration < 200
});
}

This script imports the http module from k6, which allows the script to send HTTP requests. It then defines a default function that sends a GET request to the URL http://medium.com/.

The check() function is used to validate the response from the server. In this example, we are checking that the status code is 200 (indicating a successful request) and that the transaction time is less than 200 milliseconds.

You can add as many HTTP requests and checks as you want to your k6 test script. You can also configure the number of virtual users (also called “VUs”) that will run the script and the duration of the test.

Here is an example of a more complete k6 test script:

import http from "k6/http";

export let options = {
vus: 50,
duration: "30s"
};

export default function() {
let res = http.get("http://medium.com/");
check(res, {
"status was 200": (r) => r.status == 200,
"transaction time OK": (r) => r.timings.duration < 200
});
}

This script will run with 50 VUs for 30 seconds. You can adjust these values to match your desired load and test duration.

Running a k6 test

Once you have written your k6 test script, you can run it using the k6 run command.

Analyzing k6 test results

To analyze the results of a k6 test, you can use the k6 visualize command. This will generate a dashboard with various charts and metrics that can help you understand how the application performed during the test.

The dashboard includes information such as the number of requests per second, the average response time, and the percentage of successful requests. You can use this information to identify any performance bottlenecks or issues with the application.

Here is an example of how to generate a k6 dashboard:

k6 run script.js
k6 visualize

This will run the script.js file and then open a dashboard in the default web browser. You can also specify a file path to save the dashboard as a static HTML file.

--

--