Unique login for each virtual User

Hi @Harshsahay

Welcome to the community forum :wave:

For your case, I would recommend taking advantage of the k6 test lifecycle. You’ll also find some examples in k6-learn/Setup-and-Teardown-functions.md.

You can have each VU do the login and retrieve its token during the setup() phase, which is execute once per VU. There is an example in the blog How to Load Test OAuth secured APIs with k6? that can serve as starting point. Under the last section of the blog “Test Script Template with OAuth Authentication” you’ll see that we can authenticate the user in the setup() and use the token from there on in the VU code.

If you already have a list of users and passwords in a JSON file, you can indeed use the SharedArray to load those, and have each VU can retrieve its unique data. Then during the setup use the user data to retrieve the token.

We have a similar example for that part in k6-learn/Using-execution-context-variables. Mostly you’ll need to access each array position using VU id.

import { vu } from 'k6/execution';
...
  const username = data[vu.idInTest - 1];

I hope this helps.

Cheers!