Performance Testing QA Automation

Gatling Performance Testing for Backend Reliability

Performance Testing for Backend Reliability

Introduction:

Gatling is a robust open-source tool designed for performance testing, ideal for evaluating the scalability and responsiveness of backend systems. By simulating multiple virtual users and generating load, Gatling helps uncover performance bottlenecks, ensure reliability under stress, and validate system behavior under various conditions. This blog will guide you through using Gatling to seamlessly assess your backends’ performance, providing insights through detailed metrics. 

Installation Step for Gatling:

1. Download Gatling

  • Visit the Official Website: Go to the Gatling download page: Gatling Downloads. 
  • Choose Your Version: Download the latest stable version of Gatling from the available options (usually provided in a zip or tar.gz file). 
  • Extract the Archive: Right-click on the downloaded .zip file and select “Extract All…” or use a tool like WinRAR or 7-Zip. 

2. Install Java

  • Refer – link  
  • Gatling requires Java to run. Ensure you have Java installed on your system: 
  • Check Java Installation: Open a terminal (or command prompt on Windows) and type: Java –version 
Installation Step for Gatling:
  • Set Up JAVA_HOME: Open System Properties > Environment Variables. 
  • Add a new system variable named JAVA_HOME and set its value to the path of your JDK installation. 

3. Install Maven

  • Refer – link 
  • Check Maven Installation: Open a terminal (or command prompt on Windows) and type: mvn –version 
Installation Step for Gatling:
  • Set Up MAVEN_HOME: Open System Properties > Environment Variables. 
  • Add a new system variable named MAVEN_HOME and set its value to the path of your Maven installation. 

4. Install IntelliJ

What is Backend?

Backend system refers to the server-side components of a software application that handle data processing, business logic, and interactions with databases or other services.

What are we testing as part of Backend?

It includes Application Programming Interfaces (API) that facilitate communication between the backend and other components, including front-end applications or third-party services and systems where data is stored and managed, such as SQL Database. 

Step-by-Step Guide to Identify Performance for Backend:

1. Define Objectives and Scope

  • Scope: Define the specific components of the backend system to test (e.g., APIs, database queries, microservices). 
  • Identify Goals: Determine what you want to achieve with the performance test (e.g., load capacity, response times, stability) 

2. System Understanding

  • Architecture Review: Examine the backend architecture, including servers, databases, APIs, and third-party services. 
  • Performance Criteria: Define performance metrics and criteria such as acceptable response times, throughput, and error rates. 

3. Create Test Scripts

  • Choose a Tool: Select a performance testing tool like Gatling. 
  • Script Development: Write performance test scripts that define the scenarios, user interactions, and load patterns. For Gatling, this involves creating Scala-based simulation scripts. 

4. Execute Performance Tests

  • Run Tests: Execute the test scripts under various conditions to simulate different load levels and scenarios. 
  • Monitor: Continuously monitor system performance during testing to capture real-time data. 

5. Analyze Results 

  • Data Collection: Gather and analyze the performance data and metrics collected during the test. 
  • Identify Bottlenecks: Look for performance issues such as slow response times, high error rates, or resource constraints. 
  • Compare Against Criteria: Evaluate whether the system meets the predefined performance criteria and objectives. 

Setup Gatling Project for Backend:

To initialize Gatling project and how different types of tests work, we will understand step by step in this section.

  • Step 1: Install JDK version 8 or later and validate the installation status by running the command.
  • Step 2: Install Maven, version should be greater than 3.6.
  • Step 3: Install IntelliJ IDEA which is one of the IDE, which comes with the built-in Scala support.
  • Step 4: To initialize a project with Gatling, we have two methods either you can download it from the gatling.io website or we can use the Maven Archetype based on project. In this section I have used Maven Archetype as it is an easy method to create a project. 
Setup Gatling Project for Backend

Please note in maven project the “archetypeGroupId” must be io.gatling.highcharts and “archetypeArtifactId” must be gatling-highcharts-maven-archetype. The rest depends on your preferences. 

Once you have initialized the project, open it with an IDE and for this blog we have used IntelliJ IDEA. Import the project and open the Engine. Scala class, which is present in following path src > test > scala folder. If you have found “No Scala SDK in module” message at the top, click on “Setup Scala SDK” and add the Scala SDK into the project as it is in built function. 

Gatling Simulation Class Extension

Each gatling test class must extend the Simulation class or any base simulation. The Gatling script is composed of mainly three parts. 

  1. HTTP Configuration: Each Gatling script has an HTTP configuration in the first step. Here we defined the base Url where each request is sent to, with the header included in each HTTP call. 
  2. Scenario Definition: Following we have second steps which is called as Scenario definition. It basically shows the steps that the virtual user will follow. In the example below, a GET/POST request is sent to the endpoint along with the domain coming from the base Url information in the HTTP Protocol. The body/params in the scenario and http functions define the reason of the test, and the results, which are presented in the generated load testing report, are matched with these defined scenarios. 
  3. Load Scenario: The load scenario is the last step that will inject the load for the defined scenarios, it includes number of virtual users, ram up time and duration of test. In the example below we have inserted 10 virtual users with a single iteration. All the set up will be inserted inside the scn object, and the http protocol.  

Examples

Example 1: Testing for GET related APIs 

package com.perf.tests 

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

class APITest1 extends Simulation {

//Protocol
val httpProtocol = http
.baseUrl("https://reqres.in/api/users")

//Scenario
val scn = scenario("Get API Request")
.exec(
http("Get Single User")
.get("/2")
.check(status.is(200)))
.pause(1)

//Setup

setUp(
scn.inject(atOnceUsers(10)).protocols(httpProtocol)
)
}

Gatling Report:

Gatling Performance Testing Reports

Example 2: Testing for POST related APIs

package com.perf.tests 

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

class POSTAPIDemo extends Simulation {

//Protocol
val httpProtocol = http
.baseUrl("https://reqres.in/api")

//Scenario
val scn = scenario("Create User")
.exec(
http("Create User req")
.post("/users")
.header("content-type", "application/json").asJson
.body(StringBody(
"""
| {
| "name": "John",
| "job": "leader"
|}
|""".stripMargin)).asJson
.check(status.is(201)))
.pause(1)

//Setup

setUp(
scn.inject(
atOnceUsers(10)).protocols(httpProtocol)
)

}

Gatling Report:

Gatling Performance Testing Reports

For the above two examples, gradually increase the load and compare against the criteria pre-defined. Follow the same steps for other type of request method to identify the performance. 

Conclusion:

Performance testing of the backend system using Gatling has provided valuable insights into the system’s behavior under different load conditions. The tests identified key performance metrics, such as response times and throughput, and highlighted potential bottlenecks and areas for optimization. By addressing these issues and implementing improvements, we can enhance system reliability and efficiency. Continuous performance testing with Gatling and monitoring will be essential to ensure the system meets evolving demands and maintains optimal performance. 

vishal-kumar

Associate QA Lead