CoderInTraining
CoderInTraining

Reputation: 15

Gatling Load Test - XML file as feed possible?

I have a an endpoint that takes a specifically formatted XML. I'm trying to use a similar function as circular on the XML file for the feed. I'm able to do this with a CSV file, but I can't seem to get this with an XML file. Is this even possible to do?

I've also read this: https://gatling.io/docs/3.0/session/feeder/?highlight=feeder#file-based-feeders

I'm also fairly new at gatling and have only written one load test thus far.

Here's the sample code I have:

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)
}

For some reason, the .feed() argument for csv works (csv(Environment.createResponseFeedCsv).circular; where createResponseFeedCsv is defined in my Environment file under utils).

Any help on this issue would be greatly appreciated. Thanks in advance.

Upvotes: 0

Views: 1749

Answers (1)

Mateusz Gruszczynski
Mateusz Gruszczynski

Reputation: 1510

CSV feeder works only with comma-separated values, so in theory you could prepare CSV file with just one column and that column can be list of single line representations of your XML files (assuming those did not contain any commas). But in your case it would be better to use fact that Feeder[T] is just an alias for Iterator[Map[String, T]] so you can define your own feeder which fe. reads list of files from certain directory and continually iterates over list of their paths:

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

This way this feeder will fill filePath attribute with path of files from xmls_directory_path directory. So if you load it with all your example XMLs you can call RawFileBody() with that filePath attribute (extracted with Gatling EL ):

val scn = scenario("Example Scenario")
  .feed(fileFeeder)
  .exec(
    http("Example request")
      .post("http://example.com/api/test")
      .body(RawFileBody("${filePath}"))
      .asXML
  )

Or if you fe. would like to use it in more scenarios you can define your own FeederBuilder class 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
}

In this case logic is similar, I've just changed it to feed file attribute with File object so it can be used in more use cases. Since it does not return String path, we need to extract it from File with SessionExpression[String] fe:

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
  )

Upvotes: 1

Related Questions