How to distribute VU's across different scenarios with k6


And is this possible to mention users percentiles?
For example, if I want 10% users to run one block of code, 30% - another block, and the rest of users - third scenario, all in one test.

How do I parameterize my k6 test

You can use modulo operation (%) to do something like that in your case

if (__VU % 10 ==1) {
// 10% of the VUs
} else if (__VU % 10 == 3) {
// 30% of the VUs
} else {
// the rest of the VUs (60%)

Obviously you can write the three different code blocks as different functions to make it more readable on your default function to be just the code above calling the different functions.

Also in order for the above code to work you should have 10+ VUs and they should be divisable by 10 without remainder. Or you can make it based on the iteration in which case just us e __ITER instead of __VU it will have the same problem but usually iterations are a lot more than VUs so the error should be marginal.


I want to propose another option that may be easier to work with by using a switch statement. The below is a simple case of each VU being sent to each individual request a third of the time, but it can obviously be adapted to fit your needs. To keep the script clean, I would recommend writing the different journeys as separate JavaScript files and importing them. My cursory testing in the Load Impact cloud resulted in the following distribution of 6322 requests:

2176 requests to case 1
2043 requests case 2
2103 requests to case 3

Not perfectly even, but should be good enough for most implementations!

export default function() {
  let userDistro = Math.floor(Math.random() * 100);

  switch (true) {
    case (userDistro <= 33):
    case (userDistro > 33 && userDistro <= 66):
    case (userDistro > 66 && userDistro < 100):





Related feature request