When it comes to performance testing, plenty of tools are available, and Gatling is one of them. In this article, we’ll explore what Gatling is, its key features, the installation process, and how to conduct load testing using Gatling. We’ll also discuss the output results and address some commonly asked questions.
Table of contents
- What is Gatling?
- Features of Gatling
- Installation of Gatling
- Sample performance test with Gatling
- Commonly asked questions on Gatling
- Conclusion
What is Gatling?
Gatling is an open-source load-testing tool that helps measure web application performance. It helps developers and testers add large numbers of virtual users and check how their applications can handle high loads. Gatling can handle such high loads because it has a non-blocking and asynchronous architecture that implements virtual users as messages instead of dedicated threads. Gatling is built with the help of Akka toolkit and Netty framework, which makes Gatling very resource-efficient.
Gatling was first started as a personal project by Stéphane Landelle in 2011, and since then, it has been a popular choice for performance testing web applications. It also comes with several built-in tools, such as the Recorder, which helps users record and replay scenarios, and the Gatling Compiler, which helps convert Scala simulations into executable tests.
Features of Gatling
Gatling helps users run up to 60,000 concurrent users per load generator (a machine that simulates user traffic), and users can create multiple load generators. It offers flexibility in developing test scenarios, as users can write their tests using various programming languages such as Scala, Kotlin, and Java. Its distributed load testing feature allows users to distribute the load across multiple machines, which helps to perform more extensive load tests. After each test, Gatling generates an HTML report that includes colorful graphs, detailed statistics, and many testing parameters for easy analysis.
Gatling’s Recorder tool is another highlighting feature, making creating test scripts easy by capturing website actions or flow. This saves users time and effort in test script development. Gatling also offers features like data feeders for injecting dynamic data into tests and built-in assertions for automatically validating test results. We can also integrate Gatling with CI/CD tools, which will help with continuous performance testing throughout development. Additionally, Gatling supports protocols such as HTTP, JMS, SSE, Web Sockets, and MQTT (in the paid version).
Installation of Gatling
In this guide, we will configure Gatling using Java. It is compatible only with 64-bit OpenJDK LTS versions, such as 11, 17, and 21. Therefore, setting up your Java JDK before proceeding with the Gatling installation is advisable.
Maven set up
Download and extract the latest version of Maven from the official Apache Website.
Open System Properties by pressing Win + R, typing sysdm.cpl, and press Enter.
Go to the Advanced tab and click the Environment Variables button.
Under System variables, click New. Set the variable name as MAVEN_HOME and the variable value as the path to your Maven directory. For example, C:\Program Fiels (x86)\apache-maven-3.9.8.
Edit the Path variable in System variables.
Add a new entry by clicking the New button, and in the new row, put the path directory of the maven folder followed by \bin. Then, click the OK buttons to save the changes.
To verify the installation, open your command line tool, type mvn -version, and press Enter. It will confirm that the Maven version is displayed.
Gatling Installation
Navigate to Gatling’s official website.
Scroll down to find and download the open-source version of Gatling.
Once downloaded, extract the zip file to your desired location.
Open System Properties and navigate to the Advanced tab > Environment Variables.
Under the System Variables section, create a new variable. Name it GATLING_HOME and set its value to the path of the extracted Gatling folder.
In the same System Variables section, find and select the Path variable. Then, click on the Edit button.
Now, we have to add the path of Gatling by creating a new entry by clicking on New.
Sample performance test with Gatling
For our sample performance test, we are going to use IntelliJ IDEA (Community Edition) as our IDE, and we will use the URL https://github.com/alexwohlbruck/cat-facts?tab=readme-ov-file for our load test.
Create new project in IntelliJ
Open IntelliJ and create a new project by clicking File > New > Project.
On the left side, make sure to select Java. Add a name for your project, add your project location, and select Maven as the build system. Also, check and select the appropriate Java version, and click the Create button.
Configure pom.xml file
After the creation of the project, the pom.xml file will be open by default.
This is the default contents of the pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.automatenow</groupId>
<artifactId>f</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>Code language: HTML, XML (xml)
To run Gatling tests here, we need to add the Gatling Maven plugin and its dependencies to our pom.xml file. Here’s the complete pom.xml file with Gatling support added:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.automatenow</groupId>
<artifactId>f</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<gatling.version>3.11.5</gatling.version>
<gatling-maven-plugin.version>4.9.6</gatling-maven-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>io.gatling.highcharts</groupId>
<artifactId>gatling-charts-highcharts</artifactId>
<version>${gatling.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.gatling</groupId>
<artifactId>gatling-maven-plugin</artifactId>
<version>${gatling-maven-plugin.version}</version>
</plugin>
</plugins>
</build>
</project>Code language: HTML, XML (xml)
All plugins and dependencies in this POM file are set to the latest available versions as of the time of writing. This is because there are currently no known compatibility issues between these versions. So, always verify compatibility when updating versions in your projects.
Create Java class
Right-click on the Java folder in the left-hand project structure and click on New > Package.
Give a name to the package and click the enter button.
Then, right-click on the package file and select New > Java Class. Give your class a name (without spaces) and press Enter.
After that, we have to add the name of our Java class, in the format of package_name.class_name to our existing pom.xml file (inside the plugin block) for the simulation run.
<configuration>
<simulationClass>ABC.LoadautomateNow</simulationClass>
</configuration>Code language: HTML, XML (xml)
Write test scripts for the Java class
package ABC;
import io.gatling.javaapi.core.*;
import io.gatling.javaapi.http.*;
import static io.gatling.javaapi.core.CoreDsl.*;
import static io.gatling.javaapi.http.HttpDsl.*;
public class LoadautomateNow extends Simulation {
HttpProtocolBuilder httpProtocol = http
.baseUrl("https://github.com/")
.acceptHeader("application/json")
.userAgentHeader("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");
ScenarioBuilder scn = scenario("Get Git Profile")
.exec(http("Get Request")
.get("/alexwohlbruck/cat-facts?tab=readme-ov-file")
.check(status().is(200))
);
{
setUp(
scn.injectOpen(rampUsers(500).during(30))
).protocols(httpProtocol);
}
}Code language: JavaScript (javascript)
Here’s what the script does:
- We’ve defined the test in the
ABCpackage with a class namedLoadautomateNow. - We set up the base URL
https://github.comand configured the appropriate headers. - We created a scenario named Get Git Profile that sends a GET request to the
/alexwohlbruck/cat-facts?tab=readme-ov-fileendpoint. - The test is configured to ramp up to 500 concurrent users over 30 seconds.
- We’ve included a basic check to ensure each request receives a 200 OK status code.
In this example, we’ve used 500 users ramping up (increasing from 0 to 500) over 30 seconds. You can change these numbers based on your load or stress testing needs. Using a larger number of users or a longer increase in time will create more pressure on the system. This can help find performance problems that only show up under heavy load. But be careful, as it might also put a lot of stress on your test environment and the website you’re testing.
Running the performance test
To open your run console window, press Alt + F12.
If there are some existing or older test results, type mvn clean and click Enter.
Now, type mvn clean gatling:test to execute our newly created tests. Afterward, Gatling will execute the performance test scenario defined in our Java class.
During the test execution, you’ll see real-time console output showing the test progress, including the number of requests sent, response times, and any errors encountered.
The console will display a summary of test results, including total request count, requests per sec, and response time distribution statistics.
Once completed, Gatling will generate a detailed HTML report.
You can open this HTML test report in a web browser to view detailed metrics, charts, and graphs of the test performance, including response times, requests per second, and any errors or failed assertions.
While writing the test scripts or run commands, ensure that you write the exact spelling to avoid unexpected errors. Gatling is case-sensitive in nature.
Commonly asked questions on Gatling
Is Gatling good for performance testing?
Yes
Gatling is an excellent performance testing tool. It’s widely used for its ability to run high-load scenarios and provide detailed performance metrics. Gatling is also known for its user-friendly scripting language, which makes it easy for developers and testers to create and maintain test scenarios.
Additionally, it generates detailed, easy-to-understand test reports that help teams quickly identify performance issues and analyze system behavior under load. These features, combined with its efficient handling of concurrent users, make Gatling a powerful application for performance testing.
Which is better, JMeter or Gatling?
JMeter and Gatling are both useful performance testing tools. JMeter has its own standalone interface and supports many protocols. On the other hand, Gatling offers better performance for high loads and code-based testing. JMeter lacks Gatling’s efficiency at scale, but it is more user-friendly for beginners compared to Gatling. Gatling produces detailed, colorful HTML reports and has a recorder for test creation.
Lastly, JMeter has a larger plugin ecosystem and supports more testing scenarios. Due to their different strengths, choosing between JMeter and Gatling solely depends on project needs and team skills.
What is the command to run a Gatling test?
The typical command to run a Gatling test is:
mvn clean gatling:testCode language: CSS (css)
This command uses Maven to run Gatling tests. It first cleans the project (clean) and then executes the Gatling tests (gatling:test).
Suppose you have multiple classes in your test and only want to run a single class. In that case, the run command will be:
mvn gatling:test -Dgatling.simulationClass=your.package.Your_ClassName
What is a Gatling tool used for?
Gatling is primarily used for:
- Executing multiple concurrent/virtual users to measure your application’s performance
- Capturing response times and resource usage under various load conditions
- Pushing the application to its limits to find breaking points
- Comparing performance across different configurations or versions
- Creating attractive HTML reports with different graphs and statistics
- Ensure that the system can handle the increased load
Final thoughts on performance testing with Gatling
In conclusion, Gatling is a powerful and flexible tool for performance testing, offering developers and QA engineers a powerful platform to ensure their applications can handle real-world loads. By implementing the techniques and strategies discussed in this article, you can develop more effective, efficient, and insightful performance tests. As you continue your journey with Gatling, stay tuned to our upcoming articles on Gatling performance tests. Happy testing!
Don’t miss these
Follow our blog
Be the first to know when we publish new content.
- How to find an element in Selenium - October 5, 2024
- Resolved: SessionNotCreatedException - October 2, 2024
- Katalon Recorder Automation Overview - September 12, 2024