Gatling-Scala : How to consolidated( group strings) from a loop output to a String Array or Vector Array

Gatling-Scala : How to consolidated( group strings) from a loop output to a String Array or Vector Array

  1. I need to loop with array value providing string output
  2. consolidated all the retrieved string output as an array
  3. use the consolidated array in next bulk api request.

My logic is not working, kindly help me.

Please help me if there are easier methods with gatling keywords

 var i = 0 // initialising i to 0 to increase the array index value in loop
.
.
...check(jsonPath("$.id").saveAs("ArrayValues"))
// saving this array to use in loop and at defining arraylength at last of the loop

//loop

    .foreach("#{ArrayValues}", "VValues") {
      exec(http("perftest values")
        .post("perftest")
        .body(StringBody(session =>
          s"""{
             |"id": "",
             |"perfTypeId": "00334-0000-0000-0345",
             |"legs":[{
             |      "valueTypeId": "111-220-3300-0444",
             |      "values": "${session("VValues").as[String]}"
             |    }]
             |     }""".stripMargin)).asJson
        .check(jsonPath("$.id").saveAs("perfvalue_singlevalue"))
        .check(status.is(201)))
        .exec(session => {
          val valuearraylen: Vector[String] = session("ArrayValues").as[Vector[String]]
          val perfvalue_array: Array[String] = new Array[String](valuearraylen.length)
          perfvalue_array(i) = session("perfvalue_singlevalue").as[String] 
// ERROR in above logic
 // saving value in updated array index
          i = i + 1. // moving to next array index
          session.set("string_collected_array", perfvalue_array)
        })

//provide the created array value to next api request

.exec(http("Bulk_perftest")
    .delete("/perf/bulk")
    .body(StringBody(session => s"""[ 
    "${session("string_collected_array").as[Vector[String]].mkString("\",\"")}"]"""))
    .check(status.is(204)))

//ERROR cannot pass the value - error no attribute name defined “string_collected_array”

Hi @Monika,

It seems that scala is not your cup of tea. Do you know that you can use Java or Kotlin for creating Gatling simulations?

I’m not sure what you expect to achieve. I will try to rephrase it with my own word:

Creating a scenario (describe 1 user journey), that will loop over an array and save the result to be able to bulk a final request.

My approach will be:

  • create an empty list (or ensure it is empty) in session before the loop.
  • in the loop, prepend to the list (session.set("myList", myNewValue :: session("myList").as[List[String]]))
  • after the loop, creating the wanted String (session("myList").as[List[String]].mkString(", ")) for the execution.

Notes:

  • Are you in the same scenario? (session is not shared between users)
  • Avoid (when possible) global variable
  • Array is not auto-extensible

Does it help?

Cheers!

@sbrevet thank you for helping.
All the existing scripts are done in scala, so emp wants only scala scripting.

can you please provide an example.
I was trying with list but its completely gone mess.

.exec(session => {
  val valuesperf  = List[String]()
  session.set("valuesperf", valuesperf)
})


.foreach("#{ArrayValues}", "VValues") {
  exec(http("perftest values")
    .post("perftest")
    .body(StringBody(session =>
      s"""{
         |"id": "",
         |"perfTypeId": "00334-0000-0000-0345",
         |"legs":[{
         |      "valueTypeId": "111-220-3300-0444",
         |      "values": "${session("VValues").as[String]}"
         |    }]
         |     }""".stripMargin)).asJson
    .check(jsonPath("$.id").saveAs("perfvalue_singlevalue"))
    .check(status.is(201)))
    .exec(session => {
      session.set("valuesperf", valuesperf :: session("valuesperf").as[List[String]])
      session("valuesperf").as[List[String]].mkString(", ")
    }
      session.set("string_collected_array", perfvalue_array)
    })

.exec(http(“Bulk_perftest”)
.delete(“/perf/bulk”)
.body(StringBody(session => s"“”[
“${session(“string_collected_array”).as[Vector[String]].mkString(”","“)}”]“”"))
.check(status.is(204)))

  • I am in same scenario

Please help me.

.exec(_.set("valuesperf", List.empty[String])
.foreach("#{ArrayValues}", "VValues") {
  exec(http("perftest values")
    .post("perftest")
    .body(StringBody(session =>
      s"""{
         |"id": "",
         |"perfTypeId": "00334-0000-0000-0345",
         |"legs":[{
         |      "valueTypeId": "111-220-3300-0444",
         |      "values": "${session("VValues").as[String]}"
         |    }]
         |     }""".stripMargin)).asJson
    .check(jsonPath("$.id").saveAs("perfvalue_singlevalue"))
    .check(status.is(201)))
  .exec(_.set("valuesperf", session("perfvalue_singlevalue").as[String] :: session("valuesperf").as[List[String]])
}).exec(session => {
  println(session("valuesperf").as[List[String]].mkString(", ")) // only to show the resulting value
  session
})
.exec(http("Bulk_perftest")
  .delete("/perf/bulk")
  .body(StringBody(session => s"""["${session("valuesperf").as[List[String]].mkString("\", \"")}"]"""))
  .check(status.is(204)))

Notes:

  • session is immutable, you have to return the updated value
  • preprend the list with the current value
  • use the accumulated list for the final request

Cheers!

1 Like

@sbrevet Thank you
your logic solved my issues.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.