Asynchronous execution wait

I am conducting a stress test for REST API service.
In one scenario I have to extract response parameter , save the value and use in the next scenario to execute different api call. However the first request is kind of async so meaning I have to wait until state is processed as done before I can extract the required value for the next call. How can I achieve wait in Gatling probably by polling until I get desired value in first api call response?

Hi @Pof,

I’m not sure to understand what you mean. I will try to use my own words:

You have a first request (let it be requestA) that may or may not answer with a valid response.
A second request (let it be requestB) need query parameter that you extracted from requestA

So, either pause before calling requestA enough to be sure to have the wanted answer, or you may loop with doWhile for instance until you got your parameter to be able to call your requestB.

Something like:

    ScenarioBuilder scn = scenario("user")
        .doWhile("#{myParam.isUndefined()}").on(
            exec(
                http("requestA")
                    .get("/api/request/a")
                    .check(
                        status().is(200),
                        jmesPath("answer.result").ofString().saveAs("myParam")
                    )
            )
        )
        .exec(http("requestB")
            .get("/api/request/b")
            .queryParam("foo", "#{myParam}")
        );

Does that help?

Cheers!

Yeah similar to this script Gatling (Java). Basically the parameter I desire from the request A
is not defined until “state” is set to “completed” .Below is example of json response

first loop
{ “state”: “inProgress”}

final response structure
{
“state”: “completed”,
“gameId”: “j4464dfewfw”
}
Desired param is “gameId”. So I want a way to await until state is completed and then only then I can extract the gameId for request B. How do I approach this ?

In my sample, if myParam is not present in requestA answer, the request will be marked as failed.
but marked as failed does not stop the scenario (you have exitHereIfFailed for that)

So, myParam is still undefined and the doWhile continue.
You may want to add a pause in your loop, though.

In the case of request marked as failed when param is not present, will this not impact overall simulation test report as it will Gatling could report it as failed transaction. Thanks for sharing idea .It ll be helpful.

If you want to avoid request marked as failed, you can save the whole answer. and then use your own parser to extract wanted data and save it into session

Can you show simple example of what you mean . Do it mean after saving the whole response ,I extract the “gameId” into session?. What then is checked in the dowhile block (whole response data or just expected parameter)

Something like that:

    ScenarioBuilder scn = scenario("user")
        .doWhile("#{myParam.isUndefined()}").on(
            exec(
                http("requestA")
                    .get("/api/request/a")
                    .check(
                        status().is(200),
                        bodyString().saveAs("body")
                    )
            ).exec(session -> {
              try {
                String myParam = jacksonObjectMapper.readTree(session.getString("body")).get("myParam").textValue();
                return session.set("myParam", myParam);
              } catch (Exception e) {
                // Value not found
                return session;
              }
            })
        )
        .exec(http("requestB")
            .get("/api/request/b")
            .queryParam("foo", "#{myParam}")
        );

I used Jackson, but you basically can use any Json parser.

Is it more clear?

Cheers!

Yeah thanks very much.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.