Performance Testing with Gatling
In this guide, we will cover how to perform a performance test on the Swagger Petstore API using Gatling. Gatling is a user-friendly, Java-Scala based load testing tool that allows you to characterize user behavior and simulate thousands of concurrent users accessing your application.
To start using Gatling, you need to set up a suitable development environment.Let’s set the pom.xml
Using Maven:
<dependencies>
<dependency>
<groupId>io.gatling.highcharts</groupId>
<artifactId>gatling-charts-highcharts</artifactId>
<version>${gatling.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
2. Creating a Test Scenario
A performance test scenario defines the actions to be tested and how they will be executed. Below is an example of a test scenario for five different endpoints of the Swagger Petstore API(https://petstore.swagger.io/):
import static io.gatling.javaapi.core.CoreDsl.*;
import static io.gatling.javaapi.http.HttpDsl.*;
import io.gatling.javaapi.core.*;
import io.gatling.javaapi.http.*;
public class SwaggerSimulation extends Simulation {
FeederBuilder<String> feeder = csv("pet_ids.csv").random();
ChainBuilder getPetById =
feed(feeder)
.exec(
http("Get Pet by ID")
.get("/pet/#{petId}")
.check(status().is(200))
)
.pause(1);
ChainBuilder findPetsByStatus =
exec(
http("Find Pets by Status")
.get("/pet/findByStatus?status=available")
.check(status().is(200))
)
.pause(1);
ChainBuilder addPet =
exec(
http("Add New Pet")
.post("/pet")
.body(RawFileBody("addPet.json")).asJson()
.check(status().is(200))
)
.pause(1);
ChainBuilder updatePet =
exec(
http("Update Pet")
.put("/pet")
.body(RawFileBody("updatePet.json")).asJson()
.check(status().is(200))
)
.pause(1);
ChainBuilder deletePet =
exec(
http("Delete Pet")
.delete("/pet/#{petId}")
.check(status().is(200))
)
.pause(1);
HttpProtocolBuilder httpProtocol = http
.baseUrl("https://petstore.swagger.io/v2")
.acceptHeader("application/json")
.userAgentHeader("Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
ScenarioBuilder scn = scenario("Swagger API Test")
.exec(getPetById, findPetsByStatus, addPet, updatePet, deletePet);
{
setUp(
scn.injectOpen(rampUsers(10).during(10))
).protocols(httpProtocol);
}
}
3. Preparing Test Data
In Gatling tests, the data used in scenarios can be stored in CSV files or JSON format. For example, the pet_ids.csv
file could contain the IDs of pets to be used during the test.
4. Running the Test
To run your Gatling tests, you can use the following command:
mvn gatling:test
This command runs the Gatling test using Maven and stores the results in the target/gatling
directory.
5. Analyzing Test Results
After the tests are completed, Gatling generates a detailed HTML report. This report provides insights into the performance of the endpoints tested and includes:
- Response Time Distribution: Shows the average, minimum, and maximum response times for requests.
- Request Failure Rates: Displays the percentage of failed requests during the test.
- Average Response Time Per User: Indicates the average response time for each user.
6. Advanced Usage and Optimization
To create more complex scenarios, you can leverage Gatling’s advanced features:
- Dynamic Data Usage: Use data that varies between users to create more realistic test scenarios.
- Conditional Requests and Loops: Ensure that requests are made under specific conditions or are repeated a certain number of times.
Github Link: https://github.com/dogangunemre/GatlingPerformanceTesting
Thanks!