k6: Virtual Users, Checks & Thresholds
k6 is an open-source load testing tool for testing API/website performance and reliability under simulated traffic. Test scripts are written in familiar JavaScript, but executed via a fast Go-based runtime -- meaningfully more efficient than spawning a full browser or Node.js process per simulated user.
A Basic Test Script
import http from 'k6/http'
import { check, sleep } from 'k6'
export const options = {
vus: 50, // 50 concurrent virtual users
duration: '30s',
}
export default function () {
const res = http.get('https://test.example.com/api/products')
// check() records pass/fail WITHOUT stopping the test -- the load
// test keeps generating traffic regardless of individual failures
check(res, {
'status is 200': (r) => r.status === 200,
'response has products': (r) => JSON.parse(r.body).length > 0,
})
// Simulates realistic 'think time' between requests, rather than
// hammering the target as fast as technically possible
sleep(1)
}Thresholds: Automated Pass/Fail for CI
export const options = {
vus: 50,
duration: '30s',
thresholds: {
// k6 exits with a non-zero status if breached -- lets a CI
// pipeline automatically fail the build on a regression
http_req_duration: ['p(95)<500'], // 95% of requests under 500ms
http_req_failed: ['rate<0.01'], // error rate under 1%
},
}Running Tests
k6 run script.js
# Override options from the command line for quick ad-hoc adjustments
k6 run --vus 100 --duration 1m script.jsKeep your own version of these notes — editable, searchable, and organised by your stack.
Start free