How to send http requests dynamically in Gatling?

34 Views Asked by At

I'm trying to save some seq in static variable and then make http get for each value of the seq:

.exec { session =>
    residents = session("residents").as[String]
    .split(",").toSeq
    .map(f => f.replaceAll("\"", ""))
    session
}
.foreach(residents, "resident") {
    exec(http("resident").get("#{resident}"))
}

Each element of seq is http url.

Is it possible to send http requests dynamically in Gatling depends on count of seq elements? How can I do this beacuse this code doesn't work as I expect.

1

There are 1 best solutions below

0
Stéphane LANDELLE On BEST ANSWER

That's not how Gatling works. Your residents mutable reference is:

  • global to your test, hence you'll face race conditions when running with multiple users
  • not populated at the time you're building your foreach action

You've pulled residents from the current user's Session, you must stay in this scope:

.exec { session =>
    val residentsSeq = session("residents").as[String]
    .split(",").toSeq
    .map(f => f.replaceAll("\"", ""))
    // replace the original "residents" with a Seq
    session.set("residents", residentsSeq)
}
// basically equivalent to
// foreach(session => session("residents").as[Seq[String]], "resident")
.foreach("#{residents}", "resident") {
    exec(http("resident").get("#{resident}"))
}