An Introduction to k6: An API Load-Testing Tool
By: Ng Wai Foong
Tags: Programming, DevOps, API, JavaScript, Nodejs

HTTP Requests

HTTP GET request

You can define your first test case by utilizing the built-in http module. Create a new JavaScript file in your working directory. I’ll just call it script.js . Then, add the following code, which represents a simple GET request:

import http from 'k6/http';

import { sleep } from 'k6'; export default function () {

http.get('http://example.com/test');

sleep(1);

}

Each VU will execute the code inside the default function sequentially from start to end. Once it has reached the end, it’ll loop back, and the process will be repeated all over again.

It’s always a good idea to pace out your VUs by adding a sleep statement at the end of the default function. This simulates how real users use your system. You can set the value lower to 0.1 for simulating aggressive behaviors. If you intend to simulate users that constantly call your API, simply remove the sleep statement.

If you don’t have an API at the moment and just want to test k6, you can use the following test API provided by k6:

http://test.k6.io

Run the following code in your terminal:

k6 run script.js

Result output

You should see the following output in your terminal

Image by the author

By default, k6 will collect the following metrics each time you run a test:

vus — number of active virtual users

— number of active virtual users vus_max — maximum virtual users allocated for the test

— maximum virtual users allocated for the test iterations — aggregated number of times the default function is called

— aggregated number of times the function is called iteration_duration — the total time it took to execute the default function

— the total time it took to execute the function dropped_iterations — number of iterations that couldn’t be started

— number of that couldn’t be started data_received — amount of data received

— amount of data received data_sent — amount of data sent

— amount of data sent checks — rate of successful checks (will be discussed later)

In addition, it’ll generate the following output as well if you’re calling HTTP requests:

http_reqs — total requests generated by k6

— total requests generated by k6 http_req_blocked — time spent waiting for a free TCP connection before initiating the request

— time spent waiting for a free TCP connection before initiating the request http_req_connecting — time spent establishing a TCP connection

— time spent establishing a TCP connection http_req_tls_handshaking — time spent on TLS handshaking

— time spent on TLS handshaking http_req_sending — time spent on data sending

— time spent on data sending http_req_waiting — time spent waiting for a response from the remote host

— time spent waiting for a response from the remote host http_req_receiving — time spent on data receiving

— time spent on data receiving http_req_duration — total time for the request. It’s calculated based on http_req_sending + http_req_waiting + http_req_receiving.

You can specify other arguments as well. Simply run the following to get the full list of the available arguments:

k6 help

Available HTTP methods

At the time of this writing, k6 comes with the following HTTP methods.

batch — multiple HTTP requests in parallel

— multiple HTTP requests in parallel del — DELETE request

— DELETE request get — GET request

— GET request options — OPTIONS request

— OPTIONS request patch — PATCH request

— PATCH request post — POST request

— POST request put — PUT request

— PUT request reques — issue any type of HTTP request

HTTP POST request

Here’s another example of HTTP POST , which takes JSON data as its input parameters.