GUIDES
Load Testing with Gatling and Scala
A Gatling load testing example in Scala with multiple ways to run simulations against an HTTP target.
Features
- Simulation written in Scala using the Gatling DSL
- Configurable via system properties (URL, duration, rate, thresholds)
- Four execution modes: Maven plugin, executable jar, Docker, Kubernetes Job
- Bundled Node.js HTTP test server as a minimal system under test
- Helm chart for Kubernetes deployment
- GitHub template — clone from a clean slate with one click
Getting Started
Requirements
- JDK 21
- Node.js (for the bundled test server)
- Maven
- Docker (optional)
Start the Test App
The included test server is a minimal Node.js HTTP server that echoes requests back.
cd example-app
node http-server.js 8080
The Simulation
ExampleSimulation.scala sends POST requests at a constant rate and asserts response time and success thresholds:
class ExampleSimulation extends Simulation {
val httpConf = http.baseURL(baseUrl)
val rootEndPointUsers = scenario("Root end point calls")
.exec(http("root end point")
.post("/")
.header("Content-Type", "application/json")
.body(StringBody("{}"))
.check(status.is(200))
)
setUp(rootEndPointUsers.inject(
constantUsersPerSec(PerfTestConfig.requestPerSecond) during (durationMin minutes))
.protocols(httpConf))
.assertions(
global.responseTime.max.lt(meanResponseTimeMs),
global.responseTime.mean.lt(maxResponseTimeMs),
global.successfulRequests.percent.gt(95)
)
}
Configuration
PerfTestConfig.scala reads values from system properties with sensible defaults:
| Property | Default | Description |
|---|---|---|
baseUrl | http://localhost:8080 | Target URL |
requestPerSecond | 10 | Load rate |
durationMin | 1 | Test duration in minutes |
meanResponseTimeMs | 500 | Mean response time threshold (ms) |
maxResponseTimeMs | 1000 | Max response time threshold (ms) |
Running the Tests
Maven Plugin
mvn test -Pperf-test
Run a specific simulation:
mvn test -Pperf-test -DsimulationClass=gatling.test.example.simulation.ExampleGetSimulation
Executable Jar
Packages the Gatling runtime and simulations into a single self-contained jar:
mvn clean install
java -cp target/gatling-scala-example.jar io.gatling.app.Gatling \
-s gatling.test.example.simulation.ExampleSimulation
Docker
Build the image:
mvn clean package
docker build -t gatling-scala-example .
Run a simulation:
docker run \
-e "JAVA_OPTS=-DbaseUrl=http://localhost:8080" \
-e SIMULATION_NAME=gatling.test.example.simulation.ExampleGetSimulation \
gatling-scala-example:latest
Kubernetes Job
Runs the simulation as a Kubernetes Job via Helm. Requires kubectl and Helm configured against a cluster.
./run-simulation-using-kubernetes.sh
Configurable via environment variables:
| Variable | Default | Description |
|---|---|---|
SIMULATION_NAME | ExampleGetSimulation | Fully qualified simulation class |
BASE_URL | http://localhost:8080 | Target URL |
DURATION_MIN | 0.25 | Test duration in minutes |
REQUEST_PER_SECOND | 10 | Load rate |
P95_RESPONSE_TIME_MS | 250 | 95th percentile threshold (ms) |
NAMESPACE | default | Kubernetes namespace |
TIMEOUT | 300s | Job completion timeout |
Example:
BASE_URL=http://my-service:8080 \
SIMULATION_NAME=gatling.test.example.simulation.ExamplePostSimulation \
REQUEST_PER_SECOND=50 \
./run-simulation-using-kubernetes.sh
The script installs a Helm release with a unique timestamped name, waits for the job to complete, prints simulation logs, and uninstalls the release automatically.
Related DSL Examples
Source
Full source code available on GitHub: jecklgamis/gatling-scala-example