Performance Testing with Gatling

Muhammad Arsalan Inam
Oct 12 · 7 min read

Performance Testing is a crucial part of Quality Assurance and Control. Stakeholders need to make sure that developed Web Service is able to perform well under stress. They need to measure the performance of the application when the traffic is at its peak.

There are lots of tools to perform Load Testing. For this tutorial, we are going to use Gatling with Gradle using Scala. Gatling is a very powerful tool for Load Testing. To learn about why we should consider Gatling for Performance Testing, click here.

Agenda

In this tutorial we will learn,

  • Create a Project Structure using best Practices
  • Use Gradle as Dependency Manager
  • Integrate Gatling
  • Create a Demo Simulation Class
  • Generate Gatling HTML Report

Prerequisites

You must have installed,

You should also have some basic knowledge about,


Create a Project Structure

Open IntelliJ IDE. Then go to File -> New -> Project

Select Gradle and Java. Click Next.
Add GroupId and ArtifactId. Click Next.
Use Defaults. Click Next.
Add Project Name and Location. Click Finish.

This is how the default Project Structure will look like

Default Project Structure.

Change Default Project Structure to Custom Project Structure

You need to Refactor the Project Structure as shown below in the picture

Customized Project Structure.

About Project Structure

There is no rule of thumb for a Project Structure, you can create any structure you’re comfortable with. But when designing a framework, we should consider about maintainability and re-usability. So it is important how we organize different components in the structure. Following are the packages definition that we have created in our Project Structure.

body : This package contains Request JSON payloads

config : This package contains Gatling configurations

data : This package contains Test data (e.g. test-data.csv)

application.properties: This file contains constants like Base URL

logback-gatling.xml : This file contains XML data to generate HTML Reports

simulation : This package contains Simulation Classes

util : This package contains Constants, FileUtil and PropertyReader

build.gradle : Configure all the plugins, versions, repositories, dependencies etc.


Add Scala Framework Support

Right click on Project’s root folder, Click Add Framework Support.

Click on Create, it will load the latest Scala version. Click OK.

Integrate Gatling

We need to add io.gatling.highcharts dependency in build.gradle file. Also, add org.json dependency as we will use it in simulation class demo.


Performance Test Demo

We will create a very simple performance test demo for a POST Method API. It will create a User and in response, we will save the Id of the created user in a separate JSON file which can be used as a input parameters for other API.

Here are the details of API:

Base URL: https://reqres.in

Path URL: /api/users

HTTP Method: POST

Request Payload:

Response Payload:


Code Implementation

Create a Demo Simulation Class

Create a Scala Class under ‘simulation’ package. This class will extend from Simulation.

A Simulation is a real Scala class containing 4 different parts:

  • The HTTP protocol configuration
  • The headers definition
  • The scenario definition
  • The simulation definition

To learn more about Simulation class, click here.

Create Constants Object

Create a Constants Scala Object under ‘util’ package. Define all the Constants, URLs and Paths here.

Create FileUtil Object

Create a FileUtil Scala Object under ‘util’ package. This class will contain method to write down the response body in a separate json file.

Create PropertiesReader Object

Create a PropertiesReader Scala Object under ‘util’ package. This class will contain method to read properties from application.properties file.

Create a Request Body JSON file

Create a JSON file. It will have a request payload.

Add Base_URL to application.properties file

Add gatling-xml configuration to generate Report

In logback-gatling.xml file. Add the following code to generate HTML report.

Add Java Task to execute Test in build.gradle

Add a Java Task and logbackGatlingConfig method to generate beautiful and well defined HTML Performance Reports to measure the performance of a Web Service.

So, the ‘build.gradle’ file will look like this.


Installation

Run the following command in Terminal and build the project. It will automatically download all the required dependencies.

If the build is successful. All the required dependencies are installed successfully.

Running the tests

Add Simulation class path in build.gradle file in order to run the Simulation.

Run the following command in Terminal to clean the build directory. To avoid conflicts, make sure to execute this command everytime before you run ‘gradlew testLoad’ command.

Run the following command in Terminal to execute a Java Task.

View Response JSON files

You can find the created response JSON files in the following directory of the Project.

These files are generated at runtime. The data in these files can be used as inputs for other APIs.

Validating Gatling Reports

You can find the Gatling HTML reports of the executed simulations in the following directory of the Project.

Under the gatling-results directory, go to simulation folder and open ‘index.html’ file. The HTML Performance Report will look like this.

Detailed Performance Report.

Git Repository

You can clone the Project from my personal Git space. Click here.

Muhammad Arsalan Inam

Written by

I’m a Test Automation Engineer. I love to solve programming challenges. I am skilled in Building and Maintaining different Test Frameworks using best practices.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade