gatlingscala-gatling

How to make Gatling run exec sequently?


I wrote this script in java (but in scala it behave the same, doesn't matter):

scenario("testScenario")
    .exec(Actions.characters)
    .exec(characterById)
    .exec(image)
    .exec(location)
    .exec(random)
    .exec(getResidents)
    .foreach("#{residents}", "resident")
    .on(Actions.getEachResident);

And this Actions:

static class Actions {
    static ChainBuilder characters = exec(
            http("character")
                    .get("/character?page=2")
                    .check(jsonPath("$.results[?(@.name=='" + NAME + "')].id").saveAs("id")));

    static ChainBuilder characterById = exec(http("characterById")
            .get("/character/#{id}")
            .check(jsonPath("$.location.url").saveAs("location"))
            .check(jsonPath("$.image").saveAs("image")));

    static ChainBuilder image = exec(http("image")
            .get("#{image}"));

    static ChainBuilder location = exec(http("location")
            .get("#{location}")
            .check(jsonPath("$.residents[*]").findAll().saveAs("res"))
            .check(jsonPath("$.residents[*]").findRandom().saveAs("randomResident")));

    static ChainBuilder random = exec(http("random")
            .get("#{randomResident}"));

    static ChainBuilder getResidents = exec(session -> {
        List<String> residents = session.getList("res");
        return session.set("residents", residents);
    });

    static final ChainBuilder getEachResident =
            exec(http("Each resident").get("#{resident}"));
}

In debug I see residents list contains 3 residents:

"https://rickandmortyapi.com/api/character/23"
"https://rickandmortyapi.com/api/character/204"
"https://rickandmortyapi.com/api/character/320"

I run this code many times and every time get different result, for example:

1) enter image description here

2) enter image description here

Sometimes script skips 'random' action and make two resident request (but there is 3 residents in list). Sometimes it doesn't skip 'random' action and make only one resident request.

I feel like Gatling make last 2 exec's not sequentially but concurrently. But I thought Chain of exec's must run sequently.

What I'm doing wrong and how to make Gatling run exec's sequently?


Solution

  • Nothing to do with sequentiality. Gatling works fine.

    You don't get the number of requests you're expecting because you didn't realise that the API you're testing implements caching (you can check for yourself the Expires and Cache-Control response headers).

    So when character, random or Each resident pick the same character, the request is skipped.

    You can use disableCaching, but then Gatling won't behave the way the API expects.