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

时间:2017-10-25 10:00:15

标签: gatling scala-gatling

我想使用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变量?

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

最后,我使用了一个返回动态引用(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}

相关问题