Search code examples
scalagatling

save and get variable from json to another scenario using gatling


I use gatling with scala. And I want to check amount + 100. How can I get amount from saveAs("amount") and put into 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)

Solution

  • Save the amount value as a variable in the Simulation:

    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
        }
    

    Then, you'll need to "wait" until the 100 requests were completed. An ad-hoc solution would be:

    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)