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?

1

This is how I did it, by adding methods in gatling:

package simulations.syncservice

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
import scala.concurrent.forkjoin.ThreadLocalRandom
import scala.language.postfixOps
import scala.math._

class PostActivitySimulation extends Simulation {

  var startEpoch = 0L;
  var stopEpoch = 0L;

  val appId = Map("appId" -> "NIKEREEBOK")

  //Sync service parameters
  val syncUrl = "https://YOURURL.COM"

  var httpConf = http.baseURL(syncUrl)

  def startGenerator() :Long = {
    var startNum = 1396024675000L + ThreadLocalRandom.current().nextLong(90000L)
    startEpoch = startNum;
    return startNum
  }
  def stopGenerator() :Long = {
    var stopNum = 1396335695000L + ThreadLocalRandom.current().nextLong(90000L)
    stopEpoch = stopNum;
    return stopNum
  }


  val postActivity = scenario("POST Activity")
    .exec(http("POST activity post")
      .post("/activity/")
      .header("Content-Type", "application/json; charset=ISO-8859-1")
      .header("accept" , "*/*")
      .body(StringBody(session =>
        s"""
           |{
           |    "id": "activityId",
           |    "type": "run",
           |    "start_epoch_ms": "${startGenerator()}",
           |    "end_epoch_ms": "${stopGenerator()}",
           |    "metrics": [
           |        {
           |            "type": "distance",
           |            "unit": "KM",
           |            "source": "nike.running.ios",
           |            "values": [
           |                {
           |                    "start_epoch_ms": "${startEpoch}",
           |                    "end_epoch_ms": "${stopEpoch}",
           |                    "value": 2.0
           |                }
           |
            |            ]
           |        }
           |    ]
           |}
          """.stripMargin)).asJSON
      .check(status.is(202))
      .check(
        jsonPath("$.activityId").saveAs("activityId")
      )
      .check(bodyString.transform(_.split("\"")(3)).saveAs("changeToken"))
    )
    .exec(
      session => {
        val activityId = session.get("activityId").asOption[String]
        println(activityId)
        session
      }
    )

  setUp(
    postActivity.inject
    (nothingFor(2 seconds),
      atOnceUsers(30),
      rampUsers(200) over(1000 seconds))
      .protocols(httpConf))
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.