我有以下两种方法:
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
}
Run Code Online (Sandbox Code Playgroud)
然后将其添加到请求主体中,如下所示:
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
}
)
}
}
Run Code Online (Sandbox Code Playgroud)
但是在这里,当我与feed一起使用时,这些值不是动态生成的。有人可以建议如何在整个运行过程中每次生成随机数吗?
请记住:如果您希望不仅在Gatling构建场景时在启动时对某些代码进行评估,而且每次虚拟用户执行操作时都必须传递动态内容:基于Gatling EL的String或函数。
在这里,您必须执行后者,例如:
.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))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1811 次 |
| 最近记录: |