Load Testing with k6: Performance Tests That Live in Git
k6 writes load tests in JavaScript, runs them from one binary and fails the build when a threshold is breached. It is the easiest performance tool to put in a pipeline.
In this article
- What k6 is – and what it is not
- A test, start to finish
- Checks and thresholds are not the same thing
- Executors: choosing the right shape of load
- Running it, and in a pipeline
What k6 is – and what it is not
k6 is a load-testing tool from Grafana Labs. It generates traffic, measures response times and error rates, and checks the result against thresholds you declare. It is not a security scanner and it does not drive a browser by default – k6 browser exists, but the core tool talks to your API.
If someone asks for “security testing with k6”, what they usually want is load testing with k6, or ZAP for security. Worth agreeing which before you start.
A test, start to finish
import http from 'k6/http';
import { check, sleep } from 'k6';
import { SharedArray } from 'k6/data';
const users = new SharedArray('users', () => JSON.parse(open('./users.json')));
export const options = {
stages: [
{ duration: '2m', target: 200 }, // ramp up
{ duration: '15m', target: 200 }, // steady state
{ duration: '2m', target: 0 }, // ramp down
],
thresholds: {
http_req_failed: ['rate<0.01'], // under 1% errors
http_req_duration: ['p(95)<800', 'p(99)<1500'],
'http_req_duration{name:checkout}': ['p(95)<1200'],
},
};
export default function () {
const u = users[Math.floor(Math.random() * users.length)];
const login = http.post(`${__ENV.BASE_URL}/api/login`,
JSON.stringify({ username: u.username, password: u.password }),
{ headers: { 'Content-Type': 'application/json' }, tags: { name: 'login' } });
check(login, {
'login 200': (r) => r.status === 200,
'token returned': (r) => !!r.json('data.token'),
});
const auth = { Authorization: `Bearer ${login.json('data.token')}` };
sleep(Math.random() * 4 + 2); // think time
const cart = http.post(`${__ENV.BASE_URL}/api/cart`,
JSON.stringify({ productId: 'SK-1001', qty: 1 }),
{ headers: { ...auth, 'Content-Type': 'application/json' }, tags: { name: 'checkout' } });
check(cart, { 'added to cart': (r) => r.status === 201 });
sleep(Math.random() * 6 + 3);
}
Checks and thresholds are not the same thing
| check() | threshold | |
|---|---|---|
| Scope | One response | The whole run |
| On failure | Records it, carries on | Exits non-zero – the build fails |
| Use it for | Did this request do the right thing? | Did the run meet the agreement? |
A run with failing checks and no thresholds exits 0 and the pipeline goes green. Thresholds are what make k6 a gate rather than a report.
Executors: choosing the right shape of load
- ramping-vus – the stages above. A fixed population of users, ramped. Good default.
- constant-arrival-rate – a fixed number of iterations per second regardless of how slow the system gets. This is the honest one for a public site.
- ramping-arrival-rate – the same, ramped. Use it for a spike.
- per-vu-iterations – each user does exactly n journeys. Good for a data-driven run where every row must be used once.
Running it, and in a pipeline
k6 run -e BASE_URL=https://staging.example checkout.js
k6 run --out json=raw.json checkout.js
# GitHub Actions
- uses: grafana/setup-k6-action@v1
- run: k6 run -e BASE_URL=${{ secrets.STAGING_URL }} tests/checkout.js
# a breached threshold exits non-zero, so this step fails the build by itself
Key points
- Thresholds fail the build; checks only record. You need both.
- Arrival-rate executors model a public site better than a fixed user count.
- Tag requests, then set a threshold per tag – one slow endpoint should not hide behind a fast one.
- k6 is load testing. For security, use ZAP or Burp.
Practise this on the free applications at jstestingacademy.co.in/qa-apps – no account needed.


