加特林如何为以后的请求存储和加载值

Gatling how to store and load a value for a later request

我想构建一个负载测试,其中第二个请求来自第一个响应。数据提取是在一个方法中完成的,因为它不仅仅是一行代码。我的问题是存储值 (id) 并在以后加载它。值应该如何存储和加载?我尝试了一些不同的方法,然后想出了这段代码。文档对我没有帮助。

    object First {
        val first = {
            exec(http("first request")
            .post("/graphql")
            .headers(headers_0)
            .body(RawFileBody("computerdatabase/recordedsimulation/first.json"))
            .check(bodyString.saveAs("bodyResponse"))
            )
            .exec {
              session =>
                    val response = session("bodyResponse").as[String]
                    session.set("Id", getRandomValueForKey("id", response))
                session}
            .pause(1)
        }
    }

    object Second {
        val second = {
            exec(http("Second ${Id}")
                .post("/graphql")
                .headers(headers_0)
                .body(RawFileBody("computerdatabase/recordedsimulation/second.json"))
            )
            .pause(1)
        }
    }

    val user = scenario("User")
        .exec(
            First.first,
            Second.second
        )

    setUp(user.inject(
        atOnceUsers(1),
    )).protocols(httpProtocol)

您的问题是您没有正确使用 Session。

来自documentation

Warning

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.

这正是您正在做的事情:

exec { session =>
  val response = session("bodyResponse").as[String]
  session.set("Id", getRandomValueForKey("id", response))
  session
}

应该是:

exec { session =>
  val response = session("bodyResponse").as[String]
  session.set("Id", getRandomValueForKey("id", response))
}