简体   繁体   English

加特林:转换响应并将其写入JSON文件

[英]Gatling: Transforming response and writing it to JSON file

Fairly new to Scala/Gatling and I have searched the SO a lot, but nothing specific to my issue has been answered. 对Scala / Gatling来说还很陌生,我已经搜索了很多SO,但是没有任何针对我的问题的答案得到解决。 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" “3a917e22-3c76-45de-a104-4e2aa4b72a35”
"929c89c0-7a1a-4a18-8ee8-2958c9cd430f" “929c89c0-7a1a-4a18-8ee8-2958c9cd430f”

and what I want is : 我想要的是:
[ [

"3a917e22-3c76-45de-a104-4e2aa4b72a35" , “ 3a917e22-3c76-45de-a104-4e2aa4b72a35”,
"929c89c0-7a1a-4a18-8ee8-2958c9cd430f" “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"], [ “3a917e22-3c76-45de-a104-4e2aa4b72a35”],
["929c89c0-7a1a-4a18-8ee8-2958c9cd430f"] [ “929c89c0-7a1a-4a18-8ee8-2958c9cd430f”]

Basically used before after hooks of Gatling with some printwriter magic 基本上before after盖特林机上使用一些印刷机魔术师之后

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM