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

问题描述 投票:0回答:1

对Scala / Gatling来说相当新,我已经搜索了很多东西,但我的问题并未得到解答。所以这是我的代码

    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”

而我想要的是: [

'3A 17E-3 Draft-45D-104A-4A4'' '929 SESS.7A 1A.4。

]

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

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

scala gatling printwriter
1个回答
0
投票

基本上使用加特林的before after钩与一些版画魔术

这是我的代码:

 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")
}
© www.soinside.com 2019 - 2024. All rights reserved.