简体   繁体   English

在 Gatling / Scala 中循环来自先前请求的多个响应匹配

[英]Loop over multiple response matches from previous request in Gatling / Scala

I'm still pretty new to Gatling / Scala, so my apologies if I've misunderstood something obvious, but...我对 Gatling / Scala 还是很陌生,所以如果我误解了一些明显的东西,我很抱歉,但是......

I have a scenario with a sequence of requests.我有一个包含一系列请求的场景。 One of them is along the lines of:其中之一是:

.exec (
  http("Get IDs")
  .post(<urlpath>)
  .body(<body text>)
  .headers(<headerinfo>)
  .check(jsonPath("$[*].some.json.path").findAll.transform(_.map(_.replace("unwantedchars,""))).saveAs(myIds)
)

And that works fine, returning a vector of all matching json elements, with the unwanted chars removed.这工作正常,返回所有匹配 json 元素的向量,删除不需要的字符。 What I'm trying to do next is loop over the first 5 ids, and pass them into the next request.我接下来要做的是循环遍历前 5 个 id,并将它们传递到下一个请求中。 I've tried assorted variants of the following, but no amount of variation / googling has returned the actual solution:我已经尝试了以下各种变体,但没有多少变化/谷歌搜索返回了实际的解决方案:

.exec( session => {
  val firstFive = session("myIds").as[Vector[String]].toArray.take(5)
  for (inx <- 0 until 4){
    exec(
      http("get the item")
        .get("/path/to/item/thing/" + firstFive(inx))
        .headers(<etc etc>)
      )
  session
})

So I understand nested exec's aren't supported - but how can I create a block that combines the for-loop, and the actual HTTP requests?所以我知道不支持嵌套的 exec - 但是如何创建一个结合 for 循环和实际 HTTP 请求的块?

Am I missing something obvious?我错过了一些明显的东西吗?

if you're looking to take 5, then you just need to put them in the session and then process them with a .foreach block.如果你想拿 5,那么你只需要把它们放在会话中,然后用 .foreach 块处理它们。

so instead of taking the first five and trying to make exec calls (which, as you observed, won't work), save the list back to the session因此,不要使用前五个并尝试进行 exec 调用(正如您所观察到的那样,这是行不通的),而是将列表保存回会话

.exec( session => {
  val firstFive = session("myIds").as[Vector[String]].take(5)
  session.set("firstFive", firstFive)
)

then use the DSL to loop through the collection然后使用 DSL 循环遍历集合

.foreach("${firstFive}", "id") {
  exec(
    http("get the item")
      .get("/path/to/item/thing/${id}")
      .headers(<etc etc>)
  )
}

you could even add the selection of the first five to your transform step - so the whole thing could be...您甚至可以将前五个的选择添加到您的转换步骤中 - 所以整个事情可能是......

.exec (
  http("Get IDs")
    .post(<urlpath>)
    .body(<body text>)
    .headers(<headerinfo>) .check(jsonPath("$[*].some.json.path").findAll.transform(_.map(_.replace("unwantedchars,"")).take(5).saveAs(myIds)
)
.foreach("${myIds}", "id") {
  exec(
    http("get the item")
      .get("/path/to/item/thing/${id}")
      .headers(<etc etc>)
  )
}

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

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