Accelerate performance with Gatling:3.3.0-Warmup

Reading Time: 5 minutes

Hello, welcome to the first blog of our Gatling series. Objective is to see how we can accelerate the performance of the product with a tool like Gatling and how to easily integrate with our code base to run the test on different environments. This series is totally based on my personal experience so I have tried to cover all the possible scenarios that I have faced while using this tool.

In this post we will learn the following:

  • Setup Gatling-SBT plugin
  • Gatling Components and terminologies
  • Send a get request
  • Check on status code
  • Print Response using sessions
  • Assert the response attributes

Setup Gatling-SBT plugin:

Gatling is an open-source performance testing tool that is written in Scala and backed with Akka and netty. As it is a Scala based tool so its recommended to use the Gatling-SBT plugin because SBT has native and integral support of Scala. To setup Gatling with SBT, we need to follow simple steps:

  • Create a scala project with SBT: Here I am using intelliJ as an IDE which has an inbuilt Scala plugin. If you are using any other IDE please make sure that Scala is installed and integrated with your IDE. In intellij create a normal Scala project. as shown in below Images:
  • Add the Gatling-SBT plugin in your project’s plugins.sbt: Create a file named plugins.sbt in your project directory and add Gatling plugin in your project by adding following line in your file.

addSbtPlugin(“io.gatling” % “gatling-sbt” % “3.1.0”)

  • Add the required Gatling dependencies in your project’s build.sbt: Go to your build.sbt and add following Gatling dependencies to start with a basic simulation:

enablePlugins(GatlingPlugin)

libraryDependencies += “io.gatling.highcharts” % “gatling-charts-highcharts” % “3.3.1” % “test,it”

libraryDependencies += “io.gatling” % “gatling-test-framework” % “3.3.1” % “test,it”

  • Compile the project with SBT: Compile the project to resolve the dependencies with following sbt command from the terminal window:
  • $ sbt clean compile

Post successful execution of above command, you should see something as follows:

At this stage we are done with the Gatling project setup. Before moving on let’s get some understanding about the Gatling class structure which help you to relate with the official documentation.

Gatling Components and terminologies:

Gatling test is the combination of following building blocks:

  • Gatling Simulation: A class that define your load test and describe the different configuration for the test, like what is the scenario, number of users, HTTP protocol etc. basically we can consider it as a main class of your load test.
  • HTTP Protocol: HTTP is the main protocol that Gatling supports. HTTP protocol is the combination of different configurations at protocol level. Which includes the baseUrl, engine parameters, Host etc.
  • Injection profile: Gatling injection profile enables us to test our application for different use case and in different conditions. from injection profile we can control how the no of users will interact with the server.
  • Session: Session is a virtual user state, that helps to manage the data between multiple thread. As Gatling is based on Akka actor model so everything is a message here where session help to manage that message passing between different users. So when you test in real time, you will not mix-up with the data for same type of multiple request.

Now let’s see how to interact with real time APIs. Here, we are using a public API “GET Countries v1”. This API returns the list of countries based on the provided parameter in the request URI. So lets get the list of countries:

Send a GET Request:

So to send a GET request let’s design our test as follows:

  • Create a test class which extends the Simulation class.
  • Define the HTTP protocol.
  • Define the scenarios
  • Run the test by defining a setup.

Gatling checks:

Gatling check’s first purpose is to verify response against a request and to check weather its as per the expectations or not. So here we can perform multiple checks against status code, response body and many more.

  • Verify the status code is as expected or not: here we check
    • status code is 200.
    • status code is any one of 200,204,206.
    • status code not in 500
  • Verify that the response body should contain the capital of the country.

As of now all this is happening in isolation, now what if I want to see the response body of my request or want to extract the response attributes to use in another request. Here again, Gatling check API comes in picture to save the response in a session so we can use it in further process.

Extract Response body using sessions:

As I said earlier Gatling session is a virtual user state that used to manage the data between request, So if we execute our API for ‘n’ no of users so we need to save the response for every request that we can do as follows :

Gatling Assertion:

The Assertions API is used to verify that global statistics like response time or number of failed requests matches expectations for a whole, one can easily add the assertion by calling assertion method in a the setup.

Execute the test:

Here we are done with our test design, we have sent a get request, check on status code and response attribute and assert on the max response time. Now it’s time to execute the test with the following command:

// To execute all the test in the gatling directory.
$ sbt gatling:test

To execute test for a specific API.
$ sbt compile gatling:testOnly “rapidAutomation/GetCountries.scala”

After execution, you will see the report on the terminal as well as an HTML report will generate in your target folder.

This is all about kick-start with Gatling, from setup to execute our first test against a GET request. Hope by now you have an idea on how Gatling works. In the next blog, we will see, how to send a POST request with Gatling, how to feed data from an external file, how to send different types of request bodies in Gatling. Thank you.

References: https://gatling.io/docs/current

Written by 

Gaurav is a Module Lead QA Consultant having experience of more than 4.5 years. He is well familiar with core QA concepts and well versed in designing automation frameworks in a microservice environment. He has good hands-on experience in automation tools like ReadyAPI, SoapUI, postman.io, rest-assured.io, Cypress.io, gatling.io, Selenium (Java & Scala) and others. He actively promotes shifting left in the development cycle and understands the project domain to work closely with the development team. He has a good understanding of different domains like Airline, health Care and Shipping and cruising domain.

3 thoughts on “Accelerate performance with Gatling:3.3.0-Warmup7 min read

Comments are closed.

%d bloggers like this: