0
votes

Fairly new to Scala/Gatling and I have searched the SO a lot, but nothing specific to my issue has been answered. So here's my code

    val adminUplFile: ScenarioBuilder = scenario("Admin user uploads file")
    .exec(http("Upload File")
      .post("/file") 
      .header("username", "example") r
      .header("password", "80HL/d1fETqxuCArAbIU6/Sb3F2nSTCqw/eqw1lzJio=") 
      .formUpload("file", "uploadFile") // body params ("key" , "path of the file/name")
      .formParamSeq(Seq(("uploadByID", "1"), ("location", "meetings"), ("personID", "0"), ("uploadTimeStamp", "2019-01-15 10:01:04.426"), ("md5Hash", "n95QI48+Uqxfw0hTnJdqMA=="))) 
      .check(status.is(200)) 
      .check(jsonPath("$.uuid").saveAs("guidList"))) 
    .exec { session =>
    val writer = new PrintWriter(new FileOutputStream(new File("src/gatling/resources/extractedData/GuidList.json"), true))
    writer.write(session("\"guidList\"").as[String].trim)
    writer.write("\n")
    writer.close()
    session  }

setUp(
    adminUplFile.inject(constantUsersPerSec(2) during (1 seconds)).protocols(httpConf)
  )

and here's the response I'm trying to extract :

 {
    "uuid": "3a917e22-3c76-45de-a104-4e2aa4b72a35"
}

So what I get written to the file is :
"3a917e22-3c76-45de-a104-4e2aa4b72a35"
"929c89c0-7a1a-4a18-8ee8-2958c9cd430f"

and what I want is :
[

"3a917e22-3c76-45de-a104-4e2aa4b72a35" ,
"929c89c0-7a1a-4a18-8ee8-2958c9cd430f"

]

just looking at it, it looks a simple case of adding square brackets and a comma to the file but in reality, with every virtual user trying to append the same file I'm struggling on how to make it format in a way that square brackets don't get used again and only once.

So I don't want :
["3a917e22-3c76-45de-a104-4e2aa4b72a35"],
["929c89c0-7a1a-4a18-8ee8-2958c9cd430f"]

1
Maybe open the bracket in the file before starting the scenario and close the bracket after scenario completes? Unless you need to use the file in the scenario later. Or synchronize writes to the file. If you are familiar with akka actors (and Gatling is built on akka) having an actor that writes to the file might be a good idea.yahor

1 Answers

0
votes

Basically used before after hooks of Gatling with some printwriter magic

Here is my code :

 try {
  val br = new BufferedReader(new FileReader(deleteGuidsFilePath))
  var last : String = ""

  var line = br.readLine
  while ({line != null})
  {
    last = line
    line = br.readLine
  }

  br.close()
  afterPw.write(last.replace(",", "]"))
  afterPw.close()
}
catch {
  case e: Throwable => println("Couldn't read the file")
}