How to apply iterations condition for function in K6

1.1k views Asked by At

I want to execute logout function for one time and dropDown function for multiple iterations. Which changes shoul I need in below code.

        executors: {
            logout: {
                type: 'per-vu-iterations',
                exec: 'logout',
                vus: 1,
                iterations: 1,
                startTime: '30s',
                maxDuration: '1m',
                tags: { my_tag: 'LOGOUT'},
            },
       }};
    export function logout() {
        group('Logout API', () => {
            loginFunctions.logout_api();
        })
    }
    export function dropDown() {
        group('Drop Down API', () => {
        loginFunctions.dropDown_api();
        })
    }
    export default function () {
        logout();
        dropDown();
    }

Also without default function it's not working. getting executor default: function 'default' not found in exports this error

1

There are 1 answers

1
na-- On BEST ANSWER

Not sure where you saw executors, that was the old name of the option, before #1007 was merged and released. The new and correct name is scenarios: https://k6.io/docs/using-k6/scenarios

So, to answer your question, the code should look somewhat like this:

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

export let options = {
    scenarios: {
        logout: {
            executor: 'per-vu-iterations',
            exec: 'logout',
            vus: 1, iterations: 1,
            maxDuration: '1m',
            tags: { my_tag: 'LOGOUT' },
        },
        dropDown: {
            executor: 'per-vu-iterations',
            exec: 'dropDown',
            vus: 10, iterations: 10, // or whatever
            maxDuration: '1m',
            tags: { my_tag: 'LOGOUT' },
        },
    }
};

export function logout() {
    console.log("logout()");
    sleep(1);
    // ...
}
export function dropDown() {
    console.log("dropDown()");
    sleep(1);
    // ...
}

Though, depending on your use case, the best place for the logout() code might actually be in the teardown() lifecycle function? See https://k6.io/docs/using-k6/test-life-cycle for more details