使用 gatling 将变量从 json 保存并获取到另一个场景

save and get variable from json to another scenario using gatling

我在 scala 中使用 gatling。我想检查 amount + 100。如何从 saveAs("amount") 中获取金额并放入 amount + 100

  private val getBeforeStatus = scenario("getBeforeStatus")
    .exec(http("GET method")
      .get("/get")
      .check(jsonPath("$.amount").ofType[Int].saveAs("amount")))

  private val post = scenario("post")
    .exec(http("POST method")
      .post("/post")
      .body(StringBody("{\"count\": 3}"))
      .check(status.is(200)))
    .exec(http("DELETE method")
      .delete("/delete/1")
      .check(status.is(200)))

  private val getAfterStatus = scenario("getAfterStatus")
    .exec(http("GET method")
      .get("/get")
      .check(jsonPath("$.amount").ofType[Int].is(amount + 100)))

  setUp(
    getBeforeStatus.inject(atOnceUsers(1)),
    post.inject(atOnceUsers(100)),
    getAfterStatus.inject(atOnceUsers(1)))
    .protocols(httpConf)

将金额值保存为模拟中的变量:

var amount: Int
scenario("getBeforeStatus")
    .exec(http("GET method")
      .get("/get")
      .check(jsonPath("$.amount").ofType[Int].saveAs("amount")))
    .exec{ session =>
       amount = session("amount").as[Int]
       session
    }

然后,您需要"wait"直到完成 100 个请求。临时解决方案是:

setUp(
    getBeforeStatus.inject(atOnceUsers(1)),
    post.inject(atOnceUsers(100)),
    getAfterStatus.inject(
      nothingFor(10 seconds), // enough time to end previous 100 requests
      atOnceUsers(1))
    )
    .protocols(httpConf)