Simulation Lifecycle¶
When you execute mvn gatling:test, Gatling orchestrates six deterministic lifecycle phases—including optional before() and after() hooks that spin up or tear down infrastructure such as Testcontainers Kafka brokers.
flowchart LR
P1["1. Class Init<br/>Constructor & DSL AST"] --> P2["2. before() Hook<br/>Start EmbeddedKafka / Auth"]
P2 --> P3["3. Engine Warm-Up<br/>DNS, TCP & TLS init"]
P3 --> P4["4. Injection & Execution<br/>Spawn VUs & record histograms"]
P4 --> P5["5. Drain / Cool-Down<br/>In-flight VUs finish"]
P5 --> P6["6. after() & Report<br/>Close clients • Evaluate SLAs"]
Phase-by-Phase Execution¶
| Phase | Timing | What Happens Inside Gatling |
|---|---|---|
| 1. Instantiation | < 1s |
JVM loads your Simulation subclass, executes its initializer block { setUp(...); }, builds immutable Scenario ASTs, and opens feeders. Note: Scenario lambdas do not run yet—only the builder graph is constructed! |
2. before() Hook |
1–15s |
Executes public void before() once before any virtual user spawns. Ideal for starting EmbeddedKafka, seeding test data, or fetching a shared OAuth client token. |
| 3. Warm-Up | 1–3s |
Sends a single synthetic request to .warmUp(url) so Netty classloading, SSLContext initialization, and DNS resolution don't pollute your first user's latency! |
| 4. Injection & Run | Configured duration | Spawns virtual users per injectOpen / injectClosed, streams console progress every 5 seconds, and appends binary telemetry to simulation.log. |
5. Drain (or maxDuration Cut) |
Variable | Injection stops; already-running virtual users finish their remaining steps unless capped by .maxDuration(...). |
6. after() & Reporting |
1–5s |
Runs public void after() (flushing/closing KafkaProducer), parses simulation.log into Highcharts HTML reports, and evaluates .assertions(...) to set Maven's exit code. |
Lifecycle Hooks in Practice (before & after)¶
Labs 6 and 7 use before() and after() to manage a real Kafka broker container and producer cleanly:
public class LifecycleExampleSimulation extends Simulation {
private KafkaProducer<String, String> producer;
@Override
public void before() {
String bootstrap = EmbeddedKafka.start(); // (1)!
producer = createKafkaProducer(bootstrap);
}
@Override
public void after() {
if (producer != null) {
producer.flush(); // (2)!
producer.close();
}
}
// Scenario & setUp(...) defined here
}
- Executed once before injection begins—starts the Docker Kafka broker via Testcontainers.
- Executed once after all virtual users finish—flushes buffered records and closes network sockets cleanly.
Single Virtual User Timeline¶
sequenceDiagram
participant S as Scheduler
participant VU as Virtual User #1 (Session)
participant API as Target Server
participant St as StatsEngine
S->>VU: Spawn at t = 0.00s
Note over VU: feed(userFeeder) -> userId=101
VU->>API: GET /posts (t = 0.01s)
API-->>VU: 200 OK (t = 0.18s)
VU->>St: Log "GET /posts" OK, 170ms
Note over VU: check(jsonPath("$[0].id").saveAs("postId"))
Note over VU: pause(1s) — non-blocking timer
VU->>API: GET /posts/1 (t = 1.18s)
API-->>VU: 200 OK (t = 1.30s)
VU->>St: Log "GET /posts/1" OK, 120ms
Note over VU: Scenario complete — VU exits
Next Steps¶
- Read next: HTTP vs Kafka Patterns — Compare synchronous request-response load testing with asynchronous event-streaming simulations.