You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 17:07:57