Skip to main content
The k6/metrics module provides custom metric types for tracking and aggregating performance data in your tests.

Import

import { Counter, Gauge, Rate, Trend } from 'k6/metrics';

Metric Types

Counter

Counter is a cumulative metric that only increases. Use it to count events, errors, or accumulate values.
name
string
required
The name of the custom metric.
Methods:
  • add(value, [tags]) - Add a value to the counter
import { Counter } from 'k6/metrics';

const myCounter = new Counter('my_counter');

export default function () {
  myCounter.add(1);
  myCounter.add(2, { tag1: 'myValue', tag2: 'myValue2' });
}
Threshold variables:
  • count - The total count value
  • rate - The rate of the counter
Examples: count >= 200, count < 10

Gauge

Gauge holds only the latest value added. Use it for tracking current state or the most recent measurement.
name
string
required
The name of the custom metric.
isTime
boolean
Indicates whether the values are time values or untyped values.
Methods:
  • add(value, [tags]) - Add a value to the gauge (only the latest value is kept)
import { Gauge } from 'k6/metrics';

const myGauge = new Gauge('my_gauge');

export default function () {
  myGauge.add(3);
  myGauge.add(1);
  myGauge.add(2, { tag1: 'value', tag2: 'value2' });
}
Threshold variable:
  • value - The current gauge value
Examples: value < 200, value > 1

Rate

Rate tracks the percentage of added values that are non-zero. Use it for calculating success/failure rates.
name
string
required
The name of the custom metric.
Methods:
  • add(value, [tags]) - Add a boolean or numeric value (non-zero counts as true)
import { Rate } from 'k6/metrics';

const myRate = new Rate('my_rate');

export default function () {
  myRate.add(true);
  myRate.add(false);
  myRate.add(1);
  myRate.add(0, { tag1: 'value', tag2: 'value2' });
}
Threshold variable:
  • rate - The rate value between 0.00 and 1.00
Examples: rate < 0.1 (less than 10%), rate >= 0.9 (90% or more)

Trend

Trend calculates statistics on added values (min, max, average, percentiles). Use it for timing measurements and performance analysis.
name
string
required
The name of the custom metric.
isTime
boolean
Indicates whether the values are time values (in milliseconds) or untyped values.
Methods:
  • add(value, [tags]) - Add a value to the trend
import { Trend } from 'k6/metrics';

const myTrend = new Trend('my_trend');

export default function () {
  myTrend.add(1);
  myTrend.add(2, { tag1: 'value', tag2: 'value2' });
}
Threshold variables:
  • avg - Average value
  • min - Minimum value (not recommended for thresholds)
  • max - Maximum value (not recommended for thresholds)
  • med - Median value
  • p(N) - Specific percentile (N between 0.0 and 100.0)
Examples:
  • p(95) < 400 - 95% of requests finish below 400ms
  • p(99) < 1000 - 99% of requests finish within 1s
  • p(50) < 200 - Half of requests finish within 200ms
  • max < 3000 - Slowest request finishes within 3s
Don’t use min and max for thresholds as they represent outliers. Use percentiles instead.

Build docs developers (and LLMs) love