Show Menu
Cheatography

The K6 cheatsheet is a comprehensive quick reference guide for users of the K6 load testing tool. It covers installation, usage, options, examples, assertions, reporting, and offers valuable tips and tricks. Whether you're a beginner or an experienced K6 user, this cheatsheet provides essential information for effective load testing.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Instal­lation

 
Mac:
brew install k6

Linux:
sudo apt-get install k6

Windows: Download the installer from the K6 website

Usage

 
To create a K6 test script, create a new file with the .js extension. In the file, add the following code:

import http from "­k6/­htt­p";


export default function() {

http.g­et(­"­htt­ps:­//e­xam­ple.co­m");

}


This script will make a GET request to the exampl­e.com website.
To run the test script, use the following command:
k6 run test.js


This will start the K6 test runner and execute the test script.
 

Options

 
You can customize the behavior of the K6 test runner using the following options:
vus
: The number of virtual users to simulate
iterations
: The number of times to repeat the test script
duration
: The duration of the test in seconds
Examples
To run a test with 10 virtual users for 60 seconds, use the following command:
k6 run --vus 10 --duration 60s test.js

To run a test with 100 virtual users for 120 seconds, use the following command:
k6 run --vus 100 --duration 120s test.js

Reporting

 
K6 comes with built-in metrics about the test load and the system response. Key metrics include:

http_r­eq_­dur­ation
, the end-to-end time of all requests (that is, the total latency)
http_r­eq_­failed
, the total number of failed requests
iterations
, the total number of iterations
 

Adding­-ch­eck­s-t­o-y­our­-script

 
Note that you need to import the check from the k6 library:
import { check } from 'k6';


And you need to put the actual check in the default function:
check(­res­ponse, {

 'Appli­cation says hello': (r) => r.body.in­clu­des­('Hello world!')

  });

}

Settin­g-t­est­-cr­ite­ria­-wi­th-­thr­eshold

 
Types of thresh­olds:
1. Error rate
thresh­olds: {

http_r­eq_­failed: ['rate­<=0.05'],

},

2. Response time
thresh­olds: {

 http_r­eq_­dur­ation: ['p(95­)<=­5000'],

},

3. Checks
thresh­olds: {

checks: ['rate­>=0.9'],

},