Search code examples
scalagatling

Dynamically create and setup scenarios


I'd like to dynamically create and setUp gatling scenarios in a for loop to load test a webservice.

Therefore I tried the following (shortened):

class RecordedSimulation extends Simulation {

  val httpProtocol = http
    .baseURL("http://127.0.0.1")

  val overallUsers = 1000

  val methods: Map[String, Double] = Map(
    "FindContact" -> 0.6,
    "FindAddress" -> 0.3,
    "FindNumber" -> 0.1
  )

  for ((methodname, probability) <- methods) {
    val scen = scenario(methodname)
      .exec(http(methodname)
        .get("/contactservice")
        .queryParam("method", methodname))

    setUp(scen.inject(constantUsersPerSec(overallUsers * probability) during (60 seconds))).protocols(httpProtocol)
  }
}

If I try running this simulation nothing happens: No simulation is started, also no error appears.

So my question is if it is even possible to dynamically create and setUp gatling scenarios. Am I missing something or doing something wrong?


Why do I want to do it dynamically anyway?

Well I have a lot of pretty similar methods to test and I'd like to avoid copy/pasting the same scenario over and over again.


Solution

  • I found the problem myself. This gist got me in the right direction.

    It seems you can call the setUp method only once. So I'm putting my scenarios in an ArraySeq and calling setUp with this ArraySeq as a parameter:

    import scala.collection.mutable.ArraySeq
    import io.gatling.core.structure.PopulationBuilder
    
    class RecordedSimulation extends Simulation {
    
      val httpProtocol = http
        .baseURL("http://127.0.0.1")
    
      val overallUsers = 1000
    
      val methods: Map[String, Double] = Map(
        "FindContact" -> 0.6,
        "FindAddress" -> 0.3,
        "FindNumber" -> 0.1
      )
    
      def scnList() = {
        var scnList = new ArraySeq[PopulationBuilder](methods.size)
        var i = 0
        for ((methodname, probability) <- methods) {
          var scen = scenario(methodname)
            .exec(http(methodname)
              .get("/contactservice")
              .queryParam("method", methodname))
            .inject(constantUsersPerSec(overallUsers * probability) during (60 seconds) randomized)
    
          scnList(i) = scen
          i = i + 1
        }
        scnList
      }
    
      setUp(scnList: _*).protocols(httpProtocol)
    }
    

    Note: As discussed in the comments, ArraySeq is abstract in recent versions of Scala. As keylogger suggests, use an Array instead.