在 Gatling 场景中将用户索引注入正文文件

Inject user index into body file in Gatling scenario

我想使用 ELFileBody 并将变量放入 txt 文件中。 该文件包含一个 soap 请求。

请求(场景)只执行一次,但与用户执行次数一样多。 我想放入文件变量,用户索引(执行位置)。

像这样:

.set("myVar", userIndex) //myVar is the variable declared in the body file ( ${myVar} )

这是我现在的代码:

  val users = 1500
  val baseUrl = "http://127.0.0.1:7001"

  val httpProtocol = http
    .baseURL(baseUrl)
    .inferHtmlResources()
    .acceptEncodingHeader("gzip,deflate")
    .contentTypeHeader("text/xml;charset=UTF-8")
    .userAgentHeader("Apache-HttpClient/4.1.1 (java 1.5)")

  val headers_0 = Map("SOAPAction" -> """""""")

  val uri1 = "http://127.0.0.1:7001/myProject-ws/myProjectWebService"

  val scn = scenario("Scenario1Name")
    .exec(http("scn.Scenario1Name")
      .post("/myProject-ws/myProjectWebService")
      .headers(headers_0)
      .body(RawFileBody("File_0000_request.txt")))

  setUp(scn.inject(atOnceUsers(users))).protocols(httpProtocol)

如何将用户索引注入请求正文中的 myVar 变量?

您需要读取文件,例如 val customSeparatorFeeder = separatedValues(pathToFile, separator).queue circular

scenario("Scenario1Name") 之后您需要添加 .feed(customSeparatorFeeder)

您可以在此处阅读更多相关信息 https://gatling.io/docs/2.3/session/feeder/

最后,我使用了一个 return 动态引用 (id) 的函数,并从我的场景中调用它。

def getDynamicId(): String = {
  val formatter = new SimpleDateFormat("yyyyMMddHHmmss.SSS")
  val result = "PM".concat(formatter.format(Calendar.getInstance().getTime()))
  result
} 

//[...]

scenario("ScenarioName")
  .exec(session => session.set("myVar", getDynamicId)) 

// [...]

.body(ElFileBody("BodyFile_0000_request.txt")))

在主体文件中,我有变量 ${myVar}