Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically generate JSon in Gatling?

Tags:

scala

gatling

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?

like image 569
Pritam Banerjee Avatar asked Mar 11 '23 11:03

Pritam Banerjee


1 Answers

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))
like image 86
Stephane Landelle Avatar answered Mar 21 '23 03:03

Stephane Landelle