Gatling: Basics of Simulation Script.


Gatling is a highly capable load/performance testing tool. It hits your API with defined HTTP protocols and generates a beautiful report showing all scenarios with success and failure rate. It is designed for ease of use, maintainability and high performance.

In this blog, I’ll take you through the main components of Gatling Simulation Script. For the basic setup overview of Gatling, you can visit Gatling-Maven-Setup.

In Gatling Simulation Script, the most important component is a setUp() method. It executes the suite of test cases with specified ramped-up users at time boundaries/thresholds. The setUp() needs an HTTP configuration parameter to execute or hit the requested endpoint with given headers like content-type, access-tokens etc.

Given below is an example setUp():

  setUp(scenario.inject(constantUsersPerSec(perMinute(USERS_PER_MINUTE)) during(DURING_TIME minutes)))
    .protocols(httpConf).assertions(
       global.responseTime.max.lt(50),
       global.failedRequests.percent.lt(5)
     )

Like you see in the above-given code, the setUp takes a Scenario to execute with some httpConf (i.e HTTP configuration).

With that said, the next component comes into the picture is a Scenario. It is kind of test cases which takes a single Endpoint to hit an up and running API provide its host URL.

   val validUser = repeat(10) {
      exec(http("Get Valid User")
        .get("/user").check(status.find.in(200)))
    }
    val inValidUser = repeat(10) {
      exec(http("Get InValid User")
        .get("/wronguser").check(status.find.in(400,401,404,501,502))
        .headers(headers_1))
        .pause(1)
    }

A Scenario can also consist of multiple endpoints sequencing them in a chain of execs’ i.e. executions but it is only recommended whenever we have a process dependent scenarios i.e if an endpoint takes the output of another endpoint as input the former can be sequenced after the later in the same Scenario.

  val scn: ScenarioBuilder = scenario("My Service").repeat(5) {
    feed(feeder).exec {
      exec(http("Request 1")
        .post("/enpoint/url1")
        .body(StringBody("""yourJsonBody""")).asJSON
        .check(jsonPath("$.status").is("200"))
      ).exec(
        http("Request 2")
          .post("/enpoint/url1")
          .body(StringBody("""yourJsonBody""")).asJSON
          .check(jsonPath("$.status").is("200"))
      )
    }
  }

The next Component is httpConf:, it is the holder for the header configurations which will propagate with the request hit.

  val httpConf :HttpProtocolBuilder= http.baseURL(ConfigFactory.load("gatling.properties").getString("BASE_URL"))
    .acceptHeader("application/json; charset=utf-8")
    .doNotTrackHeader("1")
    .acceptLanguageHeader("en-US,en;q=0.5")
    .acceptEncodingHeader("gzip, deflate")
    .userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0")

Then there is a Feeder we need many times.

val requestFeeder: RecordSeqFeederBuilder[String] = csv(FEEDER_PATH).circular

Yeah, so these were some important components of a Gatling Simulation Script we should be aware of. For a running example, please visit Gatling-Sample-Service.

Hope it could help.

Pasted image at 2017_11_27 04_17 PM

This entry was posted in Scala. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s