Skip to main content
Thresholds are pass/fail criteria for your load tests. They specify the acceptable limits for your metrics, allowing you to automate test evaluation.

What are Thresholds?

From metrics/thresholds.go, thresholds are expressions that evaluate metric values against defined criteria. If a threshold fails, k6 exits with a non-zero status code.
When thresholds fail, k6 exits with code 99 (from errext/exitcodes).

Threshold Syntax

From metrics/thresholds_parser.go, threshold expressions follow this format:

Supported Operators

From metrics/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

From metrics/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 from examples/thresholds.js:
All thresholds must pass for the test to succeed.

Thresholds on Tagged Metrics

From examples/thresholds.js, filter metrics by tags:

Aborting on Threshold Failure

From metrics/thresholds.go, use abortOnFail to stop tests immediately when a threshold fails:
From the source code:
  • When abortOnFail is true and the threshold fails, ts.Abort is set
  • The test terminates early
  • Useful for preventing wasted test time when critical issues occur

Abort Grace Period

From metrics/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

From examples/thresholds_readme_example.js:

Threshold Validation

From metrics/thresholds.go, the Validate() function ensures:
  1. The metric exists in the registry
  2. The aggregation method is supported for the metric type
  3. The threshold expression is syntactically correct
Invalid thresholds cause k6 to exit with exitcodes.InvalidConfig.

Common Validation Errors

Threshold Execution

From metrics/thresholds.go, the Run() method:
  1. Collects sink values (aggregated metrics)
  2. Evaluates each threshold expression
  3. Sets LastFailed flag on failing thresholds
  4. Determines if test should abort

Sink Values

From metrics/thresholds.go, different sinks provide different values: CounterSink:
  • count - Total value
  • rate - Value per second
GaugeSink:
  • value - Current value
TrendSink:
  • min, max, avg, med - Statistics
  • p(N) - Percentiles (calculated on demand)
RateSink:
  • rate - Ratio of true values to total

Thresholds in CI/CD

Thresholds make k6 tests suitable for automated pipelines:

Exit Codes

  • 0 - All thresholds passed
  • 99 - One or more thresholds failed (from errext/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.
Too strict thresholds cause false failures. Base thresholds on realistic performance expectations.
Combine thresholds with custom metrics to track business-specific SLOs (Service Level Objectives).

Common Threshold Patterns

API Performance SLO

Multi-Environment Thresholds

Progressive Thresholds

Thresholds transform k6 from a measurement tool into an automated testing framework, enabling continuous performance validation in your development pipeline.