How to use 'counterName' of repeat() at variable?

Hi,

I tried to create an bunch of objects via service using the ‘counterName’ variable of a repeat function.
But neither the “id” is calculated correctly nor the “name” is resolved (e.g. “Test_0”).

reasons are:

  • my ID has to start at 1
  • the name should be printed in the logs (e.g. “create object ‘Test_0’”)
  1. I tried to access the session object but it seems not to be existing in repeat() but exec()
  2. But inside exec() I can’t define variables

And because I’m a very beginner to Scala I got no clue how to handle it.

.repeat(5, “object_index”) {
val id = Integer.getInteger("${object_index}") + 1;
val name = “Test_${object_index}”

exec(
http(“create object '” + name + “’”)
.post("/objects")
.basicAuth(userName, userPassword)
.body("{" +
““id”: + id +,” +
““name”: “” + name + “”,” +
“}”)
.check(status.is(200)))
}

Any help is welcome.

Regards Danny

Hi Danny,

You’re actually missing the point that all the DSL elements are builders, meaning that they are only evaluated once when the Simulation is built.

In your case, the "val id = … " block is only evaluated at build time.

You have multiple solutions:

  • Do everything inside the Simulation class just like you did

You have to add an extra step in order to compute your (loop index + 1)

repeat(5, “object_index”) {
exec(session => {
val id = session.getTypedAttributeInt + 1
session.setAttribute(“id”, id)
})
.exec(
http(“create object Test_${object_index}”)
.post("/objects")
.basicAuth(userName, userPassword)
.body("""{“id”: ${id}, “name:” Test_${object_index}}""")
.check(status.is(200)))
}

  • Use an external template for your JSON request body, as explained here:

https://github.com/excilys/gatling/wiki/HTTP#wiki-template-body

I personally would go with the second solution.

Hope that helps,

Stéphane

So the description of the http() can’t be changed during runtime because it is build once - understood.
I will try the SSP-Templates because my request bodies are getting bigger in near future.

Thanks a lot, Danny

It can be changed at runtime, but you have to understand what is evaluated at build time and what is evaluated at runtime.
Things evaluated at runtime are functions (usually Session => something) and EL (that are actually converted into functions).

In my above example, the request name “create object Test_${object_index}” is an EL that will be resolved every time a user sends this request.

Cheers,

Stéphane