Quick Code snippets for Gatling

Gatling is another trendy stress-test tool, which can be used for simple scenarios, as well as for complex scenarios. Quick start tutorial can be followed from here

Two Scnarios Run Concurently

class TwoScnarios extends Simulation {
    val httpProtocol = http
        .acceptEncodingHeader("gzip, deflate")
        .userAgentHeader("Mozilla/5.0 blah blah blah")

    object Search {
        val search = exec(http("request_0")

    object Browse {
        val browse  = exec(http("request_0")

    object Edit {
        val edit  = exec(http("request_0")

    val usersScn = scenario("Users").exec(Search.search, Browse.browse)
    val adminsScn = scenario("Admins").exec(Search.search, Browse.browse, Edit.edit)

        usersScn.inject(rampUsers(10) over (10 seconds)),
        adminsScn.inject(rampUsers(2) over (10 seconds))

As a result of above code we will have two kind of users (10 Users and 2 Admins) will be running together with two predefined scenarios (userScn and adminScn accordingly). over (10 seconds) means ramping up number of users over given time. If explain in a more clear english, in 10 seconds we would like to see 2 admins are running (or in 10 seconds 10 users must be running). rampUsers function does not guarantees the number of concurent users before given time and after given time (actually plus/minus few seconds can be allowed). For detail you can check video presentation of Niko Kobler.

Global Assertions

Global assertions can be defined and if the assertion fails then the whole scenario stops with assertion exception


above assertions are doing followings:

  • if the maximum response time becomes greater than 100ms then exception is thrown
  • if the successful request percentation decreases 95% then exception is thrown

Check and Failure Management

By default Gatling checks if the http response status is 20x or 304. To demonstrate failure management we will introduce a check on a condition that fails randomly

import java.util.concurrent.ThreadLocalRandom // 1

val edit = exec(http("Form")
    .check(status.is(session => 200 + ThreadLocalRandom.current.nextInt(2)))) // 2


  • First we import ThreadLocalRandom, to generate random values.
  • We do a check on a condition that’s been customized with a lambda. It will be evaluated every time a user executes the request and randomly return 200 or 201. As response status is 200, the check will fail randomly.

To handle this random failure we use the tryMax and exitHereIfFailed constructs as follow:

val tryMaxEdit = tryMax(2) { // 1
}.exitHereIfFailed // 2


  • tryMax tries a given block up to n times. Here we try a maximum of two times.
  • If all tries failed, the user exits the whole scenario due to exitHereIfFailed.

Real Time Monitoring

Gatling supports graphite protocol which can store values in time-series data store (such as influxdb). After that we can use Grafana to visualize time-series data on it.

Distributed Testing

Distributed testing can be reached by applying following technologies

  • Build-Slaves
  • Fat-jars
  • Influx (& Grafana)

Otherwise frontline cloud solution can be used, but which will cost you some money