How to use res.timings.duration for groups?

Hi @Daniil,

your title:

How to use res.timings.duration for groups?

and

I need to calculate the load metric for each of the API methods per request. Is it correct to do this using the timings.duration metric in the following script?

don’t add up for me … :frowning: I guess you changed what you meant to ask or how you phrased the question between writing title and the body.

I expect that what you want is to group “same” requests together and see their average/max/p95 requests duration?

If you want to do it using only k6 and the summary output. What you are doing is probably what you should be.

I would argue you have outgrown the summary output and should be using another result output for better analyzing and visualizing.

Every request in k6 is measured and metrics for it are emitted with additional tags including url and name . url is obviously just that - the URL used, the name is a “special” tag that is either the url or if set can be used to group different URLs together. As the docs show this is mostly useful when there are query parameters which make the url different but this actually are the same requests. Also obviously you can use it to make very long and unreadable urls more readable by just naming them stuff. I propose using the json output to with your script to see what other tags there are and their values :D.

The k6 cloud automatically groups requests per their name, which when it isn’t set is just the url. This can be done in any other result output, albeit you will need to do it. If you choose Grafana you can reuse another user’s Grafana dashboards for example this one seems to have grouping by name, given screenshot,, but I haven’t used it :D.

If you still want to use summary output only, you could specify a threshold per each different request , for example using its name and it will be shown in the output:

import http from "k6/http";
export let options = {
    thresholds : {
            'http_req_duration{name:"my cool request"}': ["max>0"],
            'http_req_duration{name:"not a cool request"}': ["max>0"],
    }
}
export default function() {
    let res = http.get("https://httpbin.test.k6.io/status/400", {"tags": {"name": "my cool request"}});
    res = http.get("https://httpbin.test.k6.io/status/401", {"tags": {"name": "not a cool request"}});
}

would produce:


The threshold can be whatever it’s there only to make k6 actually put this in the output. The major upside is that you won’t need to add a ton of additional metrics and always adding to them. The major downside is that you can wrongly define a threshold and it will not tell you anything as it hasn’t failed :frowning: .

2 Likes