【问题标题】:How can I increment a counter in steps of 50?如何以 50 步递增计数器?
【发布时间】:2019-04-23 09:11:05
【问题描述】:

我有以下代码

package lts

import io.gatling.core.Predef._
import io.gatling.http.Predef._

class BankingSimulation extends BaseSimulation {
  val paginateThroughCustomTransactionsView = scenario("Scenario 04: Paginate through custom transactions view")
    .feed(csv("scenario04.csv").circular)
    .exec(http("04_paginateThroughCustomTransactionsView")
      .get("/api/savings/transactions?viewfilter=${viewEncodedkey}&offset=0&limit=50")
      .header("accept", "application/json")
      .check(jsonPath("$..encodedKey").saveAs("myEncodedKey"))
    )
    .asLongAs("${myEncodedKey.exists()}","offsetCounter", exitASAP = false) {
      exec(http("04_paginateThroughCustomTransactionsView")
        .get("/api/savings/transactions?viewfilter=${viewEncodedkey}&offset=${offsetCounter}&limit=50")
        .header("accept", "application/json")
        .check(jsonPath("$..encodedKey").saveAs("myEncodedKey"))
      )
    }


  setUp(
 paginateThroughCustomTransactionsView.inject(incrementConcurrentUsers(1).times(1).eachLevelLasting(1))
  ).protocols(httpProtocol)
}

目前该方案有效,但 offsetCounter 每次都增加 1。如何将其增加 50?

【问题讨论】:

    标签: gatling scala-gatling


    【解决方案1】:

    也许是更好的方法...不要依赖循环计数器,而是使用进纸器

    var offsetFeeder = (50 to 1000 by 50).toStream.map(i => Map("offsetCounter" -> i)).toIterator
    

    然后在你的 .asLongAs 块中,就在

    .feed(offsetFeeder)
    

    并执行您的“04_paginateThroughCustomTransactionsView”调用

    【讨论】:

    • 这也很棒,但不幸的是我需要根据时间而不是限制运行测试。但这肯定会在其他地方帮助我:D 谢谢!
    • 你不是一直在循环直到没有 myEncodedKey 的结果吗?
    • 嗯,有点...更多的是对数据的完整性检查。大约 100 万个编码键用于分页。这需要几个小时才能完成,但我更感兴趣的是为每个新应用版本设置 30m 测试。
    • 如果你想循环有限的时间,你可以用 scalafiniteDuration 来做到这一点。例如:.exec(session => session.set("timeout", 3 hours.fromNow)) .asLongAs(session("timeout").as[Deadline].hasTimeLeft) {...}
    • 我还没有迁移到 gatling 3,但我发现它现在对这类东西有一些原生支持
    【解决方案2】:

    好的,显然您可以对会话进行各种操作。在.asLongAs 部分中运行调用(exec)之前,您必须

    exec {session =>
     val offsetCounter = session("counter").as[Int] * 50
     session.set("offsetCounter", offsetCounter)
    }
    

    所以代码变成了

    val paginateThroughCustomTransactionsView = scenario("Scenario 04: Paginate through custom transactions view")
        .feed(csv("scenario04.csv").circular)
        .exec(http("04_paginateThroughCustomTransactionsView")
          .get("/api/savings/transactions?viewfilter=${viewEncodedkey}&offset=0&limit=50")
          .header("accept", "application/json")
          .check(jsonPath("$..encodedKey").saveAs("myEncodedKey"))
        )
        .asLongAs("${myEncodedKey.exists()}","counter", exitASAP = false) {
          exec {session =>
            val offsetCounter = session("counter").as[Int] * 50
            session.set("offsetCounter", offsetCounter)
          }
          .exec(http("04_paginateThroughCustomTransactionsView")
            .get("/api/savings/transactions?viewfilter=${viewEncodedkey}&offset=${offsetCounter}&limit=50")
            .header("accept", "application/json")
            .check(jsonPath("$..encodedKey").saveAs("myEncodedKey"))
          )
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 2021-05-16
      相关资源
      最近更新 更多