How to get the group duration timiings

I am using k6 versions k6 v0.43.1 ((devel), go1.20.1, darwin/amd64) on Mac OS ventura 13.2.1, and my requirement is to get the group timining for the request. I was trying this sample script

import { group } from 'k6/metrics';
import http from 'k6/http';

export default function () {
  group('my-group', function () {
    http.get('https://test.k6.io');
    console.log(group.duration('my-group'));
  });
}

but everytime getting either ''TypeError: Value is not an object: undefined" error or “TypeError: Object has no member ‘duration’”

Please help.

Hi @pabhishek24

Welcome to the community forum :wave:

The error you are getting is caused by the import. You should import import { group } from 'k6';, not import { group } from 'k6/metrics';.

After that, the next line that fails is console.log(group.duration('my-group')); fails. So remove that bit and the script works.

import { group } from 'k6';
import http from 'k6/http';

export default function () {
    group('my-group', function () {
        http.get('https://test.k6.io');
    });
}

I understand the goal is to get the group duration, and that will be available with the results. What type of output are you using, end-of-test, or real-time?

If you run, for example, with csv output, k6 run --out csv=test_results.csv script.js, you would see in the resulting CSV the http_req_duration for my-group:

metric_name,timestamp,metric_value,check,error,error_code,expected_response,group,method,name,proto,scenario,service,status,subproto,tls_version,url,extra_tags,metadata
...
http_req_duration,1681672047,105.724000,,,,true,::my-group,GET,https://test.k6.io,HTTP/1.1,default,,200,,tls1.3,https://test.k6.io,,
...

I hope this helps.

Cheers!

I want to use this data for realtime to capture the response tme for multiple https request in a group and there are multple group in my script - how can i do that ?

Hi @pabhishek24

I might not understand correctly what you meant, though you can create several groups in the script, you are not restricted to one. See the groups documentation and an example in Advanced API flow.

For realtime, you will be able to filter the metrics based on each group.

Cheers!