Documentation for automated readers
A curated documentation index is available at: https://grafana.com/llms.txt
A complete documentation index is available at: https://grafana.com/llms-full.txt
These indexes can help with page discovery before fetching individual documents.
This page is also available in Markdown, which may be easier for automated readers and AI tools to parse than HTML. The Markdown version is available at https://grafana.com/docs/k6/next/using-k6/workaround-iteration-duration.md, or by sending Accept: text/markdown to https://grafana.com/docs/k6/next/using-k6/workaround-iteration-duration/. For broader documentation discovery, the curated index is available at https://grafana.com/llms.txt and the complete index is available at https://grafana.com/llms-full.txt.
This is documentation for the next version of Grafana k6 documentation. For the latest stable release, go to the latest version.
Workaround to calculate iteration_duration
A common requested case is to track the iteration_duration metric without including time spent for setup and teardown functions.
This feature is not yet available but a threshold on iteration_duration or any pre-existing metrics can be used as a workaround.
It’s based on the concept of creating thresholds for sub-metrics created by tags for the required scope and setting the criteria that always pass. It works with any enabled tags that already works with threshold, for example:
iteration_duration{scenario:default}generates a sub-metric collecting samples only for the default scenario’s iteration.scenario:defaultis used because that’s the internal k6 scenario name when we haven’t specifiedoptions.scenarios` explicitly and are just using the execution shortcuts instead.iteration_duration{group:::setup}oriteration_duration{group:::teardown}create sub-metrics collecting the duration only forsetupandteardown.k6implicitly creates groups forsetupandteardown, and::is the group separator.http_req_duration{scenario:default}can be useful as well for isolating executed long-running requests.
import { sleep } from 'k6';
import http from 'k6/http';
export const options = {
vus: 1,
iterations: 1,
thresholds: {
'iteration_duration{scenario:default}': [`max>=0`],
'iteration_duration{group:::setup}': [`max>=0`],
'iteration_duration{group:::teardown}': [`max>=0`],
'http_req_duration{scenario:default}': [`max>=0`],
},
};
export function setup() {
http.get('https://quickpizza.grafana.com/api/delay/5');
}
export default function () {
http.get('http://test.k6.io/?where=default');
sleep(0.5);
}
export function teardown() {
http.get('https://quickpizza.grafana.com/api/delay/3');
sleep(5);
}Dedicated sub-metrics have been generated collecting samples only for the scope defined by thresholds:
█ THRESHOLDS
http_req_duration{scenario:default}
✓ 'max>=0' max=117.34ms
iteration_duration{group:::setup}
✓ 'max>=0' max=0s
iteration_duration{group:::teardown}
✓ 'max>=0' max=0s
iteration_duration{scenario:default}
✓ 'max>=0' max=1.13sWas this page helpful?
Related resources from Grafana Labs

