Home > Enterprise >  How do I perform a conditional action based on a response in Gatling?
How do I perform a conditional action based on a response in Gatling?

Time:05-04

I have a scenario which load tests a sample API . Now I want to store those failed accounts of the scenario and and put them back in the feeder. I have a global hash table where I want to store the failed accounts.

I am not able to figure out how to do this action using doif , check or any other Gatling DSL.

ScenarioBuilder scenarioBuilder = scenario("getImage Request")
        .repeat(8)
        .on(
        feed(feeder)
        .exec(session -> {
            
            
            String token = dataapi.createStrongToken("abc", "doj");
            Session session1 = session.set("token", token);
            return session1;
        })
        .feed(csv("clientType.csv").random())
        .exec(http("uploadImage").get("/image")
                .check(status().gte(200), status().lte(304))
                .header("token",session -> session.getString("token"))
                .queryParam("accountId", "#{accountId}")
                
        );

You can imagine a custom feeder or a csv as plausable. If I get a response in range of 4xx or 5xx I want to store a the failed account in a global hash table/linked list. Can someone please guide me how to implement this case ? Let me know if more information is needed.

CodePudding user response:

Add a status().saveAs("status") check so you can build your doIf condition:

.doIf(session -> {
  int status = session.getInt("status");
  return status < 200 || status > 304;
}).then(
  exec(
    session -> {
      String accountId = session.getString("accountId");
      // perform accountId storage
      return session;
    }
  )
)
  • Related