Caching Access Tokens

Hello,

I want to run an extended load test for several hours with multiple VUs. I would like for the VUs to share an access token, which I know how to do using the setup stage. The problem is that I haven’t found a way to update this shared/cached token such that all VUs can continue using the same new token after the first token expires. Actually I didn’t even find a way for the same VU to continue using the same new token, it had to get a new token for every request after the first token expired. The system I am testing puts strict rate limits on obtaining an access token, so requesting them many times in a second will lock out the client ID.

Does anyone have any tricks for how I can maintain a single cached token that can be updated later that all VUs can access or update?

Hi @nathanloyer

A warm welcome to the community forum :wave:

It is usually very useful to provide an example in order to help you out.

From what I understand, what you can easily do is have the VUs use a token, and update the token for each VU. For example:

// init import modules
import { sleep } from 'k6'
import exec from 'k6/execution';

// init context: define k6 options
export const options = {
    vus: 3,
    duration: '4s',
};

// init context: global variables
let vuTokens = ['initial-token-user-1', 'initial-token-user-2', 'initial-token-user-3']
let myToken;

export default function () {
    // init token if not defined yet
    if (!myToken) myToken = vuTokens[__VU - 1];
    // update token at iteration 2
    if (exec.vu.iterationInInstance === 2) myToken = `new-token-user-${__VU}`;
    // log VU, iteration, token
    console.log(`[VU: ${__VU}, iteration: ${__ITER}, token: ${myToken} `);
    sleep(1);
}

Would assign a different token at the first iteration. In your case, once it expires, you’d have to request a new token.

INFO[0000] [VU: 1, iteration: 0, token: initial-token-user-1   source=console
INFO[0000] [VU: 2, iteration: 0, token: initial-token-user-2   source=console
INFO[0000] [VU: 3, iteration: 0, token: initial-token-user-3   source=console
INFO[0001] [VU: 3, iteration: 1, token: initial-token-user-3   source=console
INFO[0001] [VU: 2, iteration: 1, token: initial-token-user-2   source=console
INFO[0001] [VU: 1, iteration: 1, token: initial-token-user-1   source=console
INFO[0002] [VU: 3, iteration: 2, token: new-token-user-3   source=console
INFO[0002] [VU: 1, iteration: 2, token: new-token-user-1   source=console
INFO[0002] [VU: 2, iteration: 2, token: new-token-user-2   source=console
INFO[0003] [VU: 1, iteration: 3, token: new-token-user-1   source=console
INFO[0003] [VU: 3, iteration: 3, token: new-token-user-3   source=console
INFO[0003] [VU: 2, iteration: 3, token: new-token-user-2   source=console

We have SharedArray that the VUs can access, but not update. That is, we cannot have is a variable shared between different VUs that the VU code can update.

Would something like the above work for your case, having the VU get their own token when it expires? If you declare the variable in the init section, the VU code should be able to update and reuse it between expirations.

I hope this helps!