How to use executor: 'shared-iterations' and stages together o simulate ramp-up scenario?

My understanding of what you’re trying to achieve, from your description, is: to simulate a ramping number of users interacting with your target, while using a predefined set of users to do that. Your initial approach was to try to achieve that by mixing ramping (modifying linearly the number of VUs at play), and shared-iterations (to make sure to execute exactly N iterations for a data set with N entries) executors. Is that correct?

Based on the assumption this is what you’re trying to achieve, I came up with the following script demonstrating how to use the Ramping VU executor hand in hand with the execution module’s ability to give you the current iteration number, to both having VUs ramping, and being able to have them pick a user from the data set based on the iteration number (to be sure that we go through all of them, sequentially):

// http://grafana.staged-by-discourse.com/t/how-to-use-executor-shared-iterations-and-stages-together-o-simulate-ramp-up-scenario/99315/
import { sleep } from "k6";
import { SharedArray } from "k6/data";
import { scenario } from "k6/execution";

const usersJSON = new SharedArray("users", function () {
  return JSON.parse(
    open("./userdata.json", "r")
  ).users;
});

export const options = {
  discardResponseBodies: true,
  scenarios: {
    users: {
      executor: "ramping-vus",
      startVUs: 0,
      stages: [
        { duration: "20s", target: 10 },
        { duration: "10s", target: 0 },
      ],
      gracefulRampDown: "0s",
    },
  },
};

export default function () {
  // As demonstrated in https://k6.io/docs/examples/data-parameterization#retrieving-unique-data
  // Plus, applying a modulo to the index of the user in the array to stay
  // in bounds of the users array.
  const idx = scenario.iterationInTest % usersJSON.length;
  const user = usersJSON[idx];
  console.log(`${idx}:${user}`);
  sleep(1);
}

From your explanation, I had a hunch you might want to interrupt the execution once all the users from the data set have been used? If that’s the case, you should be able to check if scenario.iterationInTest == dataset.length and stop the execution there.

Let me know if that’s helpful :bowing_man:

1 Like