Search code examples
k6

How to get group duration value for group


I have a K6 test where load testing a flow and I'm grouping each page request. What I want to do is perform a check on each group's duration so something like


  group('Home page', function(){
         http.get('localhost:8080')
   });
   //something like this 
   check(group.duration, function()=>{'performs check logic'})

   group('search page', function(){
    http.get('localhost:8080/Search?term=mysearch')
   })
   check(group.duration, function()=>{'performs check logic'})

The documentation states that group emits group_duration but doesn't state how to listen for it. Not strong in JS so my apologies if this is something simple.


Solution

  • Groups currently do not return their duration.

    Emit in that contexts means that if you are using json output/influxdb/stastsd/load impact's cloud for metrics storage than a metric that is that group duratioon will be send and you can later ... do whatever you want from inside the metric storage.

    Also the syntax for check is check(variable_name_to_use, map_of_name_to_function_to_calls) so something like

    check(group.duraiton, {
      "checking duration is less than 20 as normal function": function(d){ return d<20}, 
      "checking duration is less than 20 as arrow function": (d)=>{ return d<20}, 
      "checking duration is less than 20 as arrow function without the brackets": (d)=>d<20, 
    })
    

    You would also need to provide the scheme for the url when using http.*.

    I would also like to point out that the idea of group is to measure how much a given group(!) of requests or operations takes. They can for example be used to group all the operations that comprise your "login", "adding an item to a cart", "checkout and payment" and so on. They are definitely useful but grouping single requests when all your groups are single request is not adding any new data :)

    Solution 1: you can do it yourself like this:

    import { check, group } from "k6";
    import http from 'k6/http';
    
    export default function() {
            var group_duration;
        group('Home page', function(){
                    var start = new Date();
            http.get('http://localhost:8080');
                    group_duration = new Date() - start;
        });
        check(group_duration,  {
                    "Name of group check": function(d){return d<200}
            })
    
        group('search page', function(){
                    var start = new Date();
            http.get('http://localhost:8080/Search?term=mysearch');
                    group_duration = new Date() - start;
        })
            console.log(group_duration);
        check(group_duration,  {
                    "Name of group check": (d)=>d<200
            })
    }
    

    You can of course move this to a function and just return the duration as:

    function mygroup(name, f) {
            var start = new Date();
            group(name, f);
            return new Date() - start;
    }
    

    And than call mygroup and get the duration as

    var group_duration = mygroup('Home page', function(){
        http.get('http://localhost:8080');
    });
    

    Solution 2: In your case where you are using only a single request in each group you can use the fact that request do actually return how much they took. So

    resp = http.get("google.com");
    check (resp.timings.duration, {
        "check response time", (d) => d<200
    });
    

    Will check that a given request took less than 200 millisecond. You can see more things that are in the response in the docs

    As a k6 developer I think that what you are proposing is not a bad idea and I would like if you actually open an issue