Search code examples
scalaload-testinggatlingscala-gatling

Gatling - randomize scenario's request order?


GOAL

I'm trying to randomize the order of 5 steps taken by each user/scenario.

That is, these are my users, and the order of steps to take (among steps A, B, C, D, and E)

user 1: A, E, B, D, C
user 2: D, E, C, B, A 

etc

ie, each user takes each step exactly once, but in a random order.

FAILED ATTEMPT

I have my RecordSeqFeederBuilder feeder:

val multiFeeder = Array(
  Map("stepName" -> "A", "stepPath" -> "pathA"),
  Map("stepName" -> "B", "stepPath" -> "pathB"),
  Map("stepName" -> "C", "stepPath" -> "pathC"),
  Map("stepName" -> "D", "stepPath" -> "pathD"),
  Map("stepName" -> "E", "stepPath" -> "pathE"),
).random

but this will lead to user steps like

user 1: A, E, E, E, C
user 2: B, C, B, B, A 

tldr - how can I reorder the steps per user/scenario?

It would be nice if instead of ".random" I could call something like ".shufflePerScenario" -- so that each scenario accesses the elements in a unique order.

Is there some way I can do this with Gatling feeders? Is there a better way I should be approaching this?

Looks like randomSwitch could also lead to repeated steps. I think I could use dynamic data for this, but it would be nice to know a more direct solution if it exists.


Solution

  • For list of steps and their names

      val a = http("a").get("/1")
      val b = http("b").get("/2")
      val c = http("c").get("/3")
      private val steps = List("a" -> a, "b" -> b, "c" -> c)
    

    You can create a function from steps to scenario, which shuffles steps

      def randomOrder(steps: Seq[(String, HttpRequestBuilder)], prefix: String): ScenarioBuilder = {
        val shuffled = Random.shuffle(steps)
        val name = shuffled.map(_._1).mkString(prefix, ", ", "")
        scenario(name).exec(shuffled.map(_._2).map(exec(_)))
      }
    

    Make sure to add unique prefix - gatling does not allow scenarios with the same name and shuffle sometimes can produce the same combination of steps.

    example to run 3 scenarios with 2 users each

      setUp(
        randomOrder(steps, "first ").inject(atOnceUsers(2)),
        randomOrder(steps, "second ").inject(atOnceUsers(2)),
        randomOrder(steps, "third ").inject(atOnceUsers(2)),
      )
    

    enter image description here