Introduction to Load Testing with Gatling – Part 2

By | April 24, 2016
Gatling logo, used with permission.

Gatling logo, used with permission.

This is the second part in a series of posts in which I will present a written version of a workshop I recently held on load testing with Gatling. The target audience of the workshop was Java developers without any previous knowledge of Gatling and this series of articles is not different in this area.
Since I myself like concrete examples, the workshop was based a number of examples. A project containing all the examples is available on GitHub.
If you are not familiar with Gatling, I recommend having a look at the first article in this series in which I introduced the basics of load testing with Gatling.

Example 2 – Multiple Requests in a Scenario

In the second example we are going to add one request to the scenario. In addition there will be a pause between the requests. The load simulation looks like this:

Note that:

  • As in the previous example, this class HttpSimulation2 also extends the Gatling class Simulation.
  • In the portion of the code that creates the scenario, there three method invocations on the scenario builder.
  • The first method invoked is the exec method.
    This method specifies, as before, an action to be taken at this step of the scenario. In this case the action is a HTTP GET request to the base URL.
    Recall that we found the requests to the root URL to be redirected in the previous article and causing two requests to be send from the Gatling load simulation.
  • The second method invoked is the pause method:
    pause(1, 5 seconds)
    In this example, there will be a pause of random length from one to five seconds between the HTTP requests.
    It is also possible to pause for a fixed duration and to have different time units of the start minimum and maximum values.
  • The third method is another exec method invocation which, as before, specifies the action to take at a step in the scenario.
    This HTTP GET request is to “/computers” relative to the base URL.

Run the simulation with the following command in a terminal window:

If you examine a simulation report, the one in the console or the graphical report, there should be three successful requests during the load simulation.

Example 3 – Multiple Users

The next example shows how to simulate multiple users performing the action(s) in one and the same scenario during a load simulation.

The structure of a Gatling load simulation should now be familiar to you.

Note that:

  • The difference is in the call to the setUp method where atOnceUsers(10) is injected into the scenario builder.
    This will cause 10 simulated users to begin executing the scenario “Scenario1” immediately when a load simulation is started.

Run the simulation with the following command in a terminal window:

If we look at the section Active Users along the Simulation of the graphical report, it will look like this:

Active users along the third load simulation.

Active users along the third load simulation.

We can see that the simulation starts out with 10 active users and, as they complete the scenario, the number of users decline.

Example 4 – Number of Users Increasing Over Time

In the previous example, we saw how to simulate multiple users performing some scenario. In this, the fourth, example we will look at how to simulate multiple users performing some scenario with the number of users being increased over some period of time.

Note that:

  • In the invocation of the setUp method the following construction is injected into the scenario builder:
    rampUsers(20).over(5 seconds)
    This will cause the number of users to rise from zero to 20 over a five second time period.

Run the simulation with this command in a terminal window:

Look at the section Active Users along the Simulation of the graphical report, which should look something like this:

simulation4-active_users

Active users along the fourth load simulation.

We can see that the simulations starts out with two active users, which gradually rises to five users. The number of active users is then constant at five users for a period of time before it starts to decline. The reason for the number of active users being constant is that new users are added with about the same rate as users finish executing the scenario.

Also examine the other graphs in the report, which will display some more variations compared to the reports we have seen earlier due to the number of users being larger and the ramping of the number of active users.

This concludes the second part of the introduction to load testing with Gatling. The third part can be found here.
Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *