1

How do we code k6 scripts that have dynamic number of stages? Essentially, If in one run I need to have 2 stages with parameters being constant or having arithmetic or geometric progression and in the second run I want to run it for 3/4/5.....n stages.

This is how one defines scenarios/stages in k6:

export let options = {
    scenarios: {
        stage_1: {
            executor: 'constant-arrival-rate',
            rate: __ENV.STAGE_1_RPS,
            timeUnit: '1s',
            startTime: __ENV.STAGE_1_START_TIME,
            duration: __ENV.STAGE_1_DUR,
            preAllocatedVUs: __ENV.STAGE_1_VUS
        },
        stage_2: {
            executor: 'constant-arrival-rate',
            rate: __ENV.STAGE_1_RPS + 100,
            timeUnit: '1s',
            startTime: __ENV.STAGE_2_START_TIME,
            duration: __ENV.STAGE_2_DUR,
            preAllocatedVUs: __ENV.STAGE_2_VUS
    }
}

Using, the above format if one needs to run for n different number of stages, then n different scripts are needed to be written, which seems to be a good approach.

Have been through the k6 docs, but there doesn't seem to be some feature/parameter that can provide the above functionality : https://k6.io/docs/using-k6/

Aadarsh
  • 41
  • 6

1 Answers1

1

After being through multiple resources, came up with this solution that uses javascript basics(Basically, spin up a solution if the framework doesn't have the feature set!)

for(var i = 1; i <= noOfStages; i++) {
        scenariosDynamic["stage_" + i] = {
            executor: 'constant-arrival-rate',
            rate: stageRps,
            timeUnit: '1s',
            startTime: stageStartTime,
            duration: stageRunDuration,
            preAllocatedVUs: 10,
        };
        stageRps = +stageRps + +rampRps;
        stageStartTime += stageDuration;
    }
export let options = {
    scenarios: scenariosDynamic
};

Just create a javascript object with dynamic keys using the bracket notation.

Aadarsh
  • 41
  • 6