Execution of new scenarios from CLI

Hello!
Maybe I missed the answer to this, but

Let’s assume I have multiple scenarios in my test:

export let options = {
discardResponseBodies: true,
scenarios: {
    bookmarks: {
        executor: 'constant-vus',
        exec: 'bookmarks',
        vus: 2,
        duration: '10s',
    },
    tags: {
        executor: 'per-vu-iterations',
        exec: 'tags',
        vus: 2,
        iterations: 10,
        startTime: '30s',
        maxDuration: '1m',
    },
}
};
export function bookmarks() {
}

export function tags() {  
}

Is there any way to specify and run only one of these scenarios in execution command?

You can’t specify that directly as a CLI argument, sorry, though you can do it with a bit of extra coding and k6 environment variables. Something like this:

const possibleScenarios = {
    bookmarks: {
        executor: 'constant-vus',
        exec: 'bookmarks',
        vus: 2,
        duration: '10s',
    },
    tags: {
        executor: 'per-vu-iterations',
        exec: 'tags',
        vus: 2,
        iterations: 10,
        startTime: '30s',
        maxDuration: '1m',
    },
};

let enabledScenarios = {};

__ENV.SCENARIOS.split(',').forEach(scenario => enabledScenarios[scenario] = possibleScenarios[scenario]);

export let options = {
    discardResponseBodies: true,
    scenarios: enabledScenarios,
};

export function bookmarks() {
}

export function tags() {
}

Then, you can run this script with:

k6 run --env SCENARIOS=bookmarks,tags script.js
k6 run --env SCENARIOS=tags script.js
k6 run --env SCENARIOS=bookmarks script.js

And, while you’d probably want some better error detection, this should be serviceable for any number of scenarios.

2 Likes

That should work. I’ll try. Thank you! :slight_smile:

It doesn’t appear to be possible, however, to be able to configure the ramping-arrival-state executor with the --stage parameter from the CLI. I used the guidance here to create a possibleScenarios containing both constant-vus executor and ramping-arrival-rate executor. When I use --env and the “filter” approach here to select the ramping arrival executor, but specify --stage, it only does constant VU execution.

Any suggestion on how to enable my scenario? E.g. being able to select between constant-vus and ramping-arrival-rate executor and customize the stages for both?

E.g. something like k6 run --stage "1m:1000,2m:2000" --env SCENARIOS=ramp_up script.js

where script.js looks like this:

import http from 'k6/http';
import { check } from "k6";
import { sleep } from "k6";

// stage configuration should be set at command line

const possibleScenarios = {
    constant_vus: {
      executor: 'constant-vus'
    },
    arrival_rate: {
      executor: 'constant-arrival-rate',
      rate: 5000,
      timeUnit: '1s',
      duration: '10m',
      preAllocatedVUs: 6000,
      maxVUs: 10000,
    },
    ramp_up: {
      executor: 'ramping-arrival-rate',
      startRate: 0,
      timeUnit: '1s',
      preAllocatedVUs: 6000,
      maxVUs: 10000,
      stages: [
        { target: 5000, duration: '5m' },
        { target: 5000, duration: '5m' },
      ],
    },
};

let enabledScenarios = {};

__ENV.SCENARIOS.split(',').forEach(scenario => enabledScenarios[scenario] = possibleScenarios[scenario]);

export let options = {
  scenarios: enabledScenarios,
};

export default function () {
...

EDIT:
Looks like this works - but curious about correctness?
k6 run script.js --env SCENARIOS=ramp_up --env STAGES='[ { "target": 1000, "duration": "30s" } ]'
for

import http from 'k6/http';
import { check } from "k6";
import { sleep } from "k6";

// stage configuration should be set at command line

let inputStages = JSON.parse(__ENV.STAGES);

const possibleScenarios = {
    constant_vus: {
      executor: 'constant-vus'
    },
    arrival_rate: {
      executor: 'constant-arrival-rate',
      rate: 5000,
      timeUnit: '1s',
      duration: '10m',
      preAllocatedVUs: 6000,
      maxVUs: 10000,
    },
    ramp_up: {
      executor: 'ramping-arrival-rate',
      startRate: 0,
      timeUnit: '1s',
      preAllocatedVUs: 6000,
      maxVUs: 10000,
      stages: inputStages,
    },
};

let enabledScenarios = {};

__ENV.SCENARIOS.split(',').forEach(scenario => enabledScenarios[scenario] = possibleScenarios[scenario]);

export let options = {
  scenarios: enabledScenarios,
};

export default function () {
...

Thanks!

As was mentioned in the “Breaking changes” of the release notes for v0.27.0 (the first point): any execution options from upper config layers completely overwrites the ones from below.

So any CLI flag about execution will make the in script configuration to not be valid. This was done because otherwise the amount of incompatible ways to configure it were way too many. Also this allows a users to run the test with complex script execution plan with only 1 VU and 1 iteration (to test it) with only providing -u 1 -i 1 :wink:

So in your first try above you are basically overwriting the in script execution with --stages making it into a single ramping-vus executor. In the second case you are just providing some environmental variable that you then use in the script, but no execution configuration CLI flag has been used.

I hope this was clear enough as this gets a little tangled, especially when you consider that you provide the environmental variable through CLI flag. You can use k6 inspect to see what the final configuration for a script will be after k6 has tried to figure out what all the flags and script mean.

Good luck and have fun :wink:

p.s. I recommend not to name your custom environmental variables so similar to k6 ones as this is likely to give you problems :wink: