4

I have the following two methods:

def randomStartMethod() : Long = {
  var range = 1000L
  var r = ThreadLocalRandom.current().nextLong(10L*range)
  var randomStart = 1396024675000L + r
  return randomStart
}

def randomStopMethod() : Long = {
  var range = 1000L
  val r = ThreadLocalRandom.current().nextLong(10L*range)
  val randomStop =  1396024675000L + r*2L
  return randomStop
}

Then I add it to the request body like this:

val activity = repeat(10, "i") {
      exec(http("POST activity post")
        .post("/activity/")
        .header("Content-Type", "application/json; charset=ISO-8859-1")
        .header("accept", "*/*")
        .body(StringBody(
          s"""
             |{
             |    "id": "activityId",
             |    "type": "run",
             |    "start_epoch_ms": "${randomStartMethod()}",
             |    "end_epoch_ms": "${randomStop()}",
             |    "metrics": [
             |        {
             |            "type": "distance",
             |            "unit": "KM",
             |            "source": "nike.running.ios",
             |            "values": [
             |                {
             |                    "start_epoch_ms": "${randomStartMethod()}",
             |                    "end_epoch_ms": "${randomStopMethod()}",
             |                    "value": 2.0
             |                }
             |
            |            ]
             |        }
             |    ]
             |}
          """.stripMargin)).
        asJSON
        .check(status.is(202))
        .check(
          jsonPath(
            "$.activityId").saveAs("message")
        )
        .check(bodyString.
          transform(_.split("\""
          )(3)).saveAs(
          "changeToken"))

      ).exec(
        session => {
          val maybeId =
            session.get(
              "message").asOption[String]
          println(maybeId)
          session
        }
      )
    }
  }

But here the values are not generated dynamically when I use with feed. Can someone suggest how to generate the random numbers every time throughout the run?

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108

1 Answers1

7

Remember: if you want some code to be evaluated not only once on startup when Gatling builds the scenario, but every time a virtual user performs an action, you have to pass dynamic content: either Gatling EL based String, or a function.

Here, you have to do the latter, like:

.body(StringBody(session => //THIS IS A FUNCTION
          s"""
             |{
             |    "id": "activityId",
             |    "type": "run",
             |    "start_epoch_ms": "${randomStartMethod()}",
             |    "end_epoch_ms": "${randomStop()}",
             |    "metrics": [
             |        {
             |            "type": "distance",
             |            "unit": "KM",
             |            "source": "nike.running.ios",
             |            "values": [
             |                {
             |                    "start_epoch_ms": "${randomStartMethod()}",
             |                    "end_epoch_ms": "${randomStopMethod()}",
             |                    "value": 2.0
             |                }
             |
             |            ]
             |        }
             |    ]
             |}
          """.stripMargin))
Stephane Landelle
  • 6,990
  • 2
  • 23
  • 29
  • Thanks Steph. Here we meet again. So can you please give me an example? I could not figure this out. Do I need to use a feeder? Tried that but could not get it to work.I was trying to do something like this: http://stackoverflow.com/questions/35843371/how-to-add-random-value-in-json-body-in-gatling – Pritam Banerjee Aug 08 '16 at 20:30
  • If possible can you please join this room? http://chat.stackoverflow.com/rooms/120459/gatling – Pritam Banerjee Aug 08 '16 at 20:36