What are Thresholds?
Frommetrics/thresholds.go, thresholds are expressions that evaluate metric values against defined criteria. If a threshold fails, k6 exits with a non-zero status code.
errext/exitcodes).
Threshold Syntax
Frommetrics/thresholds_parser.go, threshold expressions follow this format:
Supported Operators
Frommetrics/thresholds.go, the run() function supports:
>- Greater than>=- Greater than or equal<- Less than<=- Less than or equal==or===- Equal to!=- Not equal to
Examples
Aggregation Methods by Metric Type
Frommetrics/metric_type.go, each metric type supports specific aggregation methods:
Counter Metrics
Supported methods:count, rate
Gauge Metrics
Supported methods:value
Trend Metrics
Supported methods:avg, min, max, med, p(N)
Rate Metrics
Supported methods:rate
Multiple Thresholds
Define multiple thresholds for the same metric fromexamples/thresholds.js:
Thresholds on Tagged Metrics
Fromexamples/thresholds.js, filter metrics by tags:
Aborting on Threshold Failure
Frommetrics/thresholds.go, use abortOnFail to stop tests immediately when a threshold fails:
- When
abortOnFailis true and the threshold fails,ts.Abortis set - The test terminates early
- Useful for preventing wasted test time when critical issues occur
Abort Grace Period
Frommetrics/thresholds.go, delay abort to allow test startup:
Grace periods prevent false positives during test ramp-up when metrics may be unstable.
Real-World Example
Fromexamples/thresholds_readme_example.js:
Threshold Validation
Frommetrics/thresholds.go, the Validate() function ensures:
- The metric exists in the registry
- The aggregation method is supported for the metric type
- The threshold expression is syntactically correct
exitcodes.InvalidConfig.
Common Validation Errors
Threshold Execution
Frommetrics/thresholds.go, the Run() method:
- Collects sink values (aggregated metrics)
- Evaluates each threshold expression
- Sets
LastFailedflag on failing thresholds - Determines if test should abort
Sink Values
Frommetrics/thresholds.go, different sinks provide different values:
CounterSink:
count- Total valuerate- Value per second
value- Current value
min,max,avg,med- Statisticsp(N)- Percentiles (calculated on demand)
rate- Ratio of true values to total
Thresholds in CI/CD
Thresholds make k6 tests suitable for automated pipelines:Exit Codes
0- All thresholds passed99- One or more thresholds failed (fromerrext/exitcodes)- Other codes indicate different errors
Best Practices
1
Start Conservative
Begin with loose thresholds and tighten them as you understand system behavior.
2
Focus on Percentiles
Use p(95) or p(99) instead of averages to catch outliers.
3
Set Multiple Thresholds
Define thresholds for different aspects: latency, error rate, throughput.
4
Use Tags for Granularity
Apply different thresholds to different request types or endpoints.
5
Add Abort Conditions
Use
abortOnFail for critical failures to save resources.