Gatling:转换响应并将其写入 JSON 文件

Gatling: Transforming response and writing it to JSON file

对 Scala/Gatling 相当陌生,我搜索了很多 SO,但没有针对我的问题的任何具体问题得到解答。所以这是我的代码

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

这是我要提取的回复:

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

所以我写入文件的内容是:
“3a917e22-3c76-45de-a104-4e2aa4b72a35”
“929c89c0-7a1a-4a18-8ee8-2958c9cd430f”

而我想要的是:
[

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

]

只是看看它,它看起来很简单,只是在文件中添加方括号和逗号,但实际上,每个虚拟用户都试图附加同一个文件,我正在努力研究如何将其格式化为方括号不再使用且仅使用一次的方法。

所以我不想:
[“3a917e22-3c76-45de-a104-4e2aa4b72a35”],
[“929c89c0-7a1a-4a18-8ee8-2958c9cd430f”]

基本上使用 before after Gatling 的 hooks 和一些 printwriter magic

这是我的代码:

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