首页 > 解决方案 > 加特林负载测试 - 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 下定义)。

对此问题的任何帮助将不胜感激。提前致谢。

标签: xmlscalaload-testinggatlingscala-gatling

解决方案


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 中提取FileSessionExpression[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
  )

推荐阅读