retrieve a value from json response and add it to the list, iterate through list

I am new to Gatling, I am trying to do the performance testing for couple of rest calls. In my scenario I need to extract a value from json response of the 1st call and add those values to the list after looping for few times. Again after looping for few times and adding the values into the list, I want to reuse each value in my next rest call by iterating over the values in the list. Can anyone please suggest on how to implement this. I tried something as below,

var datasetIdList = List.empty[String] val datasetidsFeeder = datasetIdList.map(datasetId => Map(“datasetId” → datasetId)).iterator def createData() = { repeat(20){ feed("").exec(http(“create dataset”).post("/create/data").header(“content-type”, “application/json”) .body(StringBody("""{“name”:“name”}""")) .asJson.check(jsonPath("$.id").saveAs(“userId”)))).exec(session => { var usrid = session(“userId”).as[String].trim datasetIdList:+= usrid session}) def upload()= feed(datasetidsFeeder).exec(http(“file upload”).post("/compute-metaservice/datasets/${datasetId}/uploadFile") .formUpload(“File”,"./src/test/resources/data/File.csv") .header(“content-type”,“multipart/form-data”) .check(status is 200)) } val scn = scenario(“create data and upload”).exec(createData()).exec(upload()) setUp(scn.inject(atOnceUsers(1))).protocols(httpConf) }

I am seeing an exception that ListFeeder is empty when trying to run above script. Can someone please help

Hi!

Welcome to gatling community!

I cannot read your code. Even my attempt to clean it resolve in a feed("") that cannot be resolved.
Please, provide a SSCCE as requested in this group.

About the general subject, my opinion would be about to have a session variable to keep track of the values and use a foreach to loop onto the values.

For further help, please provide a well-formatted and complete code.

Cheers!

Thanks Sebastian, when I tried to use session I am able to save the list, But it is working only within that scenario. Is there a way that I can use it across multilple scenarios? because I am trying to execute 2 scenarios parallely . Please find below my formatted code.

class ParallelcallsSimulation extends Simulation{

var idNumbers = (1 to 50).iterator

val customFeeder = Iterator.continually(Map(
“name” → (“test_gatling_”+ idNumbers.next())
))

val httpConf = http.baseUrl(“http://localhost:8080”)
.header(“Authorization”,“Bearer 6a4aee03-9172-4e31-a784-39dea65e9063”)

def createDatasetsAndUpload() = {
repeat(3) {
//create dataset
feed(customFeeder).exec(http(“create data”).post("/create/data").header(“content-type”, “application/json”)
.body(StringBody("""{ “name”: “${name}”,“description”: “create data and upload file”}"""))
.asJson.check(jsonPath("$.id").saveAs(“userId”)))
.exec(session => {
val name = session(“name”).asOption[String]
println(name.getOrElse(“COULD NOT FIND NAME”))
val userId = session(“userId”).as[String].trim
println("%%%%% User ID ====>"+userId)
val datasetIdList = session(“datasetIdList”).asOption[List[_]].getOrElse(Nil)
session.set(“datasetIdList”, userId :: datasetIdList)
})
}
}
// File Upload
def fileUpload() = foreach("${datasetIdList}",“datasetId”){
exec(http(“file upload”).post("/uploadFile")
.formUpload(“File”,"./src/test/resources/data/File.csv")
.header(“content-type”,“multipart/form-data”)
.check(status is 200))
}
//Get Dataset
def getDataSetId() = foreach("${datasetIdList}",“datasetId”){
exec(http(“get datasetId”)
.get("/get/data/${datasetId}")
.header(“content-type”,“application/json”)
.asJson.check(jsonPath("$.dlp.dlp_job_status").optional
.saveAs(“dlpJobStatus”)).check(status is 200)
).exec(session => {
val datastId = session(“datasetId”).asOption[String]
println(“request for datasetId >>>>>>>>”+datastId.getOrElse(“datasetId not found”))
val jobStatus = session(“dlpJobStatus”).asOption[String]
println(“JOB STATUS:::>>>>>>>>>>”+jobStatus.getOrElse(“Dlp Job Status not Found”))
println(“Time: >>>>>>”+System.currentTimeMillis())
session
}).pause(10)
}

val scn1 = scenario(“create multiple datasets and upload”).exec(createDatasetsAndUpload()).exec(fileUpload())
val scn2 = scenario(“get datasetId”).pause(100).exec(getDataSetId())

setUp(scn1.inject(atOnceUsers(1)),scn2.inject(atOnceUsers(1))).protocols(httpConf)
}

Can you please check and help.

Thanks,
Hareesh

Hi,

I’m not sure why you would like to share data between scenarios.

A scenario is like a script of a movie actor for one virtual user.
Does this information are shared by other means for your real users?

Only in that case, you may consider using a shared variable.

Example with a blocking queue of integers:

import io.gatling.core.Predef._
import io.gatling.http.Predef._

import java.util.concurrent.ArrayBlockingQueue
import scala.concurrent.duration._
import scala.util.Random

class BasicSimulation extends Simulation {

  val sharedInformation = new ArrayBlockingQueue[Int](30)

  val httpConf = http.baseUrl("[http://localhost:8080](http://localhost:8080)")

  val scnCreateData = scenario("Create data")
    .repeat(30) {
      exec { session =>
        val createdValue = Random.nextInt(100)
        println(s"Creating a data :$createdValue")
        sharedInformation.add(createdValue)
        session
      }
    }

  val scnConsumingData = scenario("Consuming data")
    .exec { session =>
      println("Waiting for data")
      val consumedData = sharedInformation.take()
      println(s"Consume data: $consumedData")
      session
    }

  setUp(scnCreateData.inject(atOnceUsers(1)), // Create data for everyone (an admin?)
    scnConsumingData.inject(
      nothingFor(4.seconds), // Wait for data to be created
      constantUsersPerSec(2).during(15.seconds) // Real users that will consume data
    )
  )
}

Hoping that helps!
Cheers!