Performance Testing with Gatling: Scripts as Code
Gatling puts the test in a programming language and the load model in one readable line. For teams that keep tests in Git and run them in CI, that is the whole argument.
In this article
- Where Gatling differs from JMeter
- A simulation, end to end
- The injection profile is the workload model
- Assertions turn a report into a pass or a fail
- Running it, and in CI
Where Gatling differs from JMeter
| JMeter | Gatling | |
|---|---|---|
| Script | XML built in a GUI | Scala, Java or Kotlin in your editor |
| Review | Hard to diff | A normal pull request |
| Concurrency | One thread per user | Asynchronous – far more users per machine |
| Report | HTML dashboard | HTML report, generated every run |
| Learning curve | Lower to start | Higher to start, lower to maintain |
Neither is better in the abstract. Gatling earns its keep when the suite is big enough to need reviewing and re-using like any other code.
A simulation, end to end
import io.gatling.javaapi.core.*;
import io.gatling.javaapi.http.*;
import static io.gatling.javaapi.core.CoreDsl.*;
import static io.gatling.javaapi.http.HttpDsl.*;
public class CheckoutSimulation extends Simulation {
HttpProtocolBuilder httpProtocol = http
.baseUrl(System.getProperty("baseUrl", "https://example.test"))
.acceptHeader("application/json")
.userAgentHeader("Gatling/perf");
FeederBuilder<String> users = csv("users.csv").random();
ScenarioBuilder checkout = scenario("Browse and check out")
.feed(users)
.exec(http("login").post("/api/login")
.body(StringBody("{"user":"#{username}","pass":"#{password}"}"))
.check(status().is(200))
.check(jmesPath("data.token").saveAs("token")))
.pause(2, 6)
.exec(http("catalogue").get("/api/products")
.header("Authorization", "Bearer #{token}")
.check(status().is(200))
.check(jsonPath("$.items[0].id").saveAs("productId")))
.pause(3, 8)
.exec(http("add to cart").post("/api/cart")
.header("Authorization", "Bearer #{token}")
.body(StringBody("{"productId":"#{productId}","qty":1}"))
.check(status().is(201)));
{
setUp(checkout.injectOpen(
rampUsers(200).during(120),
constantUsersPerSec(20).during(900)
)).protocols(httpProtocol)
.assertions(
global().responseTime().percentile3().lt(1500),
global().failedRequests().percent().lt(1.0)
);
}
}
The injection profile is the workload model
- atOnceUsers(n) – everyone arrives together. Use it for a spike test, not a load test.
- rampUsers(n).during(t) – arrivals spread over t. This is your ramp-up.
- constantUsersPerSec(r).during(t) – an arrival rate rather than a user count. Closer to how real traffic behaves.
- nothingFor(t) – a quiet gap, useful between phases of a soak.
Open models describe arrivals; closed models describe a fixed population. If you are modelling a website, arrivals are usually the honest choice.
Assertions turn a report into a pass or a fail
The assertions block above is what makes this runnable in a pipeline. Without it every run is green and someone has to read a chart to find out whether it was. With it, p99 above 1.5 seconds or more than 1% failures exits non-zero and the build fails.
Running it, and in CI
mvn gatling:test -Dgatling.simulationClass=CheckoutSimulation -DbaseUrl=https://staging.example
# GitHub Actions
- run: mvn -B gatling:test -DbaseUrl=${{ secrets.STAGING_URL }}
- uses: actions/upload-artifact@v4
if: always()
with:
name: gatling-report
path: target/gatling/
Key points
- The injection profile is the workload model – write it deliberately.
- Assertions are what make a performance test a pipeline step rather than a chart.
- Save and re-use every server-generated value; a replayed token is not a test.
- Gatling costs more to learn and less to maintain. Choose on suite size.
Practise this on the free applications at jstestingacademy.co.in/qa-apps – no account needed.


