VU active or idle

Hello folks is there a “best” thread safe way to determine if a VU is active or idle in any given instance or scenario?

generative AI offer up something I can’t find in k6/grafana doc’s k6.vu.active seems odd and throws an error anyway. The api docs show the VU modules has vu.IdleInTest and vu.IdleInInstance [k6/execution](https://VU Module)

what i’m looking to do is make sure even idle VU’s are forced to fetch or refresh it’s assignment like this

if (k6.vu.active === 0 && needsRefresh()) {
    fetchNew();
  }

should this be

if (idInInstance === 0 && needsRefresh()) {
    fetchNew();
  }

OR

if (idInTest === 0 && needsRefresh()) {
    fetchNew();
  }

thanks in advance

Hello @PlayStay,

I don’t think there’s any exposed mechanism to do so, and to be honest I don’t fully get what you mean with needsRefresh().

However, this looks like a XY problem.

So, could you please try to bring more details about what problem are you experiencing, or what’s the root reason to need to check if a VU is active or idle? In general I’d say it’s discouraged to try to act on these internal VU states, but if I can get what your needs are, I might try to bring a solution.

Thanks!

sorry @joanlopez I’m being vague, only giving slight clues as needed due to internal security policy of my company. So I’ll try again…

I need to refresh an access token periodically “needsRefresh()”. The problem I’m having with k6 is the state of the VU. active vs idle.

my token expires at a predetermined time. meaning it has a TTL. I need to ensure that whether a k6 vu is active or idle when the TTL expires I get a new token so I don’t incur massive http 401 errors.

the routine described above is the pseudo code for what I’ve done so far. minus the function contents because of our corp policies about sharing code.

so far this is producing the desired results. access tokens are updated only when need. not done in bursts. not adding extraneous http calls (like JMeter where most folks create a loop that grabs a token every X-thousand requests). I did not want to create extra overhead with the access token api while hammering the api I’m trying to profile which needs an access token. etc etc…

ultimately my question is this the best way to achieve this goal. namely getting a token periodically while maintaining efficiency of the k6 to all things considered, cpu, memory, threading or is there a suggest way, .idInTest, idInInstance or k6.vu.active.

    if (exec.vu.idInTest === 0 && isTokenExpired(api_token_issued_time)) {
        fetchNewToken(params);
    }
    if (api_token == null || isTokenExpired(api_token_issued_time)) {
        fetchNewToken(params);
        // console.log(token - stale or valid); 
    }

Hi @PlayStay,

I’d suggest you to just write the code logic to determine whether your token is expired and requires a refresh or not, independently of the VU state, as I’d expect the code to be executed only when the VU is active, and as part of the iteration.

Isn’t that your desired outcome?

Thanks!