如何在 Gatling 中创建基本的分页逻辑?
How to create basic pagination logic in Gatling?
所以我试图在 Gatling 中创建基本的分页,但失败得很惨。
我现在的场景是这样的:
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 window...我不明白它 )。我希望 asLongAs(currentPage <= totalPages) 重复 5 次,但它只重复两次。
我试图在 class 而不是对象中创建函数,因为据我所知只有一个对象。 (为了防止多个用户访问同一个变量,我也 运行 只有一个用户具有相同的结果)
我显然在这里缺少一些基本的东西(Gatling 和 Scala 的新手)所以任何帮助将不胜感激 :)
使用常规的 scala 变量来保存值是行不通的 - gatling 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)
}
}
所以我试图在 Gatling 中创建基本的分页,但失败得很惨。
我现在的场景是这样的:
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 window...我不明白它 )。我希望 asLongAs(currentPage <= totalPages) 重复 5 次,但它只重复两次。
我试图在 class 而不是对象中创建函数,因为据我所知只有一个对象。 (为了防止多个用户访问同一个变量,我也 运行 只有一个用户具有相同的结果) 我显然在这里缺少一些基本的东西(Gatling 和 Scala 的新手)所以任何帮助将不胜感激 :)
使用常规的 scala 变量来保存值是行不通的 - gatling 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)
}
}