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!