How do I parameterize my k6 test

Hi, I’ve got a new problem to solve.

I want every VU to represent a specific person, with different login credentials.
In other words, I want my 5 VUs to authenticate as 5 different people, and run separate scenarios.
Is it possible to perform such task with k6?

Thanks in advance !

1 Like

Hi,

yes, each VU can run completely separate js code. take a look at this example:

import http from "k6/http"

export function setup() {
    return {
        1: {
            token: 'value of the token for vu1',
            endpoint : '/endpoint/for/vu1'
        },
        2: {
            token: 'value of the token for vu2',
            endpoint : '/endpoint/for/vu2'
        },
        // ...
    }
}

const baseURL = "http://google.com"

export default function(data) {
  const url = baseURL + data[__VU]["endpoint"];
  const options = {
    headers: {
      "X-Token-header-name": data[__VU] ["token"]
    }
  };

  const response = http.get(url, options);
}
1 Like

2 posts were split to a new topic: How to distribute VU’s across different scenarios with k6

How would I create a variable to replace the authentication token cookie so that all the calls don’t use the same token from the browser har test, this is what the code looks like(with some confidential stuff taken out)

response = http.get(

        "https://bi.dev..........

        {

          headers: {

            "user-agent":

              "Mozilla/5.0 (Windows NT........)

            accept: "*/*",

            "sec-fetch-site": "same-site",

            "sec-fetch-mode": "no-cors",

            "sec-fetch-dest": "script",

            referer:

              "https://...............",

            "accept-encoding": "gzip, deflate, br",

            "accept-language": "en-US,en;q=0.9",

            cookie:

              "access_token_dev=eyJraWQiO..................
}.
}
);

Cookies should be mostly automatically managed by k6, though you can also manage t hem manually, if you want to: Cookies
And if you need to pass things like Authorization headers between requests, you can extract data from one HTTP response, save it in a variable, and use it in the next requests. See Response and Intro to API Load Testing: The k6 Guide

Hey @pawel - will this get distributed across k6 cloud? That is, does the __VU id remain unique when distributed or does the full set of users (in this case 1,2) get run on each node?

Hi @Ian! Unfortunately, __VU does not remain unique across all nodes, it will start from 1 in each instance your test is split into. We originally planned to segment it and have it be globally unique (Segment --rps option between instances · Issue #1341 · grafana/k6 · GitHub), but a lot of existing cloud scripts rely on the current __VU behavior and just combine it with the LI_INSTANCE_ID environment variable to generate a globally unique VU ID.

Now the plan is to offer a bunch of helper functions to get such execution information, instead of messing with the existing or new global constants. For example, have a getLocalVUNumber() function that returns the same value as __VU, but also have getGlobalVUNumber() that returns a globally unique sequential VU ID. This will not be ready in time for k6 v0.30.0, but I’m hopeful we’ll implement it in v0.31.0 or the next few k6 versions. You can follow Improve execution information in scripts · Issue #1320 · grafana/k6 · GitHub for updates.

Hey @ned - thanks for such a quick response!

Hey all,

Some questions:

  1. The example shown pre-supposes that we’ve already fetched bearer tokens and hard coded them into the script, yes?
  2. If my script is set up to start with 125 vus, but be able to scale up to 250 vus, would I need an entry in the setup for all 250? Or is the harness smart enough to say "I see six defined, iterate through the defined six, and then cycle back to one interpreting it as seven through twelve? Wash, rinse repeat until it gets to VU 250…

I may have more but that’s the starting point on need to know.