Gatling Performance Testing
Gatling is a powerful performance testing tool designed for high-performance load testing of web applications and APIs.
π― Why Gatling?β
- High Performance - Can simulate thousands of users
- Scala-based - But easy to use without Scala knowledge
- Real-time Monitoring - Live metrics during test execution
- Beautiful Reports - Rich HTML reports with charts
- CI/CD Integration - Easy integration with build pipelines
π¦ Installationβ
Option 1: Standalone Bundleβ
Download from Gatling Downloads
Option 2: Maven Integrationβ
Add to your pom.xml:
<plugin>
<groupId>io.gatling</groupId>
<artifactId>gatling-maven-plugin</artifactId>
<version>4.3.7</version>
</plugin>
π Your First Gatling Testβ
Create BasicSimulation.scala:
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
class BasicSimulation extends Simulation {
val httpProtocol = http
.baseUrl("https://jsonplaceholder.typicode.com")
.acceptHeader("application/json")
val scn = scenario("Basic API Test")
.exec(
http("Get Users")
.get("/users")
.check(status.is(200))
)
.pause(1)
.exec(
http("Get Posts")
.get("/posts")
.check(status.is(200))
.check(jsonPath("$[0].title").exists)
)
setUp(
scn.inject(
atOnceUsers(10), // 10 users at once
rampUsers(50) during (30.seconds) // 50 users over 30 seconds
)
).protocols(httpProtocol)
}
π§ Advanced Featuresβ
Load Patternsβ
setUp(
scn.inject(
nothingFor(4.seconds), // Wait 4 seconds
atOnceUsers(10), // 10 users immediately
rampUsers(50) during (30.seconds), // Ramp up 50 users over 30s
constantUsersPerSec(20) during (60.seconds), // Constant rate
heavisideUsers(1000) during (20.seconds) // Gradual ramp
)
)
Data Feedersβ
val userFeeder = csv("users.csv").random
val scn = scenario("User Creation")
.feed(userFeeder)
.exec(
http("Create User")
.post("/users")
.body(StringBody("""{"name": "${name}", "email": "${email}"}"""))
.header("Content-Type", "application/json")
)
Assertionsβ
setUp(scn.inject(rampUsers(100) during (60.seconds)))
.protocols(httpProtocol)
.assertions(
global.responseTime.max.lt(2000), // Max response time < 2s
global.responseTime.mean.lt(500), // Mean response time < 500ms
global.successfulRequests.percent.gt(95) // Success rate > 95%
)
π Running Testsβ
Command Lineβ
# Run all simulations
./bin/gatling.sh
# Run specific simulation
./bin/gatling.sh -s com.example.BasicSimulation
Mavenβ
mvn gatling:test
π Reports & Analysisβ
Gatling generates detailed HTML reports including:
- Timeline - Request distribution over time
- Response Time Distribution - Percentiles and histogram
- Active Users - Concurrent user count
- Requests per Second - Throughput metrics
Reports location: results/simulation-timestamp/index.html
π Monitoring & Debuggingβ
Real-time Monitoringβ
// Add during simulation
.exec { session =>
println(s"User ${session.userId}: Current response time trend")
session
}
Detailed Loggingβ
val httpProtocol = http
.baseUrl("https://api.example.com")
.header("Accept", "application/json")
.logLevel(Trace) // Enable detailed logging
π Best Practicesβ
- Start Small - Begin with few users, gradually increase
- Monitor Resources - Watch CPU, memory on test machine
- Realistic Data - Use production-like test data
- Think Time - Add pauses to simulate real user behavior
- Environment Isolation - Use dedicated test environment
π Next Stepsβ
- Karate + Gatling Integration (coming soon)
- API Security Testing (coming soon)
- CI/CD Performance Testing (coming soon)
Load test like a pro! π