Can I overwrite scenario.iterationInTest?

Is posible to overwrite scenario.iterationInTest to 0 ? I need to start from ‘a’ again when the data set is over.
I tried something like scenario.iterationInTest = 0 , but didn’t give me results.

import exec from "k6/execution";

export const options = {
    scenarios: {
        session_endpoint: {
            executor: "ramping-vus",
            stages: [
                {"duration": "0s", "target": 25},
                {"duration": "20m", "target": 25},
                {"duration": "0s", "target": 0}
            ]
        },
    },
};

export function setup() {
    const name = [
        "a",
        "b",
        "c",
        "d",
        "e",
        "f",
        "g",
        "h",
        "i",
        "j",
        "k",
        "l",
        "m",
        "n",
        "o",
        "p",
        "q",
        "r",
        "s",
        "t",
        "u",
        "v",
        "w",
        "x",
        "y",
        "z",
    ];
    console.log("setup() function is executed!");
    return { name };
}

export default function (data) {
    let index = exec.scenario.iterationInTest;
    console.log(
        `The index # is ${index}, The VU: ${__VU}, Iteration number: ${__ITER}, name is No. ${index} in ${data.name[index]}`
    );
}

Hi @LucasT1, welcome to the community forum!

scenario.iterationInTest is a thing k6 will provide you and you can not override or change it.

For the particular case you are talking about, which is where it is supposed to be used, you can use modulo operator to “reset” from the beginning.

So if you change to

let index = exec.scenario.iterationInTest % data.name.length;

exec.scenario.iteartionInTest will still go from 0 to infinity, but index will loop between 0 and the length of data.name.

Hope this helps you

1 Like