繁体   English   中英

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

[英]How to feed random numbers for the same variable names in 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)

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

最干净的方法是传递 function,例如 Java:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM