繁体   English   中英

如何在加特林中正确地将会话值传递给方法 feed()

[英]How to pass session values to a method feed() in gatling correctly

我有以下问题。 当我尝试执行模拟时,出现此错误:

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:49)
    at io.gatling.app.RunResultProcessor.generateReports(RunResultProcessor.scala:62)
    at io.gatling.app.RunResultProcessor.processRunResult(RunResultProcessor.scala:40)
    at io.gatling.app.Gatling$.start(Gatling.scala:88)
    at io.gatling.app.Gatling$.fromMap(Gatling.scala:41)
    at Engine$.delayedEndpoint$Engine$1(Engine.scala:11)
    at Engine$delayedInit$body.apply(Engine.scala:4)
    at scala.Function0.apply$mcV$sp(Function0.scala:39)
    at scala.Function0.apply$mcV$sp$(Function0.scala:39)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:17)
    at scala.App.$anonfun$main$1$adapted(App.scala:80)
    at scala.collection.immutable.List.foreach(List.scala:392)
    at scala.App.main(App.scala:80)
    at scala.App.main$(App.scala:78)
    at Engine$.main(Engine.scala:4)
    at Engine.main(Engine.scala)

Process finished with exit code 1

下面是我的代码:

package simulations

import io.gatling.core.Predef._
import io.gatling.core.scenario.Simulation
import io.gatling.http.Predef._

class Load extends Simulation{

  val httpProtocol = http
    .baseUrl("http://localhost:8080/app/")
    .header("Accept", "application/json")

  val scn = scenario("Scenario").exec(SimpleExample.simple)

    setUp(
      scn.inject(atOnceUsers(3)).protocols(httpProtocol)
    )
}


package simulations

import io.gatling.core.Predef._
import io.gatling.http.Predef._    
import scala.util.Random

object SimpleExample {

  var simple =
    exec(session => session
      .set("rndmSTR", randomString())
      .set("rndmINT", randomInt())
    ).
      exec(
        session => {
          exec(feed(Iterator.continually(Map(
            "game_ID" -> session("rndmINT").as[String].toInt,
            "game_Name" -> session("rndmSTR").as[String]
          ))))
            .exec(
              http("Post New Game")
                .post("videogames/")
                .body(ElFileBody("bodies/newtemplate.json")).asJson
            )
          session
        }
      )

  private def randomString() = {
    new Random().alphanumeric.filter(_.isLetter).take(5).mkString.toLowerCase
  }    
  private def randomInt() = {
    new Random().nextInt(100000)
  }

}

这是我的 .json:

{
  "id": "${game_ID}",
  "name": "${game_Name}",
  "releaseDate": "2020-08-11",
  "reviewScore": 99,
  "category": "Driving",
  "rating": "Mature"
}

我知道我可以使用 feed() 方法如下:

package simulations

import io.gatling.core.Predef._
import io.gatling.http.Predef._    
import scala.util.Random    

object NextSimpleExample {

  var rndName: String = null
  var rndID: String = null    
  var feeder = Iterator.continually(Map(
    "game_ID" -> rndID.toInt,
    "game_Name" -> rndName
  ))    

  var simple =
    exec(session => session
      .set("rndmSTR", randomString())
      .set("rndmINT", randomInt())
    ).
      exec(
        session => {
          rndID = session("rndmINT").as[String]
          rndName = session("rndmSTR").as[String]
          session
        }
      )
      .exec(feed(feeder)
        .exec(
          http("Post New Game")
            .post("videogames/")
            .body(ElFileBody("bodies/newtemplate.json")).asJson)
      )

  private def randomString() = {
    new Random().alphanumeric.filter(_.isLetter).take(5).mkString.toLowerCase
  }    
  private def randomInt() = {
    new Random().nextInt(100000)
  }

}

但在这种情况下,所有虚拟用户都会获得相似的值......

另外,我想在接下来的步骤中为每个虚拟用户值使用生成的值。 例如,在另一个 .json 文件中为 post 或 put 请求中的另一个主体插入生成的 id 和 name。 请帮助解决这个问题。

gatling DSL 定义了一次创建的构建器,因此任何类型的参考代码,如

exec(session => session
  .set("rndmSTR", randomString())
  .set("rndmINT", randomInt())
)

将为所有用户产生相同的值。

您的第二个示例确实是通过转移到普通 scala 变量的方式来重述第一个示例的复杂方式-您仍然只运行一次“随机”函数。

但是您已经接近了 - 如果您将随机函数移动到 feeder 定义中,它将起作用,因为每次调用 .feed 时都会评估这些函数。

var feeder = Iterator.continually(Map(
  "game_ID" -> randomString(),
  "game_Name" -> randomInt()
))    

所以你根本不需要会话功能

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM