How to Configure k6 Thresholds as a CI Gate
A k6 threshold pins a metric to a pass condition; if the run ends outside it, k6 exits non-zero and CI fails.
Thresholds live under options.thresholds, keyed by metric name. Use abortOnFail to stop the test the moment a threshold is crossed instead of waiting for the full duration.
Steps
- Pick the metrics that define acceptable performance (
http_req_duration,http_req_failed). - Express each as a condition string such as
p(95)<800orrate<0.02. - Add
abortOnFail: trueto fail fast on a bad build.
Threshold config
k6-script.js
export const options = {
thresholds: {
http_req_duration: [
{ threshold: 'p(95)<800', abortOnFail: true },
{ threshold: 'p(99)<1500' },
],
http_req_failed: ['rate<0.02'],
checks: ['rate>0.99'],
},
};Gotchas
- Thresholds evaluate over the whole run by default; scope them with tags for per-endpoint gates.
abortOnFailhas adelayAbortEvalso early noisy samples do not trip the gate prematurely.
Related guides
How to Run k6 Load Tests in CI With GitHub ActionsRun a k6 load test in CI with the grafana/setup-k6-action, letting the script thresholds decide pass or fail…
How to Gate CI on API Response TimesGate CI on API response times by asserting per-endpoint p95 with k6 tags and scoped thresholds, so a slow end…