根据Gatling文档,执行场景时可以使用会话属性。
但是,每次我在场景中使用函数文字访问会话时,都会遇到以下异常:
[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$1(GatlingTask.scala:49)
[error] at io.gatling.sbt.GatlingTask.execute(GatlingTask.scala:48)
[error] at sbt.ForkMain$Run$2.call(ForkMain.java:296)
[error] at sbt.ForkMain$Run$2.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.
Run Code Online (Sandbox Code Playgroud)
更具体地说,虽然此符号为我提供了正确的结果:
val scn = scenario("Foobar").feed(feeder).exec {
http("foo").httpRequest("GET", "http://example.org")
}.pause(5)
Run Code Online (Sandbox Code Playgroud)
由于上述异常而失败:
val scn = scenario("Foobar").feed(feeder).exec { session =>
http("foo").httpRequest("GET", "http://example.org")
session
}.pause(5)
Run Code Online (Sandbox Code Playgroud)
方案是模拟的计划。因此,当您说val scn = ...您不是在执行仿真时,而是在构建一个AST,稍后通过加特林执行。
所以当你说
val scn = scenario("Foobar").feed(feeder).exec { session =>
http("foo").httpRequest("GET", "http://example.org")
session
}.pause(5)
Run Code Online (Sandbox Code Playgroud)
该部分http("foo").httpRequest("GET", "http://example.org")是不具有副作用且其值从未使用过的声明。因此它可能不存在。就加特林而言,您的情况是
val scn = scenario("Foobar").feed(feeder).exec { session =>
session
}.pause(5)
Run Code Online (Sandbox Code Playgroud)
绝对不执行任何操作,因此在生成报告时会产生错误。
要实现您想要的功能,会话操作必须是单独的exec语句。像这样:
val scn = scenario("Foobar").feed(feeder)
.exec ( session => session.set("foo", "bar") )
.exec (
http("foo").httpRequest("GET", "http://example.org")
)
}.pause(5)
Run Code Online (Sandbox Code Playgroud)