scala - How to dynamically generate JSon in Gatling? -


i have following 2 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 add request body 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 values not generated dynamically when use feed. can suggest how generate random numbers every time throughout run?

remember: if want code evaluated not once on startup when gatling builds scenario, every time virtual user performs action, have pass dynamic content: either gatling el based string, or function.

here, have latter, like:

.body(stringbody(session => //this 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)) 

Comments