Thresholds and custom metrics are k6βs way of turning raw measurements into enforceable quality gates. Advanced usage lets you define thresholds on specific tags or custom metrics that represent business-level conditions.
Thresholds on Built-In and Tag-Filtered Metrics
You can define thresholds not only on global metrics like http_req_duration but also on metrics filtered by tags, such as a particular endpoint or scenario. This enables fine-grained gates, like requiring faster responses on checkout than on less critical pages.
export const options = {
thresholds: {
http_req_duration: ['p(95)<800'],
'http_req_duration{journey:checkout}': ['p(95)<600'],
checks: ['rate>0.99'],
},
};
Custom Metrics and Business-Level Thresholds
k6 supports custom counters, gauges, trends and rates that you can update from scripts to represent domain-specific events. You can then define thresholds on these custom metrics, such as the rate of failed logins or the average time for a composite transaction.
import { Trend } from 'k6/metrics';
const checkoutTime = new Trend('checkout_time');
export const options = {
thresholds: {
checkout_time: ['p(95)<1200'],
},
};
export function checkoutFlow() {
const start = Date.now();
// ... perform multiple HTTP calls ...
const elapsed = Date.now() - start;
checkoutTime.add(elapsed);
}
Advanced thresholds and custom metrics make performance gates more expressive and aligned with business goals.
Common Mistakes
Mistake 1 β Using only a single global threshold
This hides critical hotspots.
β Wrong: Treating all endpoints as equally important.
β Correct: Apply stricter thresholds on high-impact journeys.
Mistake 2 β Creating custom metrics with no clear consumer
This adds noise.
β Wrong: Tracking everything just because it is possible.
β Correct: Define metrics that support specific questions or decisions.