gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 00:38:01 25 4
gpt4 key购买 nike

我有以下两种方法:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com