Time to put it all together. Write a Gatling simulation that tests the Shopping Portal. This exercise covers every concept from this chapter -- protocol configuration, scenarios, feeders, checks, injection profiles, and assertions.
Create a Maven project with the Gatling dependency (copy the pom.xml from Lesson 2)
Define the HTTP protocol: base URL pointing to the Shopping Portal, JSON accept header, a custom user-agent header
Create a CSV feeder with 10 test users (email, password, name)
Build a "Browse and Buy" scenario: login with feeder data, pause 2-4s, browse product list, pause 1-3s, view a specific product, pause 1-2s, add product to cart
Add checks: status 200 on all requests, extract auth token from login, verify product list returns JSON array
Configure injection: ramp 50 users over 30 seconds, then sustain 5 users/sec for 2 minutes
Add assertions: p95 response time under 3 seconds, success rate above 99%, all individual requests under 5 seconds p99
Run the test with "mvn gatling:test" and analyze the HTML report
package com.example;
import io.gatling.javaapi.core.*;
import io.gatling.javaapi.http.*;
import java.time.Duration;
import static io.gatling.javaapi.core.CoreDsl.*;
import static io.gatling.javaapi.http.HttpDsl.*;
public class ShoppingPortalSimulation extends Simulation {
// Protocol
HttpProtocolBuilder httpProtocol = http
.baseUrl("https://www.testerrank.com")
.acceptHeader("text/html,application/json")
.userAgentHeader("Gatling/ShoppingTest");
// Feeder
FeederBuilder<String> userFeeder = csv("data/users.csv").random();
// Reusable chains
ChainBuilder login = feed(userFeeder)
.exec(
http("Login")
.post("/api/login")
.body(StringBody(
"{\"email\":\"#{email}\",\"password\":\"#{password}\"}"
))
.asJson()
.check(
status().is(200),
jsonPath("$.token").saveAs("authToken")
)
);
ChainBuilder browse = exec(
http("Product List")
.get("/shopping")
.check(status().is(200))
).pause(Duration.ofSeconds(1), Duration.ofSeconds(3))
.exec(
http("Product Detail")
.get("/shopping/products/1")
.check(status().is(200))
);
ChainBuilder addToCart = exec(
http("Add to Cart")
.post("/api/cart")
.header("Authorization", "Bearer #{authToken}")
.body(StringBody("{\"productId\":1,\"qty\":1}"))
.asJson()
.check(status().in(200, 201))
);
// Scenario
ScenarioBuilder browseAndBuy = scenario("Browse and Buy")
.exec(login)
.pause(Duration.ofSeconds(2), Duration.ofSeconds(4))
.exec(browse)
.pause(Duration.ofSeconds(1), Duration.ofSeconds(2))
.exec(addToCart);
// setUp
{
setUp(
browseAndBuy.injectOpen(
rampUsers(50).during(Duration.ofSeconds(30)),
constantUsersPerSec(5).during(Duration.ofMinutes(2))
)
).protocols(httpProtocol)
.assertions(
global().responseTime().percentile(95.0).lt(3000),
global().responseTime().percentile(99.0).lt(5000),
global().successfulRequests().percent().gt(99.0)
);
}
}Q: You have experience with both JMeter and Gatling. If you were setting up performance testing for a new microservices project, which would you choose and why?
A: For a new microservices project, I would choose Gatling. Three reasons: First, microservices teams are typically developer-heavy, and Gatling's code-based approach fits their workflow -- tests are written in Java, reviewed in PRs, and version-controlled alongside application code. Second, microservices rely heavily on CI/CD pipelines, and Gatling integrates natively via the Maven plugin with assertion-based pass/fail. Third, Gatling's async architecture is more efficient -- a single machine can simulate 10,000+ users, which matters when you are testing multiple services independently. I would only choose JMeter if the team had existing JMeter expertise, needed JDBC or JMS protocol testing, or included non-developer testers who benefit from the GUI. The performance testing concepts -- virtual users, ramp-up, assertions, correlation -- are the same in both tools.
Key Point: Gatling and JMeter are complementary tools. Master the concepts (virtual users, injection, checks, assertions) and switching between tools is just a syntax change. For modern CI/CD-driven teams, Gatling is the natural fit.
Key Point: Master Gatling concepts: simulation structure, feeders, checks, assertions, injection profiles, and CI/CD integration
Answer all 8 questions, then submit to see your score.
1. What is the main architectural difference between Gatling and JMeter?
2. What are the three core parts of a Gatling simulation?
3. Which feeder strategy should you use when you have 50 CSV records but need to simulate 500 virtual users?
4. What is the difference between checks and assertions in Gatling?
5. Why should you use the open injection model (injectOpen) for web traffic testing?
6. What should you always do after recording a simulation with the Gatling Recorder?
7. How does Gatling integrate with CI/CD pipelines?
8. In a Gatling report, which metric should you check first?