11

I would like to get a Random URL on http request for Gatling

My scenario is defined like this:

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
import scala.util.Random

class testSimulation extends Simulation {

  val httpConf = http.baseURL("OURURL")


  val scn = scenario("View HomePages")
                .exec(
                        http("Home page")
                                .get("/" + new Random().nextInt())
                              .resources(
                                      http("genericons.css").get("/wp-content/themes/twentyfifteen/genericons/generi$
                                      http("style.css").get("/wp-content/themes/twentyfifteen/style.css?ver=4.2.3"),
                                      http("jquery.js").get("/wp-includes/js/jquery/jquery.js?ver=1.11.2"),
                                      http("jquery-migrate.min.js").get("/wp-includes/js/jquery/jquery-migrate.min.j$
                                      http("skip-link-focus-fix.js").get("/wp-content/themes/twentyfifteen/js/skip-l$
                                      http("functions.js").get("/wp-content/themes/twentyfifteen/js/functions.js?ver$
                                      http("wp-emoji-release.min.js").get("/wp-includes/js/wp-emoji-release.min.js?v$
                                      http("wp-emoji-release.min.js").get("/wp-includes/js/wp-emoji-release.min.js?v$
                                      http("skip-link-focus-fix.js").get("/wp-content/themes/twentyfifteen/js/skip-l$
                                      http("functions.js").get("/wp-content/themes/twentyfifteen/js/functions.js?ver$
                              )
                )

  setUp(
      scn.inject
      (
      rampUsersPerSec(1) to(300) during(60 seconds),
      constantUsersPerSec(300) during(600 seconds)
      )
      .protocols(httpConf)
      )
}

I have only one random number generated instead of one per request. Do you know how to solve it ? Thanks !

Paul
  • 26,170
  • 12
  • 85
  • 119
  • I'm not familiar with gatling. Looking only at the scala, it appears the parameter, including the Random.nextInt call, to `scenario.exec` is executed immediately on the val line. The usual way to get the randomizer to be called more than once would be to put it in a function that gets executed more than once, which in turn might depend on whether `scenario.exec()` can take a function as a parameter or whether you can extend a class it does take as a parameter in a proper manner for your use case. – Paul Aug 28 '15 at 00:40

1 Answers1

11

You're passing a value, so of course new Random().nextInt gets only called once, when the Simulation is built.

You have to pass an Expression, ie a function. Only then will it be evaluated every time.

.get(session => "/" + new Random().nextInt())
Stephane Landelle
  • 6,990
  • 2
  • 23
  • 29