The Art Of Destroying Your Web App With Gatling

7 min read
Mar 7, 2018 3:16:00 PM

The Art of Destroying your web app

Warning: Gatling could harm your application; use it with caution!

But don't worry! This article is a short introduction to Performance Testing with Gatling: You will learn the basics of performance testing and how to start using Gatling. If you have ever experienced performance issues or if moving to production is your worst nightmare, this article is made for you!

What does performance testing mean?

A performance test lets you measure the performance and robustness of your application. The aim is to simulate many concurrent users as close as possible to real cases. You can then anticipate crashes and have a more precise vision of the response times of all your users, especially those with the worst response times.

There are different types of performance tests:

Load test: Simulating an expected load of users

Load Test

 

Capacity test: Increasing step by step the number of users during the simulation to know the limits of your system

Capacity Test

 

Stress test: Simulating a very high number of users using your application at the same time

Stress Test

 

Soak test: Simulating users over a long period to detect any performance degradations over time.

Soak Test

 

Why testing as soon as possible is important

Development teams have contacted us at Gatling Corp because they need performance tests. But sometimes, they contact us before moving into production or even after a first crash. In the first case, there won't be enough time to fix the performance issues we could detect; in the second case, a crash already happened.

It doesn't mean that testing in these cases is useless; quite the contrary: you will identify your app's bottlenecks. However, there is a more efficient way to do performance testing, starting from the beginning of the development cycle. There is no need to wait!

The sooner the performance problems are detected, the sooner we can fix them. It saves money and time and improves the quality of your application.

Performance tests are also useful in determining the infrastructure required to move your application to production.

 

Gatling, by Gatling Corp

Gatling is a load and performance testing tool for web applications. Gatling Corp develops it and works on all Operating Systems.

Gatling makes it easy to script performance tests and maintain them throughout the development of your application. Gatling is a powerful tool that generates a very high number of users from a single load injector. Gatling provides you with dynamic, exhaustive, and colorful HTML reports.


Testing with Gatling

Gatling's simulations are Scala files. However, a prior knowledge of Scala is NOT required: Gatling comes up with a simple DSL (Domain Specific Language) available in Java and Kotlin to script your simulations.

You can also use Gatling's recorder to bootstrap your simulations. It will record all your actions on your website and create a ready-to-use Gatling simulation.

 

Running a Gatling test

Once you finish the scripting part, you can run it with Gatling's bundle or directly from your build tool (Maven, Sbt, or Gradle).

Let's test this website with a Gatling simulation to show you how Gatling works. This website is a basic CRUD application ("Create, Read, Update, Delete"). It is designed to manage a stock of computers.

 

HTTP Configuration

The first step is to deal with the HTTP configuration. Here, you can define the base URL of all your requests and the HTTP headers sent with each of these requests:

val httpConf = http
 .baseURL("http://computer-database.gatling.io")
// Here is the root for all relative URLs
 .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
// Here are the common headers

 

Scripting requests

Let's create our scenario, named "BasicSimulation":

val scn = scenario("BasicSimulation")

 

Now, let's add a simple GET request:

val scn = scenario("BasicSimulation")
 .exec(http("Get Computers").get("/computers"))

 

These keywords are important:

  • exec means we are performing an action
  • http indicates that we are dealing with an HTTP request named "Get Computers"
  • get means the GET request is on the endpoint "/computers"

 

Now, let's see how to add a new computer to our stock:

val scn = scenario("BasicSimulation")
 .exec(http("Get Computers").get("/computers"))
 .exec(http("Add Computer").post("/computers")
  .formParam("name", "Beautiful Computer")
  .formParam("introduced", "2012-05-30")
  .formParam("discontinued", "")
  .formParam("company""", "37"))

 

This time, we use the POST method to indicate that we want a POST-type request. We can then define the parameters using the method "formParam" (for instance, the parameter "name" value is "Beautiful Computer").

In our scenario, Gatling will send GET and POST requests. However, it doesn't match a realistic user's behavior. A user will take time to read and scroll down the page before clicking on a button. Let's add a pause of 5 seconds between the requests:

val scn = scenario("D2siSimulation")
 .exec(http("Get Computers").get("/computers"))
 .pause(5)
 .exec(http("Add Computer").post("/computers")
  .formParam("name", "Beautiful Computer")
  .formParam("introduced", "2012-05-30")
  .formParam("discontinued", "")
  .formParam("company""", "37"))

 

Setup

Let's combine our HTTP configuration and the requests we just crafted to create a functional Gatling script. We need to add an injection profile of 100 users at once.

setUp(scn.inject(atOnceUsers(100)).protocols(httpConf))

 

You need to start with the keyword setUp. Then, use the keywords inject and atOnceUsers to define how many users you want in your system. And don't forget to mention the HTTP configuration you use with the keyword protocols.

With Gatling, it is also possible to shape more complex injection profiles: You can have users arriving at a constant rate, stage by stage, or at specific times.

 

Assertions = acceptance criteria

Gatling is now able to run your scenario with 100 users. But now we want to make sure that every request has a response time lower than 2 seconds: Gatling can check that this assumption is valid. It is called an assertion in Gatling's DSL:

setUp(scn.inject(atOnceUsers(100)).protocols(httpConf))
 .assertions(global.responseTime.max.lt(2000))

 

Insert the keyword assertions after the setup! In this case, the maximum response time should be lower than 2000 milliseconds, or 2 seconds.

 

Last step

The last step is to combine all the pieces we previously crafted. We need to put our script inside a class, which extends the Simulation class:

class D2siSimulation extends Simulation {
 val httpConf = http
  .baseURL("http://computer-database.gatling.io")
  // Here is the root for all relative URLs
  .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
  // Here are the common headers
<
 val scn = scenario("BasicSimulation")
  .exec(http("Get Computers").get("/computers"))
  .pause(5)
  .exec(http("Add Computer").post("/computers")
   .formParam("name", "Beautiful Computer")
   .formParam("introduced", "2012-05-30")
   .formParam("discontinued", "")
   .formParam("company", "37"))
<
 setUp(scn.inject(atOnceUsers(100)).protocols(httpConf))
  .assertions(global.responseTime.max.lt(2000))
}

The Test Results

To have more interesting test results, I slightly changed the injection profile configuration but not the requests' configuration.

You can see the results in real-time in the console when you run the test. You can check the progress of your simulation with a few indicators regarding the requests and the users:

Results of the simulation

At the end, Gatling will generate an HTML report. It will provide you with more information, especially the response times of all your requests.

Gatling Enterprise report screenshotOr even how the number of users changes during the simulation:

user arrival rateAnd the response times and their distribution over time:

response time percentilesAnd the number of requests and responses:

request and responses per second

 

The Test Results with Gatling Enterprise

Gatling Corp also edits Gatling Enterprise, Gatling's paid version. It is a web app that allows you to manage your Gatling tests easily. You can also manage clusters of load injectors (to simulate a very high number of users). Gatling Enterprise also provides more metrics (TCP connection, bandwidth, DNS resolution, injectors' monitoring).

You can see below a screenshot of the results of our Gatling simulation:

Results of the Gatling simulation


Now, test your app!

You know a performance test and how to create a simulation with Gatling. You can now make sure that your web app won't crash! No need to cross your fingers next time you move into production!

We will be back shortly for a new article presenting more advanced aspects of Gatling! Stay tuned.

 

Useful Links

If you want to: