scala - 如何在 Gatling 中动态生成 JSon?

标签 scala gatling

我有以下两种方法:

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
}

然后我将它添加到请求正文中,如下所示:
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
        }
      )
    }
  }

但是在这里,当我与提要一起使用时,这些值不是动态生成的。有人可以建议如何在整个运行过程中每次生成随机数吗?

最佳答案

请记住:如果您希望某些代码不仅在 Gatling 构建场景时在启动时评估一次,而且每次虚拟用户执行操作时都被评估,则您必须传递动态内容:基于 Gatling EL 的字符串或函数。

在这里,您必须执行后者,例如:

.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))

关于scala - 如何在 Gatling 中动态生成 JSon?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38834933/

相关文章:

java - 添加计数器,该计数器在每次 http 请求时都会增加

scala - 当加特林负载测试表现不佳时如何使 Jenkins 工作失败

forms - 玩转2.3 scala forms——如何自定义约束消息

amazon-web-services - 使用 Gatling 进行负载测试时 TLS 握手超时

json - GATLING JSON 保存和重用

scala - 在 Scala 中处理无效参数的最佳方法是什么

scala - 从 session 中读取时如何发出 Gatling 捕获请求?

scala - 添加 Lift-json 作为 Play 2.0 项目的构建依赖项

java - Scala 相当于 Java 的 static block 吗? (没有伴生对象)

Scala 二叉搜索树的并集运算