How to get total VUs count from within test

Is it possible to access total VUs count for the test ( https://k6.io/docs/using-k6/options#vus ) from within a test? I tried:
console.log(__ENV.K6_VUS);
without success. IMPORTANT: I use a config file (-c option on CLI) to configure the test stages.

I do not want __VU which is the index of the current VU. I need the total count so I can apply test logic based on that count.

Thanks, Patrick

1 Like

Local k6 execution (i.e. k6 run script.js) saves the consolidated script options back into the options object you can access from the script, regardless of where the option came from (script, config file, env var, CLI flag). So, you should just be able to do console.log(options.vus);.

This currently doesn’t work for k6 cloud, because of implementation details, but we’re working on a huge refactoring that will change that, so the above advice should also work in the cloud after we release k6 v0.27.0 (or however we name the next big k6 version).

OK, thank you. Since I need Cloud support. I will wait for the next major version.

For a cloud workaround you can use right now, you can specify the VUs with an environment variable that you then re-export in the options, like this:


export let options = {
    vus: __ENV.my_vus_var,
};

export default function {
    // work with __ENV.my_vus_var
}

Then both k6 cloud --env my_vus_var=10 script.js and k6 run --env my_vus_var=10 script.js would work.

Is this workaround compatible with the file config (see question) using multiple stages?

No, sorry, this won’t be useful with the JSON config. That said, you can implement your own config files by passing the config file path via an environment variable, parsing it in the script like JSON.parse(open(__ENV.my_config_file_path)) and then both re-exporting its contents as the script options and using them in the script for whatever you need.

Clever indeed. We might need this scheme for this purpose and others. Thanks ned.