Gatling Quick Reference - Common Gatling Functions

Amir Ghahrai

Amir Ghahrai

Follow on Twitter

This post serves as a quick reference guide for Gatling tool for performance testing.

Previously, we saw how to organize your Gatling project in a logical and easy to understand structure.

In this post, we look at some examples and usages of some common Gatling functions when creating performance test scripts.

Gatling examples covered in this post are:

Simple simulation

import io.gatling.core.Predef._
import io.gatling.http.Predef._

class SimplestSimulation extends Simulation {

Using an HTTP Proxy

setUp(scenario("Proxy on")
    .protocols(http.proxy(Proxy("", 8080)))

In the above example, is the proxy URL and 8080 is the proxy port.

HTTP Requests

GET Request

A simple GET request with query parameters

http("Get Gatling posts")
    .queryParam("post", "gatling")
    .queryParam("category", "performance testing")
    .header("Accept-Language", "en")

POST Request

A sample POST request with form params, e.g. submitting a form:

http("POST with params")
    .formParam("firstname", "David")
    .formParam("lastname", "Brown")
    .header("Accept-Language", "en")

A sample POST request with a file payload which must be in src/test/resources/bodies

http("Post with file payload")



Note: In order to use the pause, you need to add this import
import scala.concurrent.duration.DurationInt

scenario("with secode pause")
    // ...
    .pause(2, 3) // will make a random pause of 2-3 seconds
    .pause(2) // will make a fixed pause of 2 seconds

    scenario("millisecond pause")
    // ...
    .pause(200.milliseconds) // fixed pause of 0.2 second


    .repeat(3)( // repeat 3 times

Injection of virtual users

val scn=scenario("Virtual users")
    rampUsers(10) over(5.seconds))

Ramp up

rampUsers(10) over(5.seconds) 
    // linear rampup 
    // 10 users added over 5 seconds (1 extra user every 500 ms)

    constantUsersPerSec(10) during(5.seconds) 
    // adds 10 users every second
    // (so a total of 50 users after 5 seconds)

At once

// no new users added during 4 seconds

// 10 users added immediately
// not really recommended since it can hammer down the tested server

heavisideUsers(10) over(2.seconds)
// better approximation of a peak of users

Checks and Assertions

In Gatling, checks are usually used to check for status codes response bodies, whereas assertions are normally used to assert on timings of responses.


Checking status and JSON data:

    .check(jsonPath("$.name").is("some name"))

Saving response data to Gatling session


    // You can now use $newLocation and $name in your requests :
    http("get home").get("/users/${name}")


setUp(scn).assertions(, // mean resp time < 50 ms // for each request, < 5% failure


Basic usage

val feeder1 = Array( 
    Map("foo" -> "foo1", "bar" -> "bar1"), 
    Map("foo" -> "foo2", "bar" -> "bar2"), 
    Map("foo" -> "foo3", "bar" -> "bar3")

// repeating the values 
val feeder1a = feeder1.circular
val feeder1b = feeder1.random

// infinite entries with keys "value1", "value2"
val feeder2 = Iterator.continually(Map("value1" -> 100, "value2" -> "toto"))

// infinite random entries 
val feeder3 = Iterator.continually(Map(
    "value1" -> Random.nextInt(100),
    "value2" -> Random.alphanumeric.take(4))

// using the feeder to build the URLs
scenario("scenario name")
    .exec(http("request name")

Advanced usage

// reading a csv file to build a feeder
val feeder = csv("data.csv")
// the csv file must have a header row which defines the keys and be comma (,) separated

// filling a template file with the content of a feeder
    http("request name")
#gatling #performance