Skip to main content
Metrics are the heart of k6 performance testing. k6 automatically collects built-in metrics and allows you to define custom metrics for your specific testing needs.

Metric Types

k6 supports four metric types, defined in metrics/metric_type.go:

Counter

A metric that sums all added values. From metrics/metric_type.go:
Aggregation methods: count, rate

Gauge

A metric that stores the latest value. From metrics/metric_type.go:
Aggregation methods: value

Trend

A metric that tracks all values and calculates statistics. From metrics/metric_type.go:
Aggregation methods: avg, min, max, med, p(N) (percentiles)

Rate

A metric that tracks the percentage of non-zero values. From metrics/metric_type.go:
Aggregation methods: rate

Built-in Metrics

k6 automatically collects comprehensive metrics defined in metrics/builtin.go:

HTTP Metrics

From metrics/builtin.go, HTTP-related metrics:

http_reqs

Type: Counter
Description: Total number of HTTP requests

http_req_failed

Type: Rate
Description: Rate of failed requests (status >= 400 or network error)

http_req_duration

Type: Trend (Time)
Description: Total request time (sending + waiting + receiving)

http_req_blocked

Type: Trend (Time)
Description: Time blocked before initiating request (waiting for free TCP connection)

http_req_connecting

Type: Trend (Time)
Description: Time spent establishing TCP connection

http_req_tls_handshaking

Type: Trend (Time)
Description: Time spent in TLS handshake

http_req_sending

Type: Trend (Time)
Description: Time spent sending request data

http_req_waiting

Type: Trend (Time)
Description: Time spent waiting for response (TTFB - Time To First Byte)

http_req_receiving

Type: Trend (Time)
Description: Time spent receiving response data

VU and Iteration Metrics

vus

Type: Gauge
Description: Current number of active virtual users

vus_max

Type: Gauge
Description: Maximum number of VUs initialized

iterations

Type: Counter
Description: Total number of completed iterations

iteration_duration

Type: Trend (Time)
Description: Time to complete one full iteration

dropped_iterations

Type: Counter
Description: Iterations that couldn’t start due to time constraints

Check Metrics

checks

Type: Rate
Description: Success rate of checks

Group Metrics

group_duration

Type: Trend (Time)
Description: Time spent inside a group

WebSocket Metrics

From metrics/builtin.go, WebSocket-specific metrics:
  • ws_sessions (Counter) - Total WebSocket sessions
  • ws_msgs_sent (Counter) - Messages sent
  • ws_msgs_received (Counter) - Messages received
  • ws_ping (Trend, Time) - Ping duration
  • ws_session_duration (Trend, Time) - Session duration
  • ws_connecting (Trend, Time) - Connection time

gRPC Metrics

grpc_req_duration

Type: Trend (Time)
Description: gRPC request duration

Network Metrics

data_sent

Type: Counter (Data)
Description: Amount of data sent (bytes)

data_received

Type: Counter (Data)
Description: Amount of data received (bytes)

Custom Metrics

Create custom metrics to track application-specific data. From examples/custom_metrics.js:

Metric Tags

From metrics/tags.go, metrics can be filtered using tags:

System Tags

Automatic tags from metrics/system_tag.go:
  • method - HTTP method
  • status - HTTP status code
  • url - Request URL
  • name - Request name
  • group - Group name
  • check - Check name
  • scenario - Scenario name
  • service - Service name (for gRPC)

Custom Tags

Add custom tags to requests:

Global Tags

Apply tags to all metrics:

Metric Output

End-of-Test Summary

k6 displays metric statistics at test completion:

Metric Value Types

From metrics/value_type.go, metrics can represent:
  • Default - Generic numeric values
  • Time - Duration values (milliseconds)
  • Data - Data size values (bytes)

Advanced Metric Patterns

Tracking Business Metrics

Tracking Error Types

Conditional Metrics

Best Practices

1

Use Built-in Metrics First

Built-in metrics cover most use cases. Only add custom metrics when necessary.
2

Choose the Right Metric Type

Use Counter for totals, Gauge for latest values, Trend for statistics, and Rate for success rates.
3

Add Meaningful Tags

Tag metrics to enable filtering and detailed analysis.
4

Combine Metrics with Thresholds

Use metrics with thresholds to define pass/fail criteria.
Custom metrics are defined in the init context but recorded in the default function.
Too many custom metrics can impact test performance and increase memory usage.
Metrics provide the quantitative data you need to understand system performance and make informed decisions about scalability and reliability.