Issue with updating string in the exec block

Hello All,

I have just started working on my first performance test in Gatling and running into below issue:

In my test, I have a JSON template file with CONSTANTS which I’m replacing during runtime with the values from the CSV feeder and few values from the response of the previous request.
Also, I need to perform few calculations on the data coming from the CSV feeder.

I’m using session block to read the CSV feeder values, perform calculations and then replacing the constants in the string which holds the JSON request.

Below is the simplified code:

//REQUEST_STRING contains JSON template file with constants.
var REQUEST_STRING: String = Source.fromFile(requestFileWithConstants).getLines.mkString

`
val saveValues: ChainBuilder = {
exec(session => {

val START_DATE_LONG: Long = session(“startdate”).as[String].toLong //startdate is the date in miliseconds

//I have a for loop here to do calculations but to simplify, just adding one variable.
val day1_hour1 = START_DATE_LONG + 86400000 // just a simplified example

//Replacing the CONSTANT with the calculated value.
REQUEST_STRING = REQUEST_STRING.replaceAll(“DAY1_HOUR1”, day1_hour1.toString())

println(REQUEST_STRING) //This prints the JSON with the CONSTANT “DAY1_HOUR1” replaced with the value day1_hour1

session
})

.exec(
http(“Save Values”)
.post("/mylink")
.headers(My_HEADERS)
.body(
StringBody(REQUEST_STRING
.replaceAll(“VAL1”, “\${val1}” ) // val1 and val2 is from the response of the previous request.
.replaceAll(“VAL2”, “\${val2}” )
)
}

`

Issue is the 2nd exec doesn’t see the modified “REQUEST_STRING” from the 1st exec. When I println(REQUEST_STRING), I see the constants correctly replaced but the actual request happens in the 2nd exec, it doesn’t seem to see the changes happed in the first exec, it only replaces the constants “VAL1” and “VAL2”. Update to constant “DAY1_HOUR1” is ignored in the actual request.

Any help is appreciated, Thanks!

As explained here, your problem is that you’re passing a value once when the scenario is built, not a reference that’s evaluated every time.
You have to pass a function.