1

I am new to gatling, I am trying to implement a counter. I am calling a post service by iterating through list of ids and I need to check the status for each iteration and increment the counter variable when the status is 200. I am have below code snippet,

def createData() = foreach("${userIdList}","userId"){
     exec(http("file upload").post("/compute-metaservice/datasets/${userId}/uploadFile")
       .formUpload("File","./src/test/resources/data/Test.csv")
       .header("content-type","multipart/form-data")
      .check(status is 200))   }

I need to add a if condition here something like if(status is 200){counter++}. Can someone please help on how I can implement this.

har123
  • 49
  • 6

1 Answers1

1

You can use session to store the value and then in exec call check it and modify counter:

  var counter = 0
  def createData() =
    foreach("${userIdList}", "userId") {
      exec(
        http("file upload")
          .post("/compute-metaservice/datasets/${userId}/uploadFile")
          .formUpload("File", "./src/test/resources/data/Test.csv")
          .header("content-type", "multipart/form-data")
          .check(status.saveAs("uploadStatus"))
      ).exec(session => {
        if (session("uploadStatus").as[Int] == 200) counter += 1
        session
      })
    }

I'm not sure if this the best way, but it should work.

dmle
  • 3,498
  • 1
  • 14
  • 22
  • 1
    `counter` should be a session attribute instead of a variable. What you have is shared by all the virtual users in a non-thread-safe way. – George Leung Apr 05 '21 at 05:42