如何以50为单位递增计数器?

问题描述 投票:0回答:2

我有以下代码

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
2个回答
2
投票

也许是一种更好的方式......不要依赖循环计数器而是使用喂食器

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

然后在你的.asLongAs块里面

.feed(offsetFeeder)

并执行您的“04_paginateThroughCustomTransactionsView”调用


0
投票

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

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"))
      )
    }
© www.soinside.com 2019 - 2024. All rights reserved.