Different request in each repeat

So, I’m new to Gatling. I noticed yesterday that when you write a chain and repeat it, it repeats exactly the same chain that was generated for the first run (with the exception of the repeat counter that I can add to string apparently). In my scenario I have raw json data and I want to add new id into the data with a function I made, it works for the first time the chain is executed but it doesn’t generate a new one each run. It’s not the only thing I would like to change between executions… so, is it possible to have different data between executions?

for insight: I want to have new Id where generateId is in the json data for every repeat

package computedatabase

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
import java.util.Calendar
import java.text.SimpleDateFormat
import scala.util.Random

class Class name removed extends Simulation {

  val httpConf = http
    .baseURL("https://host removed") 
    .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") 
    .doNotTrackHeader("1")
    .acceptLanguageHeader("en-US,en;q=0.5")
    .acceptEncodingHeader("gzip, deflate")
    .userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0")

  val useHeaders = Map("Content-Type" -> "application/json", """system-token""" -> "***removed***")

  val otherId = 123
  val blaId = """"bla""""

  object Buy {

    val buy = repeat(1000000, "i"){
        exec(http("Request ${i}")
        .post("/****removed****")
        .headers(useHeaders)
        .body(StringBody("""{
        "request":[{
          "blaId":""" + blaId + """,
          "otherId":""" + otherId + """,
          "someId":"""" + generateId() + "${i}" + """"
        }]
      }""")).asJSON)}
  }

  val scn = scenario("***removed***").exec(Buy.buy)

  setUp(scn.inject(atOnceUsers(1)).protocols(httpConf))

  def generateId() : String = {
    val correctFormat = new SimpleDateFormat("yyyyMMddhhmmss")
    val today = Calendar.getInstance().getTime()
    //correctFormat.format(today) + Random.nextPrintableChar + Random.nextPrintableChar + Random.nextInt(9999)
    correctFormat.format(today)
  }

}


Hi Tomas,

There is a crucial thing to understand about Gatling DSL : when you’re writing exec(http(…)), you’re not, at that point, executing the request, you’re only building the necessary blocks to run that request.

What happens here is that your request body is built only once, loading the Simulation.

Therefore, generateId is only called only once, leaving all your requests with the same id.

What you need to do is pass a session function to StringBody, which will get called every time the request is run, generating a new id in the process :

.body(StringBody(session => """{
        "request":[{
          "blaId":""" + blaId + """,
          "otherId":""" + otherId + """,
          "someId":"""" + generateId() + "${i}" + """"
        }]
      }""")).asJSON)

Also, I strongly advise you against using the SimpleDateFormat/Calendar APIs for generating your formatted date, as these APIs are not exactly known to be efficient…Joda Time or ThreeTenBP would be much better choices :wink:

Cheers,

Pierre

Hi Pierre,

Yes I realised that after a while, but I just wanted to check if there was any way to do it differently :slight_smile: thanks for the advice on the Time/date APIs!

Well, there is :slight_smile:

You can also generate and make them available through a feeder, which you use inside your loop to get a new ID :

object Buy {

    val idFeeder = Iterator.continually(Map("someId" -> generateId))

    val buy = repeat(1000000, "i"){

Thank you!

You have solved most of my problems :slight_smile: One more, as you see I’m doing 1M requests and when I run the script a log prints out constantly to cmd, it starts with x amount of requests and then when the x amount is finsihed, it starts printing again from the beginning. This results in a lot of lines, hundred of thousands lines printing out again and again… can I turn this off or maybe instead of starting with the whole log again just add requests? Is this maybe just a problem with Cmder?

Glad I could help :slight_smile:

If you do not have needs for the periodic summary printed out to the console, you can always uncomment in gatling.conf the line with writers = …. and remove “console”.
Of course, you’ll still get the HTML report.

Cheers,

Pierre