Search code examples
performance-testinggatling

In Gatling, how can I generate a random number each time a call is executed? (not using feeder)


I need to find a way to generate a random number each time the REST call is executed.

I have the following GET call:

    exec(http("Random execution")
      .get("/randomApi")
      .queryParam("id", getRandomId()))
  }

Obviously it doesn't work as the random number is only generated once and I end up with the same number whenever this call is executed. I cant use the feeder option as my feeder is already huge and is generated by a 3rd party for each test.


Solution

  • .queryParam takes Expressions as its arguments, and since Expression is an alias for a session function, you can just do...

    .queryParam("id", session => getRandomId())
    

    You could also define a second feeder that uses a function to generate the values - no need to update your existing feeder or add another csv file. This would be useful if you had more complicated logic for getting / generating an Id

    val idFeeder = Iterator.continually(Map("id" -> Random.nextInt(999999)))
    
    //in your scenario...
    .feed(idFeeder)
    .exec(http("Random execution")
      .get("/randomApi")
      .queryParam("id", "${id}")
    )