简体   繁体   English

如何在 Gatling 的 Json Body 中添加随机值?

[英]How to add random value in Json Body in Gatling?

I need to create a random positive integer each time and send it to Json body in Gatling.我每次都需要创建一个随机正整数并将其发送到 Gatling 中的 Json body。

I used the below code to create a random positive ineger:我使用下面的代码来创建一个随机的正数:

val  r = new scala.util.Random;
val OrderRef = r.nextInt(Integer.MAX_VALUE);

but, How can I feed the randomly generated value into the json body?但是,如何将随机生成的值输入到 json 正文中?

I tried:我试过:

.exec(http("OrderCreation")
.post("/abc/orders")
.body(StringBody("""{    "orderReference": "${OrderRef}"}""").asJson)  

But, this doesn't seem to work.但是,这似乎行不通。 Any clues please.请提供任何线索。

Thanks!谢谢!

First of all you want to generate random number each time, thus OrderRef has to be a method, like:首先要每次生成随机数,因此OrderRef必须是一个方法,例如:

def orderRef() = Random.nextInt(Integer.MAX_VALUE)

Side comment: by Scala convention: name camelCase, () while it generates new values, without ;旁注:根据 Scala 约定:命名camelCase, () 而它生成新值,没有; in the end.到底。

To use the prepared method you cannot use the Gatling EL string .要使用准备好的方法,您不能使用Gatling EL string Syntax is very limited and basically "${OrderRef}" searches for variable with name OrderRef in Gatling Session.语法非常有限,基本上"${OrderRef}"在 Gatling Session 中搜索名称为OrderRef变量。

Correct way is to use Expression function as:正确的方法是使用Expression 函数

.exec(
   http("OrderCreation")
  .post("/abc/orders")
  .body(StringBody(session => s"""{ "orderReference": "${orderRef()}" }""")).asJSON
)

Here you are creating anonymous function taking Gatling Session and returning String as body.在这里,您正在创建匿名函数,以 Gatling Session并返回String作为主体。 String is composed via standard Scala string interpolation mechanism and uses before prepared function orderRef() .字符串是通过标准 Scala 字符串插值机制组成的,并在准备好的函数orderRef()之前使用。

Of course you can omit Scala string interpolation as:当然,您可以省略 Scala 字符串插值:

.body(StringBody(session => "{ \"orderReference\": " + orderRef() +" }" )).asJSON

which is not very preferred style when using Scala.这在使用 Scala 时不是很受欢迎的风格。

See more details at Gatling documentation to Request Body and read more about Galting EL syntax .请参阅请求正文的Gatling 文档中的更多详细信息,并阅读有关Galting EL 语法的更多信息。

An alternative way is to define a Feeder:另一种方法是定义一个 Feeder:

// Define an infinite feeder which calculates random numbers 
val orderRefs = Iterator.continually(
  // Random number will be accessible in session under variable "OrderRef"
  Map("OrderRef" -> Random.nextInt(Integer.MAX_VALUE))
)

val scn = scenario("RandomJsonBody")
  .feed(orderRefs) // attaching feeder to session
  .exec(
     http("OrderCreation")
    .post("/abc/orders")
    // Accessing variable "OrderRef" from session
    .body(StringBody("""{ "orderReference": "${OrderRef}" }""")).asJSON
  )

Here the situation is different, first we define the feeder, then we attach it to session and then use its value in request body via Gatling EL string .这里的情况有所不同,首先我们定义了 feeder,然后我们将它附加到 session 中,然后通过Gatling EL string在请求正文中使用它的值。 This works while the feeder value is taken from feeder by Gatling before an attached to session for each virtual user.在为每个虚拟用户附加到会话之前,Gatling 从 feeder 中获取 feeder 值时,这是有效的。 See more about feeders here .在此处查看有关馈线的更多信息。

Recommendation: If you scenario is simple, start with first solution.建议:如果您的方案很简单,请从第一个解决方案开始。 If it takes more complex think about feeders.如果需要更复杂的考虑馈线。

Enjoy享受

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

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