本文介绍了如何在Gatling的Json Body中添加随机值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每次都需要创建一个随机正整数,并将其发送到加特林(Gatling)的Json正文中.

I need to create a random positive integer each time and send it to Json body in Gatling.

我使用以下代码创建了一个随机正整数:

I used the below code to create a random positive ineger:

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

但是,如何将随机生成的值输入到json主体中?

but, How can I feed the randomly generated value into the json body?

我尝试过:

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

但是,这似乎不起作用.请提供任何线索.

But, this doesn't seem to work. Any clues please.

谢谢!

推荐答案

首先,您希望每次生成随机数,因此OrderRef必须是一种方法,例如:

First of all you want to generate random number each time, thus OrderRef has to be a method, like:

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

旁注:根据Scala约定:命名camelCase,()时会生成新值,但最后不带;.

Side comment: by Scala convention: name camelCase, () while it generates new values, without ; in the end.

要使用准备好的方法,您不能使用 Gatling EL字符串.语法非常有限,基本上"${OrderRef}"在Gatling Session中搜索名称为OrderRef的变量.

To use the prepared method you cannot use the Gatling EL string. Syntax is very limited and basically "${OrderRef}" searches for variable with name OrderRef in Gatling Session.

正确的方法是将 Expression函数用作:

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

在这里,您正在创建匿名函数,将Gatling Session并返回String作为正文.字符串是通过标准Scala字符串插值机制组成的,并且在准备好的函数orderRef()之前使用.

Here you are creating anonymous function taking Gatling Session and returning String as body. String is composed via standard Scala string interpolation mechanism and uses before prepared function orderRef().

您当然可以省略Scala字符串插值,如下所示:

Of course you can omit Scala string interpolation as:

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

使用Scala时不是很喜欢的样式.

which is not very preferred style when using Scala.

在Gatling文档中查看更多详细信息,以获取请求正文,并详细了解高棉EL语法.

See more details at Gatling documentation to Request Body and read more about Galting EL syntax.

另一种方法是定义Feeder:

An alternative way is to define a 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
  )

这里的情况有所不同,首先我们定义供稿器,然后将其附加到会话,然后通过 Gatling EL字符串在请求正文中使用其值.在每个虚拟用户附加到会话之前,Gatling从馈线中获取馈线值时,此方法有效.在此处查看更多有关Feeder的信息.

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. This works while the feeder value is taken from feeder by Gatling before an attached to session for each virtual user. See more about feeders here.

建议:如果您的情况很简单,请从第一个解决方案开始.如果需要更复杂的方法,请考虑馈线.

Recommendation: If you scenario is simple, start with first solution. If it takes more complex think about feeders.

享受

这篇关于如何在Gatling的Json Body中添加随机值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 04:26