我有一个我正在尝试模拟的场景,但我不确定最好/最惯用的方法。
我正在使用的应用程序会生成一些非常大的报告,这些报告需要很长时间才能执行。对于我们的主要 Web 客户端,我们开始生成报告,然后轮询{"result":"COMPLETED"}响应中的类似内容。但是客户端只会轮询这么长时间——在没有看到COMPLETED响应的 5 分钟后,它放弃并向用户显示错误消息。我想做的就是在场景中将此视为失败。
我目前已经实现了这样的东西......
exitBlockOnFail {
group("report polling") {
exec(_.set("completed", false))
.doWhileDuring(session => !session("completed").as[Boolean], timeout) {
exec(
http("polling action")
...
.check(jsonPath("$.result").transform(result => "COMPLETED".equals(result)).saveAs("completed")
)
}
// if we got here and didn't succeed then we timed out. This will mark the group as failed.
// failures on the polling action trigger an exit from the block
.doIf(session => !session("completed").as[Boolean])
{
exec(session => {
//logging a message to the console as there's no way to get this into the simulation.log
println(s"**** $description failed took more than: ${timeout.toString} (user: ${session("user").as[String]}****")
session.markAsFailed
})
}
}
}
这在 gatling 报告中显示为组失败,但没有错误消息,因为失败与请求无关。这使得破译报告变得困难,因为某些组失败也可能是由轮询请求失败引起的。
我刚刚开始使用的另一种方法是check.transform在轮询请求中使用 a 在组执行开始时使用会话集中的时间戳来确定是否已超过超时。这意味着失败现在在请求中,并在报告中获得有意义的消息
group("report polling") {
exec(_.set("start", DateTime.now()))
.doWhileDuring(session => !session("result").as[String].equals("5"), timeout) {
exec( http("polling action")
...
.check(
responseTimeInMillis.transform((responseTime, session) => DateTime.now.getMillis - session("start")
.as[DateTime].getMillis).lte(timeout).name("Report generation timed out")
))
}
}}
但这感觉很混乱——轮询操作实际上并没有失败,并且围绕组行为的逻辑已经泄露到请求定义中。
理想情况下,我想.check在组上有一个方法,我可以断言组持续时间小于超时,但我认为这是不可能的。