Search code examples
scalavectorgatling

How to use saved variable values outside of gatling scenario in scala file


One of gatling get request is giving multiple string values and I am saving them using saveAs like this:

val scn = scenario("ReadLogs")
        .exec(http("logEvent")
        .get("""/xyz/abc""")
        .check(jsonPath("$.data[*].message").findAll.saveAs("mList")))

/* My scala code to achieve some requirements*/

I can see in log that "mList" is a vector which have my required string messages. I want to process those messages in my scala code. How to do that in simple way? I think if i can use "mList" variable outside scn scenario than things will move fine so i'll put this question more specific. How can i use "mList" variable in my scala code?


Solution

  • Coding the process logic in a separate execution step, and make sure it is executed after the data is fetched.

    val fetchLogs = exec(
      http("logEvent")
        .get("""/xyz/abc""")
        .check(jsonPath("$.data[*].message")
        .findAll
        .saveAs("mList")
    )
    
    val processLogs = exec { s: Session =>
        val mList = s("mList").as[Seq[Any]]
        val result = ...
        s.set("processResult", result)
    }
    
    val scn = scenario("ReadLogs").exec(
      fetchLogs,
      processLogs
    )
    

    Update: Save the data for later process

    var mList: Seq[String] = _    
    
    val fetchLogs = exec(
      http("logEvent")
        .get("""/xyz/abc""")
        .check(jsonPath("$.data[*].message")
        .findAll
        .transform { v => mList = v; v } // save the data
        .saveAs("mList")
    )
    
    val scn = scenario("ReadLogs").exec(fetchLogs)
    
    after {
      // Process the data here. It will be executed when the simulation is finished.
    }