Performance testing
Performance tests use k6, an open-source load-testing tool driven from the command line, with test scripts written in JavaScript. Install k6 before you start.
A first script
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
vus: 10, // virtual users
duration: '30s', // test duration
};
export default function () {
const res = http.get('https://openent.example.org/api/endpoint');
check(res, {
'status was 200': (r) => r.status === 200,
'response time was < 500ms': (r) => r.timings.duration < 500,
});
sleep(1);
}
optionssets the number of virtual users and the duration.checkasserts on each response, here the status code and the response time.sleeppauses between iterations, as a real user would.
Run it:
k6 run script.js
Reading the results
k6 prints its metrics as the test runs and a summary at the end.
| Metric | Meaning |
|---|---|
http_req_duration | Response time: average, minimum, median, maximum and percentiles |
http_req_failed | Share of requests that failed |
vus | Virtual users currently active |
iteration_duration | Time taken by one full iteration of the script |
checks | Share of assertions that passed |
Ramping the load
Stages raise and lower the number of virtual users over time:
export const options = {
stages: [
{ duration: '1m', target: 20 },
{ duration: '3m', target: 50 },
{ duration: '2m', target: 0 },
],
};
Scenarios run several load profiles in the same test, each with its own executor, number of virtual users and duration.