Search code examples
kotlingatling

How can I dynamically generate body for each request in gatling kotlin


I have the following code as below:

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("""
  basic scenario
""".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)
        )
    }

I am trying to issue one request with varying batch size ranging from 1 .. 1001 with 100 interval step. However when I run this it fails with this:

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


Solution

  • You got functions usage in Gatling wrong.

    Please read the exec documentation: you can't use Gatling components in functions in there.

    It's the other way around: you must pass your functions to the Gatling components, see doc.

    .foreach(payloadBatchSizes, "item").on(
      exec(
        http("collector")
          .post("/v1/events")
          .body(
            StringBody { session -> getPayloadBatch(session.getInt("item")) }
          )
      )
    )