Sharing of variable with Mutiple VUs

Is there anyway we can share the token for multiple VUs, iterations. For my case. I just want to invoke 1 time to get token dynamically and can reuse the same token for multiple VUs and iterations.

let token;

export default function testPost () {
    group('GET TOKEN', function(){
        token = getCredentials();//group to get token
    })

    group('MY_TESTING_URL', function(){
        if(token){
            const res = http.get("https://loadimpact.com/" + token);
        }
    });

    group('TESTING_URL2', function(){
        if(token){
            const res = http.get("https://loadimpact.com/" + token);
        }
    });
}

Currently, i explicitly add null check to the token to avoid it being invoked multiple times. (this is not a good solution as there are times that it got executed multiple times).

let token;

export default function testPost () {
    if(!token){ // or while(true) then break if found token
        group(‘GET TOKEN’, function() {
            token = getCredentials(); // group to get token
        })
    }

    group(‘MY_TESTING_URL’, function(){
        if(token){
            const res = http.get(“https://loadimpact.com/” + token);
        }
    });
    group(‘TESTING_URL2’, function(){
        if(token){
            const res = http.get(“https://loadimpact.com/” + token);
        }
    });
}

Hi there,

this is a good use case for the setup function. You can return the token in setup() and reuse it for all VUs and iterations.

1 Like