Listen to this blog |
If your development is focused on providing HTTP support, Gatling is one of the best performance testing tools that we can use in load generation. Gatling is a Scala, Akka, and Netty-based open-source load and performance testing framework. Despite the fact that it is written in a domain-specific language, the tool provides us with a graphical user interface that allows us to record the scenario. When we finish recording the scenario, the GUI generates the Scala script that represents the simulation. Excited to learn how to master such a tool with so many features like the Gatling Recorder and a guide to creating a new Maven project? You’re at the right place, as we are one of the leading QA Companies with a lot of experience in using Gatling to its full potential.
Features of Gatling:
Let’s kickstart this blog by exploring the prominent features of Gatling.
Excellent HTTP Protocol – It makes Gatling a great choice for load testing web applications or directly calling APIs.
Scala simulation scripts – Since all the Gatling scripts are written in the Scala language, writing load testing scenarios in Scala provides great flexibility.
The Gatling Recorder – The Gatling recorder helps convert the flow of an application into a Gatling scenario. It is very much useful if you come across a complex web application.
The Code can be kept in the Version Control System – As the Gatling load test scenarios are written in the Scala language, they can be easily stored in the Version Control System. So the process of scenario code maintenance gets simplified.
Why Gatling?
Now that we have seen the main features of Gatling, let’s explore the other real-world benefits Gatling offers. Since it is an excellent tool for load/stress testing your system without regarding other performance requirements, several thousand concurrent users can be created from a single JVM. So, Gatling is an excellent tool for you to include in your continuous integration as it doesn’t require you to set up a distributed network of machines to perform testing. Apart from that, it is also useful if you want to write your own code rather than just recording the scripts. Now let’s see how to install Gatling.
Installing Gatling from Website
First and foremost, you have to download the Gatling performance testing tool from their official website. You will find two download options available there, one is the open-source version and the other is the Enterprise edition. We will be using the open-source version for this blog, and so you can go ahead and download that. The Enterprise edition does have a few additional features that you can explore once you are well-worse with the free version. Once your download has been completed, open the folder and unzip it.
Gatling has its own set of installation requirements for Windows and macOS. So you must have a JDK installed to execute the basic version. In addition to that, the utility requires JDK8.
Before we start, make sure to go through the Gatling manual to verify if you have got all the necessary prerequisites ready. Start by going to the bin folder in the unzipped Gatling folder to get Gatling from here. You could also do it by using the command prompt.
You could use Gatling.sh if you’re a Mac user. But since we’re on Windows, we’ll be using Gatling.bat. The tool will start up and run once you double-click on Gatling.bat. We will also be able to run a few example scripts provided by Gatling in the user directories.
If you wish to use command prompt, go to the Gatling Directory and open command prompt. Once you’ve opened the Gatling Directory Command Prompt, you can get the Gatling.bat file by using the following commands as shown in the image below.
- cd bin
- dir
- gatling.bat
Gatling Recorder
We have already established the fact that the Gatling recorder is one of the prominent features of Gatling. As one of the leading Test Automation Companies, we have found this feature to be very resourceful. So let’s learn how to set up and run your recorder as an HTTP proxy or a HAR converter.
The Gatling Recorder assists you in swiftly generating scenarios by acting as an HTTP proxy between the browser and the HTTP server or converting HAR (HTTP Archive) files. In either case, the recorder creates a rudimentary simulation that closely resembles your previously recorded navigation.
So, let’s take a look at how we can record using the HAR option.
Gatling Recorder Prerequisites
1) Gatling should be put in place.
2) The path to the Java Home should be set.
3) The Gatling Home path must be defined.
Gatling Recorder using the HAR File Converter
A HAR file (HTTP Archive) can be imported into the Recorder and converted into a Gatling simulation. The Chrome Developer Tools or Firebug or the NetExport Firebug add-on can be used to obtain the HAR files. Follow the below steps to convert the HAR files to Gatling simulations.
Step 1:
Right-click on the page and click on Inspect to open Developer Tools. Make sure the Preserve log option is checked under the ‘Network’ tab.
Step 2:
Once you finish navigating the website, right-click on the requests you want to export and click on the ‘Export’ option as shown in the below image.
Step 3:
Launch Gatling recorder
Use the below-mentioned command in command prompt to execute the file recorder file from the Gatling directory.
C:\Gatling\gatling-charts-highcharts-bundle-3.6.1\bin>recorder.bat
Once you’ve run the above command, the Gatling Recorder will start and you will be able to see the following page.
Step 4:
Check the Recorder mode and make sure that the HAR Converter mode is selected. In this scenario, it is set as ‘HTTP proxy’ by default and so let’s change it to HAR Converter. We will also be seeing how to record using the ‘HTTP Proxy’ mode in the next stage of the blog.
Step 5:
Import the saved HAR file into the recorder and click on the ‘Start’ button.
Step 6:
Once it has been successfully imported, you would have to run the following script using the below command.
C:\Gatling\gatling-charts-highcharts-bundle-3.6.1\bin>gatling.bat
Gatling Recorder using HTTP Proxy
So as stated above, we will now be seeing the steps you’ll need to follow to use the recorder using HTTP Proxy.
Step 1:
Configure the Browser
In order to use Gatling to capture our scenario, we must first configure our browser’s proxy settings. The following instructions will help you to set up Chrome for Gatling recording.
- Open the Chrome Browser.
- In the top right corner, click on the three dots to get a drop-down menu.
- Click on the ‘Settings’ option.
- Scroll down to the bottom of the settings page and click on the ‘Advanced’ drop down option.
- Click on System from the list of options that appear.
- Once you click on ‘Open your Computer Proxy Setting’, the proxy page will be shown.
- Uncheck the ‘Automatically detect settings’ option.
- Check the ‘Use the proxy server for your LAN’ option.
- The address should be “localhost” and the port should be “8000”.
- Click on the ‘Save’ Button.
Step 2:
Recording the Scenario
- First, you have to go to the bin folder of the Gatling bundle. (In our case: C:\Gatling\gatling-charts-highcharts-bundle-3.6.1\bin)
- Double click the “recorder.bat” file or run it using command prompt. Follow the same procedure as you did when using the HAR converter.
- The recorder window will be displayed.
- Enter the port number in the local host box as per your preference. (In our case, we have used 8000).
- Enter the package name.
- Enter the class name.
- Select the following options: “Follow Redirects,” “Infer HTML resources,” “Remove Cache Headers,” and “Automatic Referrers.”
- Choose the location of the output files. We have used C:\Gatling\gatling-charts-highcharts-bundle-3.6.1\user-files\simulations.
- Note: It’s a good idea to set simulation folders as defaults so that you wouldn’t have to copy the recorder file during load testing.
- Keep all the other options to be the same and don’t change any existing options as well.
- Click on the ‘Start’ button.
- Open Chrome browser and navigate the flow you wish to record.
- Once you are done, click the “Stop & Save” button to close the Gatling recording window.
- The recorded file will be saved and ready to run. The file will be stored in the directory that you specified in the previous step.
Step 3:
Executing load testing using Gatling
After the import is complete, you can run the script using the command given below.
C:\Gatling\gatling-charts-highcharts-bundle-3.6.1\bin>gatling.bat
Creation of First Maven Project Using Scala for Gatling
Before creating a maven Project using Scala, we will look at the prerequisites you will need for its creation.
1) JDK 1.8 or Higher version should be installed in your system
2) The following environment variables have to be set
a. Java_Home Environment Variable
b. Maven_Home Environment Variable
c. Gatling_Home Environment Variable
If you want to check if you have all the prerequisites, you can simply follow the following steps.
Step 1:
Right Click on ‘This PC’ > Properties > Advanced System Settings > Environment Variables > System Variables
Now, you will find all the environment variables available in your system. If you have all the required prerequisites, you can start with the creation of your first Maven Project.
Creating Your First Project
The easiest way to create a new project is by using an “archetype”. An archetype is a general skeleton structure or template for a project.
To create the project we need to follow a few steps:
Step 1:
Run the archetype plugin using command prompt by using the following command.
mvn archetype:generate -DarchetypeGroupId=io.gatling.highcharts -DarchetypeArtifactId=gatling-highcharts-maven-archetype
Once you press enter, you will see Maven downloading many Jar Files by resolving dependencies and downloading them as needed (Only once).
Step 2:
Once all the jar files have been downloaded, assign the groupId, artifactId, and the package name for your classes before confirming the archetype creation.
Step 3:
Once you’ve provided all the required details, you will see a build success message and the location of the particular project that implies your project has been successfully created.
Step 4:
Open the Generated Project using Intellij Idea and follow the below steps to open the project using Intellij.
File > Open > Select the directory for the generated project > Click on the pom.xml file
Once you click to open the pom.xml, you will notice that it takes a little time to load all the dependencies and the project structure. It will also contain the following launchers.
- Gatling Engine
- Gatling Recorder
The Gatling load test engine can be launched by right-clicking on the Engine class in your IDE. The target/Gatling directory will be used to store the simulation reports.
You can easily start the Recorder by right-clicking on the Recorder class in your IDE. The src/test/Scala directory will be used to construct the simulations. We have attached an image reference to show how it’ll look once everything has been loaded.
Now, you are all set and ready to write your first Gatling script.
The pom file will contain the following things:
<?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.gatling.highcharts</groupId> <artifactId>gatling-highcharts-maven-archetype</artifactId> <version>1</version> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <encoding>UTF-8</encoding> <gatling.version>3.6.1</gatling.version> <gatling-maven-plugin.version>3.1.2</gatling-maven-plugin.version> </properties> <dependencies> <dependency> <groupId>io.gatling.highcharts</groupId> <artifactId>gatling-charts-highcharts</artifactId> <version>${gatling.version}</version> </dependency> <dependency> <groupId>io.gatling</groupId> <artifactId>gatling-app</artifactId> <version>${gatling.version}</version> </dependency> <dependency> <groupId>io.gatling</groupId> <artifactId>gatling-recorder</artifactId> <version>${gatling.version}</version> </dependency> </dependencies> <build> <testSourceDirectory>src/test/scala</testSourceDirectory> <plugins> <plugin> <groupId>io.gatling</groupId> <artifactId>gatling-maven-plugin</artifactId> <version>${gatling-maven-plugin.version}</version> </plugin> </plugins> </build> </project>
Writing First Gatling Script:
To start writing your first Gatling Script first of all we need to create a package with the name simulation under SRC > test > scala as shown below.
Once you have created the simulations folder, we need to create a class under that particular folder and name it as you wish.
Now, you can start writing your script in that particular class. If you’re not aware of the general procedures to follow while writing your script, don’t be worried as we’ve got you covered.
The protocols to be followed for writing the scripts:
- The Package Name should be Mentioned
- Make sure to import all the important Gatling Classes
- The Class should extend the Simulation Class
- The script should consist of the below three integral parts
- HTTP Configuration
- Scenario
- Setup
- In the HTTP configuration part, we should provide the base URL to pass the header and the value.
- Here, we give the scenario a name and execute the request by providing the request URL and an assertion for the particular request.
- When it comes to the setup part, we must inject the number of concurrent users utilizing the setup method and pass the protocols.
The Script should be as follows:
package simulations; import io.gatling.core.scenario.Simulation import io.gatling.core.Predef._ import io.gatling.http.Predef._ class TestApiSimulation extends Simulation { //http conf val httpConf = http.baseUrl("https://reqres.in/") .header("Accept", value = "application/json") .header("content type", value = "application/json") //scenario val scn = scenario("get user") .exec(http("get user request") .get("https://reqres.in/api/users/2") .check(status is 200)) //setup setUp(scn.inject(atOnceUsers(1))).protocols(httpConf) }
Once the script is ready, you can run it by
- Clicking right on the ‘Engine’ option
- Click Run Engine
After you click on run Engine, you will see the script run and you will be asked to enter the description of the run in the console. Once you have entered the description, you will get the output in the console along with the report link. If you want to view the report, all you have to do is just copy the report link and paste it into any browser.
Global Information Results
Statics Report
Detail Report
Conclusion:
We hope you enjoyed reading our blog while learning how to install & use Gatling to load test an HTTP server and also how to create your own maven project. Using a graphical user interface, we can record a simulation based on a defined scenario, and once the recording is complete, we’ll be good to start our test.
Comments(0)