In the previous post, we learned how to install and configure Gatling using Maven. In this article, we will explore on how to write a Gatling performance test with Java.
What is Gatling
Gatling is a script-based load and performance testing tool for web applications. It is written in Scala which allows it to run on any system. It can be easily integrated with version control systems and ci/cd pipelines. We can write the Gatling performance test with Java as well from version 3.7 as Gatling now supports Java along with Scala.
Is Gatling Open Source
Gatling offers two versions
1) Open-source which is free to use.
2) Enterprise: Gatling Enterprise additionally offers users the ability to generate load from multiple geographic locations with cloud-based injectors.
Gatling Terminology
Let’s start with the basic terminology of the Gatling framework :
1)Scenario: The summary of steps that the virtual users perform to simulate typical user behaviour, e.g., login to an application, searching for a product, Adding to a cart, etc.
2)Simulation: How many virtual users will execute the Scenario in a given timeframe?
3)Session: Sessions are messages passed along a scenario workflow or an action. A Session backs each virtual user.
4)Recorder: The Gatling UI records a Scenario and outputs a Simulation.
5)Feeders: A way to inject data for our virtual users from an external source like CSV files, JSON files, a JDBC data source, etc.
Getting Started with Gatling Performance Test
There are multiple ways to write and run performance tests with Gatling. To work with Gatling first we need to do the setup.
1) Gatling installation and configuration
After setting up the Gatling Java project we can run a basic simulation script and check if the setup project is working properly.
Go to src->test->Java folder and open it. Open and execute Engine class. It will execute a pre-configured simulation script that comes bundled with this project.
On execution, it will ask to provide an execution description and after successful execution, it will generate results and beautiful HTML Reports under the target folder.
2)Record your own script
Next, we will try to record and simulate our own scenario using a Gatling recorder. A recorder is a Gatling tool that allows you to record your actions on a web application and export them as a Gatling scenario.
Recorder Mode: We need to do some configuration to work with the recorder. In Gatling GUI we can either work with the HAR converters or can configure an HTTP proxy. I am going to use the HAR converter for the demo.
HAR Files or HTTP Archive is a JSON formatted log used to track a web browser’s interaction with the website. It can be easily obtained using Chrome dev tools.
Go to the Network tab–>Clear all the logs–>Check the Preserve log checkbox and start the recorder–>Select log file –>Right click –>Save as HAR with all content.
Test Scenario
1)Open the demo application
2)Click on the laptop category
3)Click on add to cart
4)Click on checkout
Export the file as HAR and store it in a folder. See the attached Media for reference.
Recorder Configuration
Go to the project folder and under src/test/java there is a recorder class. On click of the run, this class will launch Gatling recorder GUI. Configure the recorder as below mentioned configuration:
- Select recorder mode as “HAR Converter”.
- Browse the Recorded HAR file from your system and provide the correct path.
- Give package name
- Give ClassName
- Select Java Format (Java 8 or Java 11)
- Check to enable filters
- Click on start
Once this is done, a simulation java class named “demoblazeSimulation” will be generated under the src/test/java folder under the package provided by you. Open this class and you can see that a script has been generated for your scenario.
If you want you can modify this script as per your need, if not just simply go to the Engine class again and execute the engine class. This time we will see the option to choose our simulation from our recorded scripts. Select the simulation, provide the description and analyze the results.
From the global information section of the report, we can identify the max. response time of 700 ms for this test, while the mean response time was 420 ms. Keep in mind that this test was performed locally.
Execution Report
On top of the text-based result inside the console, Gatling creates an HTML-based report for further investigation. This visual test summary is stored within the gatling folder inside the output directory of our build tool. For Maven, that’s target
folder.
We can open this report using any available browser and analyze the execution outcome.
This demo code can be found on GitHub.
Click here for Part-3 of the Gatling Series.