DEV Community

Dennis Whalen for Leading EDJE

Posted on • Updated on

Automate your Load Testing with Gatling

I first got involved with automated load testing about 10 years ago. At that time I was working as a developer on a team building an enterprise application to be used by hundreds of concurrent users.

To prepare for load testing we first setup a production-like test environment. We then happily navigated to the home page to do some basic smoke testing before the real testing began. Unfortunately the home page would not load at all.

After some investigation, we realized we had SQL performance issues with just a single user, and our application needed to support hundreds of concurrent users.

This was my introduction to automated load testing and tuning, and helped me understand why automated load testing is so critical to delivering quality enterprise software.

What is Load Testing

Load testing validates performance-related non-functional requirements. Sample performance requirements could include:

  • The application must support up to 300 concurrent users
  • Response time must be less than 3 seconds 95% of the time
  • Response time must be less than 5 seconds 99% of the time

Other inputs to load testing could be the business transaction distribution. For example:

  • Searching inventory is 45% of all transactions
  • Placing a new order is 35% of all transactions
  • Checking order status is 10% of all transactions
  • Administrative tasks are 10% of all transactions

Based on these types of requirements, load testing tools enable us to design and develop automated processes to verify the application conforms to the requirements, and to identify areas that need attention.

Typically the purpose of the testing is to ensure the backend environment is designed, developed, and sized in the way that will allow the application to meet performance requirements.

To execute a 300 concurrent user load test, we don’t need to open 300 versions of a browser or mobile app. Instead, our load testing tool will simulate the client by sending the same API requests the client would send.
Alt Text

What is Gatling

Gatling is a popular load test tool with two versions, an open-source version that is free, and an Enterprise version, Gatling FrontLine. For this post I will be focusing on the free version.

Gatling is written in Scala so it can run on both Linux and Windows machines. Gatling is a code-based tool, which means you are writing Scala code to build your scripts. Gatling does not have an IDE; rather it’s a plug-in that will integrate with popular IDEs such as Visual Studio Code or IntelliJ IDEA.

Gatling is built utilizing the Akka toolkit, meaning Gatling does not need a separate thread for each user, allowing for a much larger workload to be generated on a single test machine as compared to tools like JMeter.

Gatling provides robust reports that allow you to analyze how your application is performing under load.

OK, enough talk, let’s start using Gatling!

Running a sample Gatling script

Download Gatling

  1. Access the Open Source page and click the Download Now button. As of this date, the current Gatling version is 3.3.1, so this will download the sample project: gatling-charts-highcharts-bundle-3.3.1-bundle.zip.
  2. Extract the downloaded zip file.

Run the sample script

  1. Gatling includes some sample scripts ready for you to execute. From a command line, navigate to the /bin folder of your unzipped working folder and execute ./gatling.sh (gatling.bat on Windows)
  2. Gatling will then ask you a couple questions before running the script. For now select simulation “0”, enter a run description if you like, and press enter. The script will start and run for about 25 seconds. If things go as planned you should see something like this: Alt Text

In addition to the stats in the command window, you’ll see a location for the HTML results report that you can open in a browser. It should look something like this:
Alt Text

So that’s it! You downloaded a sample gatling script, ran it, and viewed the results. If you want to take a look at the script you ran, you can find it at user-files/simulations/computerdatabase/BasicSimulation.scala. Open it with a text editor or your favorite IDE.

Now it’s time to create your own script!

Your First Script

For our first script will test a sample Gatling website. This site provides a list of computers in inventory, and allows you to add and change computers. We want a script that will view the list of all computers and then add a new one.

There are a couple options for creating Gatling scripts. One is to use the Gatling Recorder that is included with Gatling, and the other is to develop scripts from scratch. Since we're just getting started, let's start with the recorder.

Using the Gatling Recorder

To start the Gatling Recorder, from a command line navigate to the /bin folder of your unzipped working folder and execute ./recorder.sh (recorder.bat on Windows). The recorder utility will launch:
Alt Text

We're going to use this utility to generate a test script.

In the upper right corner there is a dropdown for Recorder Mode that has 2 options, HTTP Proxy and HAR Converter. For this post we are going to select HAR Converter, which uses a HTTP Archive File as input the the script generation process.

Generating a HAR File

A HAR file contains detail about the browser's interaction with network resources. The file is typically used to help troubleshoot performance issues, as it contains detailed performance data from the browser perspective. The HAR file is in JSON format and can be generated with Chrome, Firefox, IE, or Edge. We are going to generate a HAR file using Google Chrome.

Open Chrome, open Developer Tools, click the Network tab, and browse to https://computer-database.gatling.io/computers. We are going to generate a load test script for this site.

In Developer Tools, click the Clear button, then click the Preserve Log checkbox, which will allow your network activity to be captured as a HAR file.

Now we are going to step through our site and allow the browser to record the activity to the HAR file.

  1. Refresh the page
  2. Click the green "Add a new computer" button
  3. Fill out details for the new computer and click "Create this computer"
  4. Now we just need to save the HAR file. Right click anywhere in the network activity window and select "Save all HAR with content".

Now that you have a HAR file, you can use that in the recorder.

Generating the script with the HAR file

Back in the recorder, let's generate the script from the HAR file:

  1. For Recording Mode, select "HAR converter"
  2. For "Har File", select the HAR File that you saved previously
  3. For "Class Name", enter MyFirstGatlingScript
  4. Leave all other options as is, and click the Start button at the bottom right.

Hopefully you see something like this:
Alt Text

View the script

From the Gatling install folder, navigate to the user-files/simulations folder. You should see a file named "MyFirstGatlingScript.scala". It's a text file, so open it in your favorite text editor.

I'm not going to cover everything in the script, but there are a few points I'd like to highlight:

  1. About halfway into the script you should see some code like this:
    Alt Text

    Even if you're not familiar with Scala, this may seem fairly familiar. Here you can see the server calls that were made when you were stepping through the site to generate the HAR file:

    • request_0 is .get("/computers"), which was the display of list of computers.
    • request_1 is .get("/computers/new") which was the click of the "Add a new computer" button.
    • request_2 is .post("/computers") which is adding the new computer.
  2. After request_0 and request_1 you should see a pause statement, which will introduce a pause in the script to simulate user think time. The number of seconds to pause is based on how long you paused between steps when the HAR file was initially created. This data was automatically saved when the HAR file was created by Chrome. Realistic think time in critical to building realistic load test scenarios.

  3. The final point of interest is the last line:
    Alt Text

    This code is setting up how many concurrent users to run and how to add them to the test. For this example, we have just a single user that will be added immediately.

Run the script

Finally, let's run this new script. We'll follow the same patten we used to run the initial sample script:

  1. From the Gatling install folder, navigate to the bin folder
  2. Execute ./gatling.sh (gatling.bat on Windows)
  3. You should now see a new script in the list of available scripts, and that's the new script you just created: Alt Text
  4. Select your script and watch it run!

    Once the script ends you should see something like this:
    Alt Text

And there you have it. You create your first Gatling script and executed it!

This is a basic introduction to Gatling. In the real world you won't be able to rely on the recorder to do all the work.

You'll need a strategy for introducing test data, asserting API calls work as expected, including multiple business scenarios, logging, etc.

You are going to have requirements for your tests that will require you to edit the generated scripts, and you will likely need to build new scripts from scratch. In my next post I'll get into details like this and more, so stay tuned!


Smart EDJE Image

Top comments (1)

Collapse
 
antyadev profile image
Anton Moldovan

Nice write-up.
Regarding the load tests tool, I suggest considering NBomber, a .NET tool for load testing. It's a modern and flexible .NET load-testing framework for Pull and Push scenarios, designed to test any system regardless of a protocol (HTTP/WebSockets/AMQP, etc) or a semantic model (Pull/Push).