如何在Gatling中为相同的变量名提供随机数



如果你有一个JSON,它有一个客户阵列,每个客户都必须有一个唯一的客户编号,我如何用随机数字来输入:

{
"customers": [
{
"customerNo": "123",
"Name": "Joe"
},
{
"customerNo": "456"
"Name": "Jane"
},
]
}

我认为这可能奏效:

{
"customers": [
{
"customerNo": "${customerNo}",
"Name": "Joe"
},
{
"customerNo": "${customerNo}"
"Name": "Jane"
},
]
}
val customerNumber = Iterator.continually(
Map("customerNumber" -> Random.nextInt(Integer.MAX_VALUE))
)

然后添加:

feed(customerNumber)

但这在两种情况下都使用相同的生成数。

最干净的方法是传递函数,例如在Java:中

StringBody(session ->
"""
{
"customers": [
{
"customerNo": "%s",
"Name": "Joe"
},
{
"customerNo": "%s"
"Name": "Jane"
},
]
}""".formatted(Random.nextInt(Integer.MAX_VALUE), Random.nextInt(Integer.MAX_VALUE))
)

最新更新