如何在加特林动态生成JSon? [英] How to dynamically generate JSon in Gatling?

查看:55
本文介绍了如何在加特林动态生成JSon?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两种方法:

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

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

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?

推荐答案

记住:如果您不仅希望在加特林构建场景时在启动时对某些代码进行评估,而且还希望每次虚拟用户执行操作时都对代码进行评估传递动态内容:基于加特林EL的String或函数.

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

这篇关于如何在加特林动态生成JSon?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆