Search code examples
scalagatlingscala-gatling

Loop statements in Gatling


I'm new to Gatling. I'm trying to understand how to work with the Loop statements. I have a code example:

  private def createGame: ChainBuilder = exec(
    exec(session => session.set("number", "1"))
      .asLongAsDuring(true, 15.seconds) {
        exec(session => {                                            //1
          val k1 = session("number")                                 //6
          val ORDER_ID = session("ORDER_ID")                         //7
          println(s"I am verifying something ${k1}, ${ORDER_ID}")    //8
          session                                                    //9
        })
          .exec(buildSessions)                                       //2
          .exec(http("Get specific game")                            //4
          .get("/videogames/${number}")                              //5
          .check(status.in(200 to 210)))                             //3
      }
  )
I'm having hard time to understand the order of calls inside the asLongAsDuring loop. I marked the lines with comments, this is how the test runs when I am debuging it (starting with //1). Could some explain why the code inside the loop doesn't work line by line? Thanks.

p.s. Seems like it's the same for all loop statements.


Solution

  • You're confusing build time and runtime.

    First, Gatling loads the Simulation and builds the builders (such as your ChainBuilder) and generate the actual Scenario, then the different virtual users execute it and in particular execute your functions.

    Note: your Session API is wrong, please check the documentation.