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

自古美人都是妖i 提交于 2019-12-10 17:18:42

问题


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?


回答1:


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.
}


来源:https://stackoverflow.com/questions/32250233/how-to-use-saved-variable-values-outside-of-gatling-scenario-in-scala-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!