How to get current active VUs count on constant-arrival-rate mode?

I need to get the current active VUs count within a test so I can track its count into a custom metrics.

I’ve tried the solution from here but it didn’t work because it turns out it wasn’t what I’m looking for.

I want to get the active vus that are running as seen on the console app when loadtest is running.

as seen on the screenshot above, I want to get the number 8 on a specific moment in the script. e.g. tracking the vus count before request and after request.


Could you elaborate a bit on your use case? From your description I suspect there might be other, possibly easier, ways to achieve the same result.


I want to make roughly this scenario:

var activeUser = new Trend('activeUser');

export default function() {

    var data,
        res = ws.connect(wsUrl, params, socket => {
        socket.on('open', () => {
            userCountBefore = __VUSCOUNT; // assume this is how to get the vus count
            activeUser.add(userCountBefore, {time: 'before'});
            socket.on('message', msg => {
                data = JSON.parse(msg)
                switch (data.type) {
                    case 'connected':
                        return socket.send(JSON.stringify({type: 'find'}));
                    case 'found':
                        userCountAfter = __VUSCOUNT;
                        activeUser.add(userCountAfter, {time: 'after'});

Also there’s another case that as far I know, I cannot do it because it’s not possible to get the custom metrics value from the script. Initially I want to get the maximum waiting user count for response at a time. It can be done if custom metrics is accessible by using gauge and counter.

Unfortunately, there isn’t a way to get this information right now… :disappointed: Please share your use case and give a :+1: to this issue, so we can prioritize such an API higher: Improve execution information in scripts · Issue #1320 · grafana/k6 · GitHub

I’m not sure I grok your use case exactly, but given the fact you’re using an arrival-rate executor, you should be able to use customMetric.add(+1) at the start of your iteration, and customMetric.add(-1) at its end. That would give you the amount of currently running VUs at any given point in time, if you use some external output. Then you should be able to correlate that with any script logic you want, I think…

Oh, thanks! yeah I could do that I think. I can use the external output of the metrics and then process it.

I think currently this is the most approachable workaround.