Skip to content

k6 Threshold Expression Parse Error Fix

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about k6 threshold expression parse error fix. We cover key concepts, practical examples, and best practices.

Your k6 test fails with threshold expression parse error for http_req_duration — the threshold syntax is wrong, the metric name is incorrect, or the comparison operator is not supported.

The Problem

import http from 'k6/http';

export const options = {
  thresholds: {
    http_req_duration: ['avg < 2000', 'max > 5000'],
    http_req_failed: ['rate < 0.1'],
  },
};

export default function () {
  http.get('https://api.example.com');
}
ERRO[0000] threshold expression parse error for http_req_duration:
  "avg < 2000" — expected '>' or '<' as second token

The threshold syntax uses a different format — the metric aggregation and comparison must be combined differently.

Step-by-Step Fix

1. Use correct threshold syntax

export const options = {
  thresholds: {
    http_req_duration: ['avg<2000', 'max<5000'],
    http_req_failed: ['rate<0.01'],
  },
};

Threshold format: aggregation operator value — no spaces around the operator, aggregation keyword first.

2. Use percentile thresholds

export const options = {
  thresholds: {
    // p95 under 2 seconds
    http_req_duration: ['p(95)<2000'],
    // p99 under 5 seconds
    http_req_duration: ['p(99)<5000'],
  },
};

3. Combine multiple conditions on one metric

export const options = {
  thresholds: {
    http_req_duration: [
      'avg<1000',
      'p(95)<2000',
      'p(99)<5000',
      'max<10000',
    ],
  },
};

4. Tag-specific thresholds

import http from 'k6/http';

export const options = {
  thresholds: {
    'http_req_duration{expected_response:true}': ['p(95)<1500'],
    'http_req_duration{expected_response:false}': ['p(95)<5000'],
  },
};

5. Use custom metric thresholds

import http from 'k6/http';
import { Trend } from 'k6/metrics';

const myTrend = new Trend('api_response_time');

export const options = {
  thresholds: {
    api_response_time: ['avg<500', 'p(95)<1000'],
  },
};

export default function () {
  const res = http.get('https://api.example.com');
  myTrend.add(res.timings.duration);
}

Expected output:

     ✓ http_req_duration........: avg=245ms  p(95)=512ms  p(99)=890ms
     ✓ thresholds on 'http_req_duration': all 4 conditions met

Prevention Tips

  • Use threshold syntax without spaces around operators: avg<2000
  • Use p(95) syntax for percentiles
  • Tag thresholds with {tag:value} for per-endpoint granularity
  • Test thresholds in a short run before full test execution
  • Use http_req_failed for basic health gating

Common Mistakes with thresholds

  1. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  2. Misunderstanding that String is [Char] with poor performance for large text operations
  3. Using foldl instead of foldl' causing stack overflow on large lists

These mistakes appear frequently in real-world K6 code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What aggregation functions are available in thresholds?

Supported aggregations: avg, min, max, med, p(X) for percentiles, count, rate (for rate metrics like http_req_failed), and value (for Gauge metrics).

Can I use thresholds with WebSocket metrics?

Yes, k6 exposes WebSocket metrics like ws_connecting, ws_msgs_received, and ws_msgs_sent. You can set thresholds on these just like HTTP metrics: ws_connecting: ['avg<500'].

How do thresholds interact with multiple scenarios?

Each scenario can have its own thresholds. Thresholds are evaluated per-scenario by default. Use the scenarios option to define per-scenario metrics and thresholds independently.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro