Hello,
This script works fine in the case of only one user,
but in the case of multiple users , the create method in the MonthPay class will throw a bad request exception 400, because we cannot create two MonthPays or more with the same parameter (period parameter in this case 01-09-2024)
public class RHPSimulation extends Simulation {
private HttpProtocolBuilder httpProtocol = http
.baseUrl("https://app-dev.rh-partner.com")
.acceptHeader("application/json")
.contentTypeHeader("application/json")
.acceptEncodingHeader("gzip, deflate, br");
private static Map<CharSequence, String> authorizationHeader = Map.ofEntries(
Map.entry("X-XSRF-TOKEN", "#{xsrfToken}"),
Map.entry("authorization", "Bearer #{jwt}")
);
private static class Hello {
private static ChainBuilder getXSRFToken = exec(
http("Get the xsrf token")
.get("/auth/hello")
.check(status().is(200))
.check(headerRegex("Set-Cookie", "XSRF-TOKEN=([^;]+)")
.exists()
.saveAs("xsrfToken"))
).exec(session -> {
System.out.println("###- XSRF-TOKEN: " + session.getString("xsrfToken"));
return session;
});
}
private static class Authentication {
private static final ChainBuilder authentication =
exec(Hello.getXSRFToken)
.exec(http("authenticate")
.post("/auth/login")
.header("X-XSRF-TOKEN", "#{xsrfToken}")
.body(RawFileBody("gatlingdemostoreapi/rhpsimulation/login.json"))
.check(status().is(200))
.check(jsonPath("$.access_token").exists().saveAs("jwt"))
)
.exec(session -> {
System.out.println("###- JWT: " + session.getString("jwt"));
return session;
});
}
private static class MonthPay {
private static final ChainBuilder create =
exec(Hello.getXSRFToken)
.exec(http("Create month pay")
.post("/pay/api/companies/4056/month-pays?calculatePointing=true")
.headers(authorizationHeader)
.body(RawFileBody("gatlingdemostoreapi/rhpsimulation/month-pay-period.json"))
.check(status().is(201))
.check(jmesPath("id").saveAs("monthPayId"))
.check(jmesPath("status").saveAs("calculationStatus"))
).exec(session -> {
System.out.println("###- Month Pay ID: " + session.getString("monthPayId"));
System.out.println("###- Calculation Status: " + session.getString("calculationStatus"));
return session;
});;
private static final ChainBuilder delete =
exec(Hello.getXSRFToken)
.exec(
http("Delete month pay")
.delete("/pay/api/companies/4056/month-pays/#{monthPayId}?deleteMonthPointing=false")
.headers(authorizationHeader)
.check(status().is(204)));
}
private static class EmployeePay {
private static final ChainBuilder calculate =
exec(Hello.getXSRFToken)
.exec(http("Initiate pay calculation")
.post("/pay/api/companies/4056/month-pays/#{monthPayId}/calculate-pay")
.headers(authorizationHeader)
.check(status().is(200)))
.exec(session -> {
System.out.println("###- Calculation Status: " + session.getString("calculationStatus"));
return session;
})
.asLongAs(session -> !"EVALUATION_SUCCEEDED".equals(session.getString("calculationStatus")))
.on(
pause(Duration.ofMillis(500))
.exec(http("Check calculation status")
.get("/pay/api/companies/4056/month-pays/#{monthPayId}")
.headers(authorizationHeader)
.check(
status().is(200),
jmesPath("status").saveAs("calculationStatus")
)
)
);
}
private ScenarioBuilder scn = scenario("RHPSimulation")
.exec(
exec(Authentication.authentication),
pause(2),
exec(MonthPay.create),
pause(10),
exec(EmployeePay.calculate),
pause(2),
exec(MonthPay.delete)
);
{
setUp(scn.injectOpen(atOnceUsers(1))).protocols(httpProtocol);
}
}
I want to test the load of my app with over 1000 users.
How can I handle this scenario during load testing?
Gatling version: 3.11.5
Gatling flavor: Java
Gatling build tool: Maven