k6 thresholds — how do I fail a build on p95 latency?
I want the CI job to fail if p95 > 500ms or error rate > 1%. What does the threshold syntax look like?
k6thresholdsci
I want the CI job to fail if p95 > 500ms or error rate > 1%. What does the threshold syntax look like?
Accepted solution
Thresholds are k6's pass/fail gate. You declare them in `options.thresholds` against any metric, and if a threshold is breached k6 exits with a non-zero code so CI fails the build.
export const options = {
thresholds: {
http_req_failed: ['rate<0.01'],
http_req_duration: ['p(95)<500', 'p(99)<1200'],
'http_req_duration{endpoint:checkout}': ['p(95)<800'],
},
};Practical rules: always assert on percentiles (p95/p99), never the average — averages hide the tail your users actually feel. Tag critical endpoints and set per-tag thresholds so a slow search endpoint does not mask a fast-but-broken checkout. Add `abortOnFail: true` when you want a smoke test to stop the moment the SLO is broken instead of burning ten minutes of load.