从会话中读取时如何发出 Gatling 捕获请求?

How do I make Gatling capture request while reading from the session?

根据 Gatling documentation,我可以在执行场景时使用会话属性。

但是,每次我在场景中使用函数字面量访问会话时,都会出现以下异常:

[error] java.lang.UnsupportedOperationException: There were no requests sent during the simulation, reports won't be generated
[error]     at io.gatling.charts.report.ReportsGenerator$.generateFor(ReportsGenerator.scala:45)
[error]     at io.gatling.app.Gatling.generateReports(Gatling.scala:198)
[error]     at io.gatling.app.Gatling.start(Gatling.scala:82)
[error]     at io.gatling.app.Gatling$.fromArgs(Gatling.scala:59)
[error]     at io.gatling.sbt.GatlingTask.liftedTree1(GatlingTask.scala:49)
[error]     at io.gatling.sbt.GatlingTask.execute(GatlingTask.scala:48)
[error]     at sbt.ForkMain$Run.call(ForkMain.java:296)
[error]     at sbt.ForkMain$Run.call(ForkMain.java:286)
[error]     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[error]     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
[error]     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
[error]     at java.lang.Thread.run(Thread.java:745)
[error] Simulation FooBarSimulation failed.
[info] Simulation(s) execution ended.

更具体地说,虽然这个符号给出了正确的结果:

  val scn = scenario("Foobar").feed(feeder).exec {
    http("foo").httpRequest("GET", "http://example.org")
  }.pause(5)

由于上述异常而失败:

  val scn = scenario("Foobar").feed(feeder).exec { session =>
    http("foo").httpRequest("GET", "http://example.org")
    session
  }.pause(5)

场景是模拟的计划。因此,当您说 val scn = ... 时,您并不是在 执行 模拟,而是构建一个 AST,稍后由 gatling 执行。

所以当你说

val scn = scenario("Foobar").feed(feeder).exec { session =>
  http("foo").httpRequest("GET", "http://example.org")
  session
}.pause(5)

http("foo").httpRequest("GET", "http://example.org") 部分是一条没有副作用的语句,它的值从未被使用过。 所以它还不如不存在就加特林而言,你的场景是

val scn = scenario("Foobar").feed(feeder).exec { session =>
  session
}.pause(5)

什么都不做,因此在生成报告时会产生错误。

要实现您想要的效果,会话操作必须是一个单独的 exec 语句。像这样:

val scn = scenario("Foobar").feed(feeder)
  .exec ( session => session.set("foo", "bar") )
  .exec (
    http("foo").httpRequest("GET", "http://example.org")
  )
}.pause(5)