In this blog, we’ll explore using Gatling, a powerful load-testing tool, to test a simple Spring Boot application. We'll set up a performance test for a sample REST API endpoint, demonstrate step-by-step how Gatling integrates with the project, and configure a scenario similar to the example discussed earlier.
What is Gatling?
Gatling is a highly performant open-source load-testing tool. It helps simulate high-traffic scenarios for your APIs, ensuring your application can handle the expected (or unexpected) load efficiently.
1. Setting Up the Spring Boot Project
We'll create a Spring Boot REST API with a simple /search
endpoint that accepts query parameters: query
and category
.
@RestController
@RequestMapping("/api")
public class SearchController {
@GetMapping("/search")
public ResponseEntity<String> search(
@RequestParam String query,
@RequestParam String category) {
// Simulate a simple search response
String response = String.format("Searching for '%s' in category '%s'", query, category);
return ResponseEntity.ok(response);
}
}
Step 1.2: Add Dependencies in pom.xml
Make sure your project has the following dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
Start the Spring Boot application and ensure the /api/search
endpoint is reachable, e.g., http://localhost:8080/api/search?query=phone&category=electronics
.
2. Setting Up Gatling
Step 2.1: Add Gatling Plugin to Your Project
If you use Maven, add the Gatling plugin to your pom.xml
for performance testing
<build>
<plugins>
<plugin>
<groupId>io.gatling</groupId>
<artifactId>gatling-maven-plugin</artifactId>
<version>4.0.0</version>
<executions>
<execution>
<id>gatling-test</id>
<phase>integration-test</phase>
<goals>
<goal>execute</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Run the following command to initialize the Gatling directory structure:
mvn gatling:generate
It will create a src/test/scala
folder for your simulation scripts.
3. Writing a Gatling Simulation
Create a file SearchSimulation.scala
under src/test/scala
with the following content:
Step 3.1: Import Gatling Basics
Step 3.2: Create a CSV Feeder
Create a file search_terms_with_categories.csv
under src/test/resources
:
query,category
phone,electronics
laptop,computers
book,education
shoes,footwear
4. Running the Gatling Test
Run the simulation using the following Maven command:
mvn gatling:test
target/gatling
folder. Open the report to see performance metrics like response time, throughput, and error rates.5. Key Concepts Explained
Scenario: The
scenario
in Gatling defines user behavior (e.g., feeding data, sending HTTP requests).Feeder: Feeds dynamic data (from a CSV, JSON, or database) to requests. In this case,
search_terms_with_categories.csv
feeds values forquery
andcategory
.Load Simulation: The
setUp
block determines how many users execute the scenario and at what rate (e.g., 100 users ramping up over 30 seconds).HTTP Protocol: Defined globally using
http.baseUrl()
for all requests. You can also customize headers, timeouts, etc.
Comments
Post a Comment