Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
stages: [
{ duration: '1m', target: 20 },
{ duration: '3m', target: 20 },
{ duration: '1m', target: 0 },
],
thresholds: {
http_req_failed: ['rate<0.01'],
http_req_duration: ['p(95)<500'],
},
};
export default function () {
const res = http.get('https://api.example.com/products');
check(res, { 'status is 200': (r) => r.status === 200 });
sleep(1);
}k6 run load.jsStep by step
- Test a copy, not production — a staging system of the same size, or production only in a planned window and with the team informed.
- Smoke test first — 1 user for 30 seconds proves the script works:
k6 run --vus 1 --duration 30s load.js- Load test (quick answer) — ramp to the normal peak (20 users), hold, ramp down. Each virtual user runs the
defaultfunction again and again;sleep(1)acts like a person thinking. - Thresholds are the goal — fewer than 1 % failed requests and 95 % of requests under 500 ms. If a threshold fails, k6 marks it with ✗ and exits with code 99, so CI fails.
- Real requests — log in, send JSON, use a token from the environment (never in the file):
const params = { headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${__ENV.TOKEN}` } };
const res = http.post('https://api.example.com/orders', JSON.stringify({ sku: 'A-100', qty: 1 }), params);k6 run -e TOKEN="$(cat /run/secrets/test-token)" load.js- Other test types — stress (ramp past the peak to find the breaking point), spike (0 → 200 users in 10 s), soak (normal load for 2–4 hours to find memory leaks).
- Read the summary — look at
http_req_duration(avg, p(90), p(95)),http_req_failedanditerations. Watch the server dashboards at the same time: CPU, database connections, error logs. - Store results over time — send metrics to Prometheus and compare runs in Grafana:
K6_PROMETHEUS_RW_SERVER_URL=http://prometheus.example.com:9090/api/v1/write k6 run -o experimental-prometheus-rw load.jsCheck that it worked
The run ends with all thresholds marked ✓ and exit code 0 (echo $?); the request count matches the plan (about 20 users × 1 request per second); the server dashboards show the load at the same time.
Common problems
| Problem | Cause | Fix |
|---|---|---|
Many 429 responses | Rate limit or WAF blocks the test. | Allow the test machine's IP in staging; or test behind the limiter on purpose. |
| The load machine is at 100 % CPU | The client is the bottleneck, not the API. | Bigger machine, fewer checks per request, or distributed k6. |
dial tcp: too many open files | File descriptor limit. | ulimit -n 65535 before the run. |
| Results look too good | Responses served from a cache or CDN. | Vary URLs/parameters; test the origin as well. |
| CI is green though times were bad | No thresholds set. | Add thresholds; k6 then exits with code 99. |
| Test data fills the database | Every iteration creates orders. | Use a test tenant; clean up in teardown(). |
Go further
- Command pages: k6, ab, curl.
- Next how-tos: Define an SLO and an error budget, Find out why a server is slow, Trace requests with OpenTelemetry, Find slow PostgreSQL queries.
- Learn it: Observability tools at work — load tests. Full guide: k6.
1Why does a k6 test need thresholds?
Thresholds turn the goal into pass or fail: when one fails, k6 exits with a non-zero code and the pipeline stops.
2What is the difference between a load test and a stress test?
A load test checks the normal peak; a stress test goes past it on purpose to find where the system breaks.