帮我理解 session api Gatling

Helping me understand session api Gatling

我是加特林新手 我正在尝试循环 json 响应,找到我正在寻找的国家代码,并获取与该国家代码对应的 ID。 示例算法: list.foreach( value => { if (value.coutrycode == "PL") 然后存储 value.id })

加特林机:

def getOffer() = {
          exec(
                http("GET /offer")
                  .get("/offer")
                  .check(status.is(Constant.httpOk))
                  .check((bodyString.exists),
                    jsonPath("$[*]").ofType[Map[String,Any]].findAll.saveAs("offerList")))
                      .foreach("${offerList}", "item"){
                        exec(session => {
                          val itemMap = session("item").as[Map[String,Any]]
                          val countryCodeId = itemMap("countryCode")
                          println("****" + countryCodeId)
                          // => print all the country code on the list 
                          if (countryCodeId =="PL"){ // if statement condition
                             println("*************"+ itemMap("offerd")); // print the id eg : "23"
                             session.set("offerId", itemMap("offerId")); // set the id on the session
                          }
                          println("$$$$$$$$$$$$$$" + session("offerId")) // verify that th session contains the offerId but is not 
                          session
                        })
                      }
        }

当我尝试打印会话(“offerId”)时,打印的是“item”而不是 offerId。 我查看了文档,但不理解这种行为。你能给我解释一下吗?

都在documentation.

Session instances are immutable!

Why is that so? Because Sessions are messages that are dealt with in a multi-threaded concurrent way, so immutability is the best way to deal with state without relying on synchronization and blocking.

A very common pitfall is to forget that set and setAll actually return new instances.

val session: Session = ???

// wrong usage
session.set("foo", "FOO") // wrong: the result of this set call is just discarded
session.set("bar", "BAR")

// proper usage
session.set("foo", "FOO").set("bar", "BAR")

所以你想要的是:

val newSession =
  if (countryCodeId =="PL"){ // if statement condition
    println("*************"+ itemMap("offerd")); // print the id eg : "23"
    session.set("offerId", itemMap("offerId")); // set the id on the session
  } else {
    session
  }
// verify that the session contains the offerId
println("$$$$$$$$$$$$$$" + newSession("offerId").as[String]) 
newSession