Global Variables

Is there a way to store values in a global way just like JMeter does with its property function?


Yes, this is indeed possible. Simply declare a variable in the init context, i.e. the top level of your entry point script. Any other imported functions you use will automatically have access to the variable even if it isn’t declared anywhere in those functions.

What I typically do, and something that we do automatically with scripts generated from a recording, is to create a vars object (or array, doesn’t matter) and using that to store things like dynamic IDs. You can see this in action in the example-woocommerce repo (it is declared in main.js here).

Thanks Tom.

What if I have two scenarios where the in the first one I have to make a call to obtain a token and this have to be called every 15min, then for the second scenario I make a POST call using this token. I’d like to know how to stop the thread for 15min without impacting the second scenario.

This is my code:

export function GetToken() {
    let url = new URL(`${AUHTZ_BASE_URL}/token`);

    url.searchParams.append('grant_type', `${grantType}`);
    url.searchParams.append('client_secret', `${clientSecret}`);
    url.searchParams.append('client_id', `${clientID}`);

    const response =;

    return response.json().access_token;

I’m currently returning the token so I can use it in my second scenario. Unfortunately, I can’t stop the thread for 15min due to dependency.

P.S: Sorry for my english, is not my first language

Unfortunately, there is no built-in way for VUs to communicate with each other once they’re inside their default function.

I see two options:

  1. have someone increase the validity period of the token beyond 15min (I’m guessing this is why you’re having to repeat the call?).

  2. use a 3rd party data store to store the latest value, and have your VUs retrieve the latest value from that. Redis would be an option. Something like webdis would allow you to communicate with Redis over HTTP (meaning you shouldn’t need any other libraries to interact with it from k6).

I would also recommend just getting the token when you need it instead of in two separate scenarios and possibly using groups to figure out the time it takes to do the not getting token parts.

You can also (ab)use the fact that vus are reused between scenarios (if possible) and use something like this to go between scenarios.