如何在Gatling中创建基本分页逻辑?

问题描述 投票:1回答:1

所以我试图在加特林创建基本分页,但失败了。

我当前的情况如下:

1)首次调用始终是一个POST请求,主体包括页面索引1和页面大小50

 {"pageIndex":1,"pageSize":50}

然后我将收到50个对象+环境中对象的总数:

"totalCount":232

由于我需要遍历环境中的所有对象,因此我需要将此调用POST 5次,每次都使用更新的pageIndex。

我当前(失败)的代码如下:

  def getAndPaginate(jsonBody: String) = {
    val pageSize = 50;
    var totalCount: Int = 0
    var currentPage: Int = 1
    var totalPages: Int =0
    exec(session => session.set("pageIndex", currentPage))
    exec(http("Get page")
      .post("/api")
      .body(ElFileBody("json/" + jsonBody)).asJson
          .check(jsonPath("$.result.objects[?(@.type == 'type')].id").findAll.saveAs("list")))
      .check(jsonPath("$.result.totalCount").saveAs("totalCount"))
      .exec(session => {
        totalCount = session("totalCount").as[Int]
        totalPages =  Math.ceil(totalCount/pageSize).toInt
        session})
      .asLongAs(currentPage <= totalPages)
      {
        exec(http("Get assets action list")
          .post("/api")
          .body(ElFileBody("json/" + jsonBody)).asJson
          .check(jsonPath("$.result.objects[?(@.type == 'type')].id").findAll.saveAs("list")))
        currentPage = currentPage+1
        exec(session => session.set("pageIndex", currentPage))
        pause(Config.minDelayValue seconds, Config.maxDelayValue seconds)

      }
  }

当前,分页值没有分配给我在函数开始时创建的变量,如果我在对象级别创建变量,则将以我不理解的方式分配它们。例如,Math.ceil(totalCount / pageSize).toInt的结果为4,而应为5。(如果我在立即窗口中执行它,则为5。。。。)我希望asLongAs(currentPage <= totalPages)重复5次,但只重复两次。

我试图在类而不是对象中创建函数,因为据我所知只有一个对象。 (为防止多个用户访问同一变量,我也只运行了一个具有相同结果的用户)我显然在这里缺少一些基本的东西(对Gatling和Scala来说是新的),所以任何帮助将不胜感激:)

scala gatling
1个回答
0
投票

使用常规的scala变量来保存值将不起作用-加特林DSL定义了仅在启动时执行一次的构建器,因此像]这样的行>

.asLongAs(currentPage <= totalPages)

仅将使用初始值执行。

所以您只需要使用会话变量来处理所有事情

def getAndPaginate(jsonBody: String) = {
  val pageSize = 50;

  exec(session => session.set("notDone", true))
  .asLongAs("${notDone}", "index") {
    exec(http("Get assets action list")
      .post("/api")
      .body(ElFileBody("json/" + jsonBody)).asJson
      .check(
        jsonPath("$.result.totalCount")
          //transform lets us take the result of a check (and an optional session) and modify it before storing - so we can use it to get store a boolean that reflects whether we're on the last page
          .transform( (totalCount, session) => ((session("index").as[Int] + 1) * pageSize) < totalCount.toInt)
          .saveAs("notDone")
      )
    )
    .pause(Config.minDelayValue seconds, Config.maxDelayValue seconds)
  }
}   
© www.soinside.com 2019 - 2024. All rights reserved.