Gatling Kotlin动态生成请求体失败:无请求发送报错排查
Gatling Kotlin仿真无请求发送问题排查与解决
问题描述
尝试在Gatling Kotlin中实现批量大小从1到1001(步长100)的请求,为每个请求动态生成请求体,但运行后仿真在0秒内完成,出现无请求发送的报错,无法生成报告。
原代码
private val payloadBatchSizes = (1..1001 step 100).toList() private val httpProtocol = http .baseUrl(apiGatewayUrl) .acceptHeader("application/json") .header("A", "foo") .header("B", "bar") .header("C", "foo") .header("Authorization", authToken) private val success = scenario("""\n basic scenario\n""".trimIndent()).foreach(payloadBatchSizes, "item", "counter").on( exec { session -> http("collector") .post("/v1/events") .body(StringBody(getPayloadBatch(session.getInt("item")))) session } ) init { setUp(success.injectOpen( atOnceUsers(1) ).protocols(httpProtocol) ) }
报错信息
Simulation completed in 0 seconds Parsing log file(s)... Parsing log file(s) done Generating reports... Exception in thread "main" java.lang.UnsupportedOperationException: There were no requests sent during the simulation, reports won't be generated at io.gatling.charts.report.ReportsGenerator.generateFor(ReportsGenerator.scala:46) at io.gatling.app.RunResultProcessor.generateReports(RunResultProcessor.scala:63) at io.gatling.app.RunResultProcessor.processRunResult(RunResultProcessor.scala:38) at io.gatling.app.Gatling$.start(Gatling.scala:99) at io.gatling.app.Gatling$.fromArgs(Gatling.scala:51) at io.gatling.app.Gatling$.main(Gatling.scala:39) at io.gatling.app.Gatling.main(Gatling.scala)
问题原因
原代码中exec的lambda函数仅创建了HTTP请求对象,但未将其返回给Gatling执行,而是直接返回session。Gatling会判定该步骤无任何可执行动作,因此跳过请求发送逻辑,最终导致无请求被执行。
解决方案
修改exec块逻辑,将HTTP请求构建后的动作返回给Gatling,而非直接返回session,以下两种方式均可:
方式1:直接将HTTP请求作为exec参数
private val success = scenario("basic scenario").foreach(payloadBatchSizes, "item", "counter").on( exec( http("collector") .post("/v1/events") .body(StringBody { session -> getPayloadBatch(session.getInt("item")) }) ) )
方式2:在lambda中返回HTTP请求动作
private val success = scenario("basic scenario").foreach(payloadBatchSizes, "item", "counter").on( exec { session -> http("collector") .post("/v1/events") .body(StringBody(getPayloadBatch(session.getInt("item")))) .build(session) } )
注:场景名称中的换行符可直接去除,避免不必要的格式问题。
内容的提问来源于stack exchange,提问作者CyberPunk
相关产品推荐
相关产品推荐

