How to set required resources in k6 tests

Hi Everyone,
recently I start to do performance testing with k6.

I want to test my resource( in this case, apps, dashboards, and ect : are my resource) which will change depending on the number of users, for example, for each user I will have 5 apps and 6 dashboards, which means if I have 5 users the number of App will be 25, and the number of dashboards will be 30.

Is there any way in k6 that I can define this calculation before test runs and depend on the number of users my test simulates the different amount of resources?

Hi, welcome to the forum :slight_smile:

There’s a few approaches you could use for this, depending on how your script is structured.

One way is to use scenarios and the per-vu-iterations executor. If you set the number of VUs and iterations using an environment variable, you can adjust it when invoking the script.

So if your script has:

export let options = {
  scenarios: {
    appTests: {
      executor: 'per-vu-iterations',
      vus: __ENV.K6_VUS,
      iterations: __ENV.K6_VUS*5,
      exec: 'runAppTest',
    },
    dashboardTests: {
      executor: 'per-vu-iterations',
      vus: __ENV.K6_VUS,
      iterations: __ENV.K6_VUS*6,
      exec: 'runDashboardTest',
    },
  },
};

export function runAppTest() {
  console.log('running app test');
}

export function runDashboardTest() {
  console.log('running dashboard test');
}

You can run it with k6 run -e K6_VUS=1 script.js to run 5 iterations of the runAppTest() function and 6 iterations of the runDashboardTest() function:

running (00m00.0s), 0/2 VUs, 11 complete and 0 interrupted iterations
appTests       ✓ [======================================] 1 VUs  00m00.0s/10m0s  5/5 iters, 5 per VU
dashboardTests ✓ [======================================] 1 VUs  00m00.0s/10m0s  6/6 iters, 6 per VU

And if you run it with k6 run -e K6_VUS=5 script.js it will run 25 and 30 iterations respectively:

running (00m00.0s), 00/10 VUs, 275 complete and 0 interrupted iterations
appTests       ✓ [======================================] 5 VUs  00m00.0s/10m0s  125/125 iters, 25 per VU
dashboardTests ✓ [======================================] 5 VUs  00m00.0s/10m0s  150/150 iters, 30 per VU

Hope this helps :slight_smile:

Thanks for your fast reply :slight_smile:
Can you please confirm that when I define the resources in this way and then run my test ( which can be for example; log in to a system) actually it will run my test in a system that has already 30 dashboards and 20 spaces).
and I can say I measure how fast the system can react with those number of resources.

Sorry, I’m not sure what you mean by “dashboards” and “spaces”. Can you provide a working example of your script?

The example I gave you would execute those functions a varying amount of times depending on the K6_VUS environment variable you set when running the script. It’s up to you to code whatever logic you need, including checking if the resource already exists or not.

unfortunately, I have no script for it, I just got the scenarios and I need to find out how to estimate the resource requirements based on the growing customer.
I know it is not an easy topic, I can give a real-world example :
imagine we want to test a bus and tell the company how many passengers can go with this bus and without having an effect on the speed of the bus.
what we do not do is, for this test we don’t ask real people to come and seat on the bus we might use some mannequins with different weight. and then start to test the bus with a different number of resources (which here are mannequins) to say the limit of this bus is how many passengers.
and in a scenario that I have, I need to define my own resources to have the amount of traffic that a certain number of users can bring to the portal. and since I don’t want to add them manually one by one in a portal I should find a way to simulate or inject the JSON file or etc… to first have a ready environment and then run my test ( can be anything like login process for a system with 200 apps and so on … )

I hope I was able to explain what I am looking for
However, thanks for your help and, as soon as I found a way I will add the solution here, to share with others.

p.s. : it might be that I need to provide the environment setup in other tools and somehow rum my normal k6 test just against the defined environment. (I’m testing a cloud-based application therefore i don’t want to deploy a lot of resource for the test directly in our cloud i try to add the resource on my machine only )

If I’m understanding your analogy well, you want to test your service with a variable payload size, i.e. making POST requests with bodies of different sizes.

You can easily do this with k6 by loading a JSON file and selecting the payloads depending on which scenario you’re running. So for example, you could have a JSON file like this:

{
  "apps": [
    {...},
  ],
  "dashboards": [
    {...},
  ]
}

And then load it, extract the number of resources you need for that particular scenario and submit a POST request.

See the documentation on open(), http.post(), and Data Parameterization for details. You can also try searching this forum as there’s plenty of examples of scripts doing something similar.

Good luck!