How to distribute VU's across different scenarios with k6

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):
      http.get("http://test.loadimpact.com?switch=caseone");
      break;
    case (userDistro > 33 && userDistro <= 66):
      http.get("http://test.loadimpact.com?switch=casetwo");
      break;
    case (userDistro > 66 && userDistro < 100):
      http.get("http://test.loadimpact.com?switch=casethree");
      break;
    default:
      http.get("http://test.loadimpact.com?switch=default");
      break;

  sleep(1);


  }

};
3 Likes