Overriding VUs individual scenario

Hi Team,

We have multiple scenarios defined like below.

export const options = {
discardResponseBodies: true,
scenarios: {
contacts: {
executor: ‘constant-vus’,
exec: ‘contacts’,
vus: 50,
duration: ‘30s’,
},
news: {
executor: ‘per-vu-iterations’,
exec: ‘news’,
vus: 50,
iterations: 100,
startTime: ‘30s’,
maxDuration: ‘1m’,
},
},
};

No externally controlled executor. is there any option override the VUs’ while executing for particular scenario.

Hi @Gerard

I’m not entirely sure that I understood what you meant by:

is there any option override the VUs’ while executing for particular scenario.

In the spirit of avoiding misunderstandings: could you elaborate on what you are looking to achieve, please?

Thanks :bowing_man:

Hi @oleiade ,

Scenario like this. We can define the workload on js files and override through cli. This was work perfectly for a single scenario. And how to overide different values with different scenarios setup workload.

Ex : we can override the vuser or duration with the cli by “k6 run --vus 10 --duration 30s script.js”, in case of multiple scenario is there are any options ?

Also how to override the option. Define workload on the script or cli or external config files. Which override which option ?

Thanks for the clarification @Gerard

What I understand as your goal is to be able to override scenario-specific options directly from the command-line flags; rather than from the predefined options in the script.

In that regard, k6 does not allow users to do that from command-line flags. However, you should be able to achieve the same thing by using environment variables, and introducing a bit of logic in your script to use them as you wish.

k6 lets you use environment variables in the context of your test script. By using them, we should be able to achieve roughly what you’re asking for. Say you have a homepage scenario, and you wish its default VUs count to be 50, but want to be able to change that value at runtime. You could define an environment variable, either:

  • via your shell: MY_ENV_VAR=123 k6 run myscript.js
  • or by passing them using the -e key=val flag to the k6 command: k6 run -e my_env_var=123 script.js

You could access those values you’ve set from your script to override the options.scenarios. of your choice.

Here is an example:

import http from "k6/http";

export const options = {
  scenarios: {
    products: {
      executor: "constant-vus",
      exec: "products",
      vus: 50,
      duration: "30s",
    },
    homepage: {
      executor: "per-vu-iterations",
      exec: "homepage",
      vus: 50,
      iterations: 100,
      maxDuration: "1m",
    },
  },
};

if (__ENV.products_vus) {
  options.scenarios.products.vus = __ENV.products_vus;
}

if (__ENV.homepage_max_duration) {
  options.scenarios.homepage.maxDuration = __ENV.homepage_max_duration;
}

export function products() {
  // do something
}

export function homepage() {
  // do something else
}

Running this script as such: k6 run -e products_vus=12 -e homepage_max_duration=2m script.js, or alternatively using: PRODUCTS_VUS=12 HOMEPAGE_MAX_DURATION=2m k6 run script.js, overrides my script’s scenarios options as desired.

Let me know if that’s helpful :bowing_man:

1 Like

How do the same overriding in js script when config defined in json?