1

I am creating a test with two scenarios with different amount of users and ramp-up time. The idea is that each user shall login once to retrieve a SSO-cookie, that shall be used in subsequent requests. The user data is stored in one json-file and each group of users are parsed to two shared arrays, one for each scenario. The users are grouped in the JSON with different roles and accesses rights so they can't be mixed.

The below code works pretty well if you don't have that many users and "correct" sleep in business logic part, else it will get out sync in regards of exec.scenario.iterationInTest. I am not able to use VU id either since VU id is per test and not per scenario.

Anyone who has any workaround for this?

At the moment I am calling a third party API-counter to get numbers in order to get unique users for each scenario but I don't like that solution. It would be great to have a unique VuserID per scenario starting from 1.

import { SharedArray } from "k6/data";
import jsonpath from "https://jslib.k6.io/jsonpath/1.0.2/index.js"

import { login } from '../requests/login.js';

const testdata = JSON.parse(open('../data/testdata.json'));
const usersA = new SharedArray("usersA", function () { return jsonpath.value(testdata, '$.usersA') });
const usersB = new SharedArray("usersB", function () { return jsonpath.value(testdata, '$.usersB') });

let user;
const usersARampUp = usersA.length * 2;
const usersBRampUp = usersB.length * 9;


export const options = {
    scenarios: {
        loginUserA: {
            executor: 'ramping-vus',
            exec: 'userA',
            startVUs: 0,
            stages: [
                { duration: usersARampUp + 's', target: usersA.length },
                { duration: '10m', target: usersA.length }
            ]
        },
        loginUserB: {
            executor: 'ramping-vus',
            exec: 'userB',
            startVUs: 0,
            stages: [
                { duration: usersBRampUp + 's', target: usersB.length },
                { duration: '10m', target: usersB.length }
            ]
        }
    }
};

export function userA() {
    let dataCounter = exec.scenario.iterationInTest;

    if (__ITER == 0) { //Only login once per test to retrieve SSO cookie
        user = usersA[dataCounter];
        login(user.userName, user.password); //SSO-cookie is set in cookieJar
    }
  //Business logic after login and SSO cookie is retrieved
}

export function userB() { 
    let dataCounter = exec.scenario.iterationInTest;

    if (__ITER == 0) { //Only login once per test to retrieve SSO cookie
        user = usersB[dataCounter];
        login(user.userName, user.password); //SSO-cookie is set in a cookieJar
    }
  //Business logic after login and SSO cookie is retrieved
}
Ricardo Sanchez
  • 4,935
  • 11
  • 56
  • 86
Mathog
  • 11
  • 2

0 Answers0