K6

K6 is an open-source load testing program for API's via Javascript.

Basics

$ brew install k6

Create a new file, script.js, to hold your basic script:

import http from 'k6/http';
import { sleep } from 'k6';

export default function () {
  http.get('https://test.k6.io');
  sleep(1);
};

Now run your new test in your terminal with k6 run script.js.

Design

You can design your tests with startup code that runs only once and the defaut function, which will be run over and over for the duration of the test by each virtual user.

// init code

export default function() {
  // vu code
}

Metrics

Field Description
http_req_blocked Time spent blocked (waiting for a free TCP connection slot) before initiating the request.
http_req_connecting Time spent establishing TCP connection to the remote host.
http_req_duration http_req_sending + http_req_waiting + http_req_receiving
http_req_failed The rate of failed requests according to setResponseCallback.
http_req_receiving Time spent receiving response data from the remote host.
http_req_sending Time spent sending data to the remote host.
http_req_tls_handshaking Time spent handshaking TLS session with remote host
http_req_waiting Time spent waiting for response from remote host (a.k.a. "time to first byte", or "TTFB").
http_reqs Total requests
iteration_duration The time it took to complete one full iteration of the default/main function.
iterations The aggregate number of times the VUs in the test have executed the JS script (the default function).
vus_max Max possible number of virtual users

Options

Options can be included in your script as an export called options:

export let options = {
  vus: 10,
  duration: '30s',
};

If these options are to be used in a CLI, they should be prefaced with two hyphens and followed directly by their argument.

Flag Args Effect
duration n: string formatted as {number}s, indicating number of seconds Duration of test
vus n: integer Number of virtual users to be utilized in the test

References

Last modified: 202401040446