xml - 加特林负载测试 - XML 文件作为饲料可能?
问题描述
我有一个采用特定格式的 XML 的端点。我正在尝试在提要的 XML 文件上使用与循环类似的功能。我可以用 CSV 文件做到这一点,但我似乎无法用 XML 文件做到这一点。这甚至可能吗?
我也读过这个:https ://gatling.io/docs/3.0/session/feeder/?highlight=feeder#file-based-feeders
我对 gatling 也很陌生,到目前为止只写了一个负载测试。
这是我的示例代码:
object ProcessXml {
val createProcessHttp = http("Process XML")
.post("/myEndpointPath")
.body(RawFileBody("data/myXml.xml"))
.asXML
.check(status is 200)
val createProcessShipment = scenario("Process Shipment XML")
.feed(RawFileBody("data/myXml.xml).circular) // feed is empty after first user
.exec(createProcessHttp)
}
出于某种原因,csv 的 .feed() 参数有效(csv(Environment.createResponseFeedCsv).circular;其中 createResponseFeedCsv 在我的环境文件中的 utils 下定义)。
对此问题的任何帮助将不胜感激。提前致谢。
解决方案
CSV feeder 仅适用于逗号分隔的值,因此理论上您可以准备仅包含一列的 CSV 文件,并且该列可以是 XML 文件的单行表示的列表(假设那些不包含任何逗号)。但在你的情况下,最好使用 fact thatFeeder[T]
只是一个别名,Iterator[Map[String, T]]
这样你就可以定义你自己的 feeder which fe。从某个目录读取文件列表并不断迭代它们的路径列表:
val fileFeeder = Iterator.continually(
new File("xmls_directory_path") match {
case d if d.isDirectory => d.listFiles.map(f => Map("filePath" -> f.getPath))
case _ => throw new FileNotFoundException("Samples path must point to directory")
}
).flatten
这样,此馈线将使用目录filePath
中文件的路径填充属性xmls_directory_path
。因此,如果您使用所有示例 XML 加载它,您可以RawFileBody()
使用该属性调用它(使用Gatling ELfilePath
提取):
val scn = scenario("Example Scenario")
.feed(fileFeeder)
.exec(
http("Example request")
.post("http://example.com/api/test")
.body(RawFileBody("${filePath}"))
.asXML
)
或者,如果你觉得。想在更多场景中使用它,您可以定义自己的FeederBuilder
类 fe:
class FileFeeder(path: String) extends FeederBuilder[File]{
override def build(ctx: ScenarioContext): Iterator[Map[String, File]] = Iterator.continually(
new File(path) match {
case d if d.isDirectory => d.listFiles.map(f => Map[String, File]("file" -> f))
case _ => throw new FileNotFoundException("Samples path must point to directory")
}
).flatten
}
在这种情况下,逻辑是相似的,我只是将其更改为file
带有对象的 feed 属性,File
以便可以在更多用例中使用它。由于它不返回String
路径,我们需要从 fe 中提取File
它SessionExpression[String]
:
val scn = scenario("Example Scenario")
.feed(new FileFeeder("xmls_directory_path"))
.exec(
http("Example request")
.post("http://example.com/api/test")
.body(RawFileBody(session => session("file").as[File].getPath))
.asXML
)
推荐阅读
- c# - 当我将图像提供给经过训练的模型时,它会为未在模型中训练的对象提供 80% 以上的准确度
- php - 如何使用 php 将 url 编码为 json
- c - 如何检测C中的整数溢出
- docker - 我的 pod 在 openshift 上不允许进行 chown 操作
- graphql - 如何使用 apollo-tooling 获取 https://example.com/graphql 的 schema.json
- javascript - 将 tabindex 属性应用于 asp:Menu 控件以获得焦点
- mysql - 如何使用子查询导致减法?
- rust - 带有结构的向量的多次迭代“不能移出借来的内容”
- s4sdk - S4 SDK调用需要很长时间并且没有错误地停止
- python - 无法使用熊猫从一组文件夹中读取 excel 文件