How to track count of VUs for different flows?

I am currently distributing users across different flows as described in this comment

const options = {
  stages: [
    {target: 70, duration: '20s'},
    {target: 30, duration: '10s'},
    {target: 30, duration: '7s'},
    {target: 0, duration: '10s'}
  ]
};

export default function() {
  if (__VU % 10 ==1) {
    flow1()
  } else {
    flow2()
  }
}

To track the number of VUs for each flow across time, I have tried using a custom metric like this

const flows = new Counter('flows');
export default function() {
  if (__VU % 10 ==1) {
    // type and start tags are added to prevent points overwriting each other https://docs.influxdata.com/influxdb/v0.13/troubleshooting/frequently_encountered_issues/#writing-duplicate-points
    flows.add(+1, {name: 'flow 1', type: 'start', vu: __VU});
    flow1()
    flows.add(-1, {name: 'flow 1', type: 'end', vu: __VU});
  } else {
    flows.add(+1, {name: 'flow 2', type: 'start', vu: __VU});
    flow2()
    flows.add(-1, {name: 'flow 2', type: 'end', vu: __VU});
  }
}

But this does not work because I think for some VUs, the script ends before they register exiting. Looking at the output in influxdb confirms my suspicion as I have 128 starts and 58 ends.

Is it possible to achieve this using another approach?

Hi there,

your approach seems fine to me, though I couldn’t reproduce your issue of different number of start and end metrics, and my tests had the same number of both:
2020-04-03-102515_435x339_scrot

This might mean that your iterations are being cut short for some reason, or that there are errors submitting the metrics to InfluxDB. Did you receive any errors in the test output? What command did you use to run it?

Though if you just want to track the number of VUs for each flow across time, you only need the “+1” (type: start) metrics to do so.

Here’s what that looks like in Grafana:
2020-04-03-102148_620x379_scrot

And here’s the query I used:

HTH,

Ivan

After some internal discussion, the reason you’re seeing different start and end metric counts is because of the way stages currently works. When ramping down VUs k6 might interrupt some iterations without waiting for them to complete. See issue #879. In the upcoming #1007 major overhaul there will be new gracefulStop and gracefulRampDown settings which will allow you to control this better.

For now, instead of using custom metrics, you might get more accurate results if you use groups to separate your flows and the resulting group_duration metric if you want to keep track of the duration of each flow.

In that case your script might look like:

import { group } from 'k6';

export default function() {
  if (__VU % 10 == 1) {
    group('flow1', function() {
      flow1();
    });
  } else {
    group('flow2', function() {
      flow2();
    });
  }
}

And in Grafana you can plot the mean of that metric like so:

… which would be rendered as:
2020-04-03-114557_605x400_scrot

1 Like

k6 v0.27.0 should make this easier: