Foreach issues ...

Hi all,
I’m trying to use foreach to build up a scenario where I POST each item in a sequence but running the following error:
[error] found : io.gatling.http.request.builder.HttpRequestBuilder
[error] required: io.gatling.commons.validation.Validation[io.gatling.core.session.Session]

Here’s the scenario:
val xs = 1 to 10
val scn = scenario(“Test”)
.foreach(xs, “x”) {
exec(session => {
val x = session(“x”).as[Integer]

I feel like I’m doing something obviously wrong but I can’t seem to understand how to fix this from the documentation.


I got past that error by doing:
val xs = (1 to 10)
val scn = scenario(“Test”)
.exec(session => {
foreach(xs, “x”) {
val x = session(“x”).as[Integer]

but now I get:
Simulation Test started…

From doc:

Gatling DSL components are immutable ActionBuilder(s) that have to be chained altogether and are only built once on startup. The results is a workflow chain of Action(s). These builders don’t do anything by themselves, they don’t trigger any side effect, they are just definitions. As a result, creating such DSL components at runtime in functions is completely meaningless. If you want conditional paths in your execution flow, use the proper DSL components (doIf, randomSwitch, etc)

exec { session =>

  if (someSessionBasedCondition(session)) {
    // just create a builder that is immediately discarded, hence doesn't do anything
    // you should be using a doIf here
    http("Get Homepage").get("[](")

So, Stephane. Is there really no way to create actions in an ActionBuilder at runtime? How does the recorder do it? This would dash my hopes of creating a scenario from an textfile. C’est rellement pas possible?

I suspect you’re confusing load time, when the Simulation instance is loaded and the scenario workflows are created, and runtime, when the virtual users are propelled along those workflows and for example execute functions.
You can perfectly create scenarios based on some file content at load time.

Stephane, vous avez raison. That was my confusion. Can you point me to some code that builds up scenarios dynamically at load time? I still have one day to put this together before a demo (am currently creating source code with shell scripts – quelle horreur)

If i understood it right, you want to have 10 requests with appropriate body from 1 to 10?
It yes, you can try smth like below. I use Get just to see my requests

val endNum = 5
val startNum = 1
var counter = new java.util.concurrent.atomic.AtomicInteger(startNum-1)

val httpProtocol = http

val scn = scenario("+1")
.asLongAs(session => counter.getAndIncrement() < endNum)
.get(session =>“” + s"""${counter}""")