naike
naike

Reputation: 33

Gatling: dynamically setup one scenario multiple times

I have one scenario with 4 different API calls, which always stay the same. I'm trying to create a parallel stress test, where I want the same scenario to run first with say 10%, and then 20%, 30%... and so on of max users.

I'm facing a problem where I'm not able to set up the same scenario more than once.

But I also need the setup to be dynamic, depending on configurable variables in the script This is what's causing the problem, I want to be able to easily configure it to run e.g. 10%, 50%, and 100%.

This is what I have so far (shortened, and atOnceUser and nothingFor take inputs dynamically from the config iterating over bla(i))

def scnList() = {
    var scnList = new ArraySeq[PopulationBuilder](someArray.length)
    var i = 0
    for (i <- 0 until someArray.length) {
      var scen = myscenario
        .inject(atOnceUsers((bla(i)), nothingFor(blabla(i) minutes))
      scnList(i) = scen
    }
    scnList
}

setUp(scnList: _*).protocols(httpConf)
    .assertions(global.successfulRequests.percent.gt(99),
    forAll.responseTime.max.lt(5000)
)

Upvotes: 1

Views: 5635

Answers (2)

retrospectacus
retrospectacus

Reputation: 2586

Example code to run the same scenario N times in sequence. Hope it helps someone.

  private def createUser(n: Int): ScenarioBuilder =
    scenario(s"Create User $n").exec(
      baseScenario,
      feed(newGatlingUserFeeder),
      exec(...),
      ...
    )

  private def sequentialInjection(times: Int, scenario: Int => ScenarioBuilder): PopulationBuilder =
    (1 until times).foldLeft(scenario(0).inject(atOnceUsers(1))) { case (previousScenario, i) =>
      previousScenario.andThen(scenario(i).inject(atOnceUsers(1)))
    }


  setUp(
    // Create 1000 users.
    sequentialInjection(1000, createUsers)
  )
    .protocols(httpProtocol)

Used n here to give each step a new name, otherwise you get the error java.lang.IllegalArgumentException: requirement failed: Scenario names must be unique but found duplicates. The OP could adapt this to use loading percentages.

Mateusz' solution does not work; the scenarios will all run in parallel.

Upvotes: 0

Mateusz Gruszczynski
Mateusz Gruszczynski

Reputation: 1500

Well setUp can be called only once per simulation, also if you will pass it a list of scenarios as parameter than it will run them in parallel what makes no sense in your case. What should work is to run single scenario with injection profile that will run your scenario with few different levels of load one after another. Number of users per each iteration can be loaded from some config fe.

val steps = Seq(5, 10, 15, 25, 40) // Contains users/s in each step, can be read from config

//Build list of injections based on sequence above
val injectionProfile = steps.flatMap(
  load => Seq(
    constantUsersPerSec(load).during(30 seconds),
    nothingFor(10 seconds)
  )
)

val httpConf = http.baseURL("http://google.com")

val myscenario = scenario("My scenario")
  .exec(
    http("Get example")
      .get("/")
      .check(status.is(200))
  )

setUp(
  myscenario.inject(injectionProfile).protocols(httpConf)
)

If you would like to have every iteration in separate report than best way is to create abstract simulation class and few more classes that extends it with different constructor parameters, but then it would be hard to get variable number of steps, so if you are ok with having constant number of variants than it may be other way.

Upvotes: 2

Related Questions