1

I need to iterate over a number of files within a directory in order to perform a file upload with each of them. The directory resides in src/test/resources.

I understand Gatling’s file feeders, but I’m not seeing any that allow me to look up files from a directory (the names of the files are arbitrary and shouldn’t be hard-codes in the test if at all possible).

What’s the best way to go about this?

ZacharyAKlein
  • 633
  • 4
  • 9
  • Can you not - list all files in folder - make a list of Map e.g. `List(Map("file", file01.txt), Map("file", file02.txt) ...`) - Use `Iterator(myList)` (check the API) ? – ccheneson May 06 '21 at 13:37

1 Answers1

2

First you need feeder with files. That's an Array made of Maps. This maps need to have String as a key and each file will have it's own map with things we need.

Let's say we need just a name, so something like that should work:

def getFilePropsFromDir(dir: String): Array[Map[String, String]] = {
  val d = new File(dir)

  if (d.exists && d.isDirectory) {
    d.listFiles.filter(_.isFile).map(x => Map("path" -> x.toString))
  } else {
    Array()
  }
}

val feederWithFiles = getFilePropsFromDir("src/test/resources/dir_with_files/")

Then you need scenario like that (I don't upload anything)

val sut = scenario("Just feed files and query google")
    .feed(feederWithFiles)
    .exec(session => {
        val path = session("path").as[String] // get values by key from map - we had only "path" there
        println(path)
        val fileToUpload = getFileToUpload(path) // dummy function

        session.setAll(
          // prepare data for using later. Also k -> v
          ("fileToUpload", fileToUpload), 
          // another entry to illustrate how to use session elements
          ("googleAddress","http://google.com") 
        )
      }
    )
    .exec(
      exec(
          http("Should do something that uploads, but I just GET google")
            .get("${googleAddress}") // access key "googleAddress" from session
        )
    )

  setUp(
    sut.inject(rampUsers(1).during(1.seconds))
  ).protocols(http)

  def getFileToUpload(path: String): String = {
    path
  }

I created 2 files and GET was executed 2 times. Now you need to figure out how to do upload.

Imports that I have:

import io.gatling.core.Predef._
import io.gatling.core.body.StringBody
import io.gatling.core.structure.ChainBuilder
import io.gatling.http.Predef.http
import java.io.File
import scala.concurrent.duration._
import scala.io.Source
pocza
  • 632
  • 6
  • 10
  • Beware that your strategy relies on `src/test/resources` existing, ie running from an exploded project, not a packaged jar. Wondering if it's possible to have classpath resolution instead. – Stéphane LANDELLE May 07 '21 at 05:18
  • @pocza Your solution seems to work, but I'm curious why there's nested `exec` calls in the second code snippet? – ZacharyAKlein May 12 '21 at 00:05