Update 2020-04-07: This article has been updated for Gatling 3.3.1 as described here.
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:
package se.ivankrizsan.gatling.simulations import io.gatling.core.Predef._ import io.gatling.core.structure.ScenarioBuilder import io.gatling.http.Predef._ import io.gatling.http.protocol.HttpProtocolBuilder import io.gatling.http.request.builder.HttpRequestBuilder.toActionBuilder import scala.concurrent.duration._ /** * Example Gatling load test that sends two HTTP GET requests with a short pause between. * The first request will be redirected, again making it look like there were two requests sent. * The second request will not be redirected. * Run this simulation with: * mvn -Dgatling.simulation.name=HttpSimulation2 gatling:test * * @author Ivan Krizsan */ class HttpSimulation2 extends Simulation { val theHttpProtocolBuilder: HttpProtocolBuilder = http .baseUrl("https://computer-database.gatling.io") /* * This scenario consists of two GET requests; one to the base URL and one to /computers relative * to the base URL. * Between the requests there will be a pause for five seconds. * Note that in order to get access to different durations, we must add the following import: * import scala.concurrent.duration._ */ val theScenarioBuilder: ScenarioBuilder = scenario("Scenario1") .exec( http("GET to base URL") .get("/")) .pace(4 seconds) .exec( http("GET to /computers") .get("/computers")) setUp( theScenarioBuilder.inject(atOnceUsers(1)) ).protocols(theHttpProtocolBuilder) }
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:
mvn -Dgatling.simulation.name=HttpSimulation2 gatling:test
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.
package se.ivankrizsan.gatling.simulations import io.gatling.core.Predef._ import io.gatling.core.structure.ScenarioBuilder import io.gatling.http.Predef._ import io.gatling.http.protocol.HttpProtocolBuilder import io.gatling.http.request.builder.HttpRequestBuilder.toActionBuilder /** * Example Gatling load test simulating ten users that each sends one single HTTP GET request. * All the users will start sending requests immediately when the simulation is started. * Run this simulation with: * mvn -Dgatling.simulation.name=HttpSimulation3 gatling:test * * @author Ivan Krizsan */ class HttpSimulation3 extends Simulation { val theHttpProtocolBuilder: HttpProtocolBuilder = http .baseUrl("https://computer-database.gatling.io") val theScenarioBuilder: ScenarioBuilder = scenario("Scenario1") .exec( http("myRequest1") .get("/computers")) setUp( /* * Here we specify that ten simulated users shall start sending requests immediately * in the Scenario1 scenario. */ theScenarioBuilder.inject(atOnceUsers(10)) ).protocols(theHttpProtocolBuilder) }
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:
mvn -Dgatling.simulation.name=HttpSimulation3 gatling:test
If we look at the section Active Users along the Simulation of the graphical report, it will look like this:
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.
package se.ivankrizsan.gatling.simulations import io.gatling.core.Predef._ import io.gatling.core.structure.ScenarioBuilder import io.gatling.http.Predef._ import io.gatling.http.protocol.HttpProtocolBuilder import io.gatling.http.request.builder.HttpRequestBuilder.toActionBuilder import scala.concurrent.duration._ /** * Example Gatling load test simulating a number of users that rises up to 10 users over * a period of 20 seconds. * Run this simulation with: * mvn -Dgatling.simulation.name=HttpSimulation4 gatling:test * * @author Ivan Krizsan */ class HttpSimulation4 extends Simulation { val theHttpProtocolBuilder: HttpProtocolBuilder = http .baseUrl("https://computer-database.gatling.io") val theScenarioBuilder: ScenarioBuilder = scenario("Scenario1") .exec( http("myRequest1") .get("/")) setUp( /* * Increase the number of users that sends requests in the scenario Scenario1 to * ten users during a period of 20 seconds. */ theScenarioBuilder.inject(rampUsers(20).during(5 seconds)) ).protocols(theHttpProtocolBuilder) }
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:
mvn -Dgatling.simulation.name=HttpSimulation4 gatling:test
Look at the section Active Users along the Simulation of the graphical report, which should look something like this:
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!