...
/Example 2 - Integration Load Test Simulation
Example 2 - Integration Load Test Simulation
Study an example scenario containing multiple APIs that form an end-to-end integration test for load testing.
What is integration testing?
This is a testing technique where individual components are tested in a sequence or as a group.
In a complex software system, there are multiple microservices interacting with each other. Each of them is developed to solve a specific use case and allows for multiple interfaces to interact with each other.
As part of the load testing, the entire sequence of APIs will be called in the same sequence for users at the given load configuration.
Sample simulation
In this scenario, we are simulating and automating the integration flow for a single service.
Steps involved
The integration flow contains the following steps:
import io.gatling.core.Predef._import io.gatling.http.Predef._class SampleSimulation extends Simulation {val protocols = http.disableCaching // sending no-cache header.disableWarmUp // disable initial warm up.baseUrl("http://localhost:8080") // setting the global base url.contentTypeHeader("application/json") // setting the global content-type header.acceptHeader("application/json") // setting the global accept headerval scn = scenario("sample scenario").exec(http("fetch all the users").get("/api/users") // sending GET request.check(status.is(200), // checking whether the http status code is 200jsonPath("$.data[*].id").exists, // extracting the id from response bodyjsonPath("$.data[*].id").findRandom.saveAs("randomId") // fetching random id and saving to session in key 'randomId')).exitHereIfFailed // to stop further execution if any failure occurred in the previous exec.exec(http("get info for user id ${randomId}") // fetch the 'randomId' from user session.get("/api/users/${randomId}") // fetch the user information.check(status.is(200), // checking whether the http status code is 200status.not(400), // checking whether the http status code is not 400status.in(200, 201), // checking whether the http status code is in (200, 201)jsonPath("$.data.id").ofType[Int].is(session => session("randomId").as[Int]),jsonPath("$.data.first_name").transform(str => str.trim).saveAs("first_name"), // extract 'first_name' from response, transform the value by trimming and save it to session in key 'first_name'jsonPath("$.data.last_name").transform((str, session) => str.trim + " " + session("first_name").as[String]).saveAs("full_name"))).exitHereIfFailed // to stop further execution if any failure occurred in the previous exec.exec(http("update user id ${randomId}").put("/api/users/${randomId}") // sending PUT request.body(StringBody("""{ "name": "gatling", "job": "gatling" }""")).asJson // setting both content-type and accept to 'application/json' overridding the global setting.check(status.is(200), // checking whether the http status code is 200jsonPath("$.name").is("gatling"), // checking whether the extracted 'name' field from response body is 'gatling'jsonPath("$.job").is("gatling"), // checking whether the extracted 'job' field from response body is 'gatling'jsonPath("$.updatedAt").exists // checking whether the extracted 'updatedAt' field exists)).exitHereIfFailed // to stop further execution if any failure occurred in the previous exec.exec(http("delete user ${randomId}").delete("/api/users/${randomId}") // sending DELETE request.check(status.is(204) // checking whether the http status code is 204))// running the entire sequence of APIs 1 timesetUp(scn.inject(atOnceUsers(1))).protocols(protocols)}
Understanding the code
-
Fetching all the users
The below
exec
makes a GET request and fetches the information of the random user id that was fetched from the previous step. After fetching:exec(http("fetch all the users") .get("/api/users")