【发布时间】:2014-03-09 07:39:13
【问题描述】:
我正在使用 Gatling 进行负载测试。当我创建客户资料时,将生成客户 ID。我能够提取客户 ID 并保存到会话变量中。
但是如何将这些值存储到文本文件中。
请帮帮我。
【问题讨论】:
标签: performance-testing scala-collections gatling
我正在使用 Gatling 进行负载测试。当我创建客户资料时,将生成客户 ID。我能够提取客户 ID 并保存到会话变量中。
但是如何将这些值存储到文本文件中。
请帮帮我。
【问题讨论】:
标签: performance-testing scala-collections gatling
有几种方法可以实现。
如果您熟悉 Scala,您可以:
如果这对你来说太复杂了,最简单的方法是使用 logback:
关于第二种解决方案,请查看 logback 文档:http://logback.qos.ch/documentation.html
【讨论】:
在下面的示例中,我将提取的 NumberIDs 从 SOAP 响应中保存出来,然后将它们保存到 numbersIDs.csv 文件中,以便将其输入另一个请求。
//Variables to store the extracted values of the response
var numberIDs : Seq[String] = _
//Java Writer
val numberIDs_writer = {
val fos = new java.io.FileOutputStream("numbersIDs.csv")
new java.io.PrintWriter(fos,true)
}
val scn = scenario("basicSimulation")
.exec(http("Request_One")
.post("/services")
.body(StringBody(inputXMLpayLoad))
.headers(Request_One_Header)
.check(bodyString.saveAs("Request_Response")) //Save the response in a variable called Request_Response
.check((status.is(200)))
//Extract all numberIDs of the SOAP response
.check(regex("""<NumberId>([\d]+)</NumberId>""")
.findAll
.transform { string => numberIDs = string; string }
.saveAs("numberIDs")))
//Save variable numberIDs into the csv file
.foreach("${numberIDs}", "id") {
exec(session => {
numberIDs_writer.println(session("id").as[String])
session
})
}
【讨论】:
从 2.0.0-M1 开始,infoExtractor hook 需要一个 Session 参数: https://github.com/excilys/gatling/issues/1004
还有一个内置函数可以在请求失败时将 Session 内容添加到 Simulation.log 记录中。 https://github.com/excilys/gatling/commit/b7b6299c658d0aa1b88971208eb0c98a9808e37f
如果你只是想用 logback 登录,你可以修改这个例子。
class MySimulation extends Simulation with Logging {
def logSessionOnFailure(status: RequestStatus, session: Session, request: Request, response: ExtendedResponse): List[String] = {
if (status == KO) logger.error(session)
Nil
}
val httpConf = http
...
.extraInfoExtractor(logSessionOnFailure)
...
}
【讨论】:
另一种可能性(未经测试,但可以工作)是使用响应转换器(在 .post 之后调用 .transformResponse)。在转换器主体中,您将获得一个响应对象,您可以从中提取生成的 ID 并将其附加到文件、集合等中。然后将原始响应作为转换结果返回。然而,从设计的角度来看,这不是一个很好的解决方案,因为您的转换有副作用。
【讨论】: