Gating Releases with k6 Thresholds: Encoding SLOs Directly in Load Tests
Use k6 thresholds to encode SLOs like p(95) latency and error rate directly in load-test scripts, then gate CI/CD releases on the exit code. Includes a worked example with tagged endpoints and practical limitations.
17 Aug 2025, 15:39 UTC

The Problem: Performance Regressions Slip Past CI
Your CI pipeline passes. Unit tests are green. Integration tests pass. But the new deployment pushes 95th-percentile latency from 200 ms to 800 ms, and users notice before your monitoring alerts fire. The gap? Most pipelines verify correctness, not performance service-level objectives (SLOs).
k6 thresholds close that gap. They let you express SLOs—like "p(95) latency under 500 ms" or "error rate below 1%"—as JavaScript expressions in the test script itself. When a threshold fails, k6 exits non-zero, giving your CI system a concrete signal to block the release.
How Thresholds Differ from Checks
k6 has two assertion mechanisms, and confusing them is common. Checks run per-request (or per-iteration) and record pass/fail for each sample. They're great for validating response codes, JSON schemas, or business logic on every hit. Thresholds evaluate aggregated metrics after the full test run—percentiles, rates, counts, averages—and produce a single pass/fail verdict for the whole execution.
Think of checks as unit tests for individual requests. Thresholds are the integration gate: "Did the system as a whole meet its SLO?"
Worked Example: Encoding a Latency and Error-Rate SLO
Suppose your team owns an order API. The SLO: 95th-percentile response time ≤ 500 ms, and HTTP error rate < 1%. Here's a minimal k6 script that encodes both:
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
thresholds: {
'http_req_duration': ['p(95)<500'],
'http_req_failed': ['rate<0.01'],
},
vus: 20,
duration: '30s',
};
export default function () {
const res = http.get('https://api.example.com/orders');
check(res, { 'status is 200': (r) => r.status === 200 });
sleep(1);
}
Run it locally:
k6 run script.js
# or quietly, for CI:
k6 run --quiet script.js
echo $? # 0 = all thresholds passed, non-zero = at least one failed
The terminal output includes a thresholds summary section listing each expression with a ✓ or ✗. In CI, the exit code is what your pipeline consumes—GitHub Actions, GitLab CI, Jenkins, and others treat non-zero as a failed job.
Scoping Thresholds to Specific Endpoints
The example above applies thresholds globally. Real services often have different SLOs per endpoint. Use tags to scope thresholds:
export const options = {
thresholds: {
'http_req_duration{endpoint:orders}': ['p(95)<500'],
'http_req_duration{endpoint:health}': ['p(99)<100'],
'http_req_failed{endpoint:orders}': ['rate<0.01'],
},
};
export default function () {
const orderRes = http.get('https://api.example.com/orders', {
tags: { endpoint: 'orders' },
});
const healthRes = http.get('https://api.example.com/health', {
tags: { endpoint: 'health' },
});
// ...
}
Now the order endpoint has a 500 ms p(95) budget while the health endpoint gets a stricter 100 ms p(99). Each tagged metric series is evaluated independently.
Trade-offs and Limitations
- No mid-run abort. Thresholds are evaluated after the test completes. A 30-minute soak test that breaches its p(95) at minute 5 still runs the remaining 25 minutes. For fast feedback, keep threshold-gated tests short (smoke or load profiles) and run longer soak tests separately without gating.
- Percentile stability needs samples. p(95) and p(99) on 50 requests are noisy. Aim for at least a few hundred completed iterations per tagged series before trusting percentile thresholds as a gate.
- Thresholds ≠ alerts. They're a release gate, not an observability tool. Pair them with Prometheus/Grafana alerts on the same SLOs for production visibility.
- Version stability. Threshold syntax is stable in k6 OSS, but cloud-specific features (e.g., k6 Cloud result streaming) may change. Pin your k6 version in CI and review release notes before upgrading.
Verifying the Gate Works
Before trusting the gate in production CI, run a quick verification:
- Create a script with an intentionally failing threshold (e.g.,
p(95)<1). - Run
k6 run --quiet script.jsand confirm the threshold summary shows ✗. - Check
echo $?returns non-zero. - Flip the threshold to a passing value and repeat; exit code should be 0.
This confirms your CI runner will actually block on threshold failures.
Next Steps
Add one threshold-gated load test to your CI pipeline this week. Start with a single critical endpoint, a 30-second load profile, and the two SLOs most likely to regress (latency percentile and error rate). Once the gate is green consistently, expand coverage endpoint by endpoint. The goal isn't perfect coverage on day one—it's a reliable signal that stops the next performance regression before it reaches users.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.