What are metrics?
Metrics are measurements that k6 collects during test execution. They provide quantitative data about:- Request rates and response times
- Error rates and status codes
- Data transfer and connection times
- Custom application-specific measurements
The four metric types
k6 organizes metrics into four types, each suited for different measurements:- Counter
- Gauge
- Rate
- Trend
Counters sum values over time.Use for: Total number of requests, errors, or events
Built-in metrics
k6 automatically collects metrics for HTTP requests and test execution. You don’t need to do anything to collect these.The RED metrics
The most important metrics follow the RED method (Requests, Errors, Duration):http_reqs
RequestsTotal number of HTTP requests
http_req_failed
ErrorsRate of failed requests
http_req_duration
DurationRequest response time
Other HTTP metrics
k6 also collects detailed HTTP timing metrics:Execution metrics
Example output
When you run a test, k6 displays metrics in the end-of-test summary:Creating custom metrics
Create custom metrics to measure application-specific performance.Basic pattern
1
Import the metric type
2
Create the metric in init context
3
Add measurements in VU code
Custom metric examples
Custom metric output
Custom metrics appear in the end-of-test summary with their aggregated values:Custom metrics are collected at the end of each VU iteration. For long-running tests, metrics might not appear until the first iteration completes.
Tagging custom metrics
Add tags to custom metric values for filtering:Thresholds on custom metrics
Set pass/fail criteria for custom metrics:Complete example
Here’s a comprehensive example using all four metric types:Metric name restrictions
Metric names must:- Contain only ASCII letters, numbers, and underscores
- Start with a letter or underscore
- Be 128 characters or less
Best practices
Start with RED metrics
Monitor http_reqs, http_req_failed, and http_req_duration before diving into custom metrics.
Choose the right type
Use Trend for durations, Rate for percentages, Counter for totals, Gauge for current values.
Tag for analysis
Add tags to metrics to enable filtering by endpoint, status, or other dimensions.
Set thresholds
Define thresholds on metrics to automate pass/fail decisions.
Next steps
Metrics Reference
Complete list of all built-in metrics
Thresholds
Set pass/fail criteria using metrics