Introduction to Load Testing with Gatling – Part 3

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

Gatling logo, used with permission.

This is the third 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.
Earlier articles in the series are: Part 1 and part 2.

Example 5 – Body, Headers and Parameters

In the fifth example we will look at two different ways to set HTTP headers on the requests sent, how to set a body when doing a HTTP POST and finally how to add parameters to the URL.

Note that:

  • When creating the HTTP protocol builder object, three method calls are chained after the invocation of the baseURL method.
    Rows 22, 23 and 24 in the listing above. These methods set HTTP header values that are to be enclosed with requests sent by the load simulation.
    The acceptHeader call specifies what will be enclosed with the Accept HTTP header, the acceptCharsetHeader what will be enclosed with the Accept-Charset HTTP header and the acceptEncodingHeader what will be enclosed with the Accept-Encoding HTTP header.
    Setting HTTP headers this way will cause them to be enclosed with every request issued using the protocol builder.
  • On rows 26 to 28 there is Scala code that creates a map containing HTTP header values for the Accept and Accept-Encoding HTTP headers.
    Using a map we can enclose a set of HTTP headers with a request, as can be seen on row 37.
  • There is a immutable variable named theBody of the type Body initialised with a StringBody object containing the string “Farty towels!”.
    Body data may be included with POST requests from different types of sources. This example shows how to enclose string data in a StringBody object, but there are InputStreamBody, RawFileBody, ByteArrayBody and CompositeByteArrayBody that retrieves body data from an input stream, a file specified by a filepath, a single byte array or a list of byte arrays.
  • In the Scenario1 scenario we can see that the “Login and Post Data” request is a POST request to /computers relative to the base URL.
  • Following the post method call, there is a call to the body method with the theBody object parameter.
    The body method is used to specify the body that is to be included with the HTTP POST request.
  • Chained after the body method call is a call to the headers method taking the map in the variable theCommonHeaders as parameter.
    As before, this is how to specify the HTTP headers for one single request. Using different maps, we can enclose a different set of headers for each of the request in a scenario if we so desire. It is also possible to set individual headers using the header method.
  • Finally on rows 38 and 39 there are two calls to the queryParam method.
    This will cause two query parameters, “login” and “password”, to be added to the request URL with the values “admin” and “secret” respectively.
    The resulting request URL will look like this: computer-database.gatling.io/computers?login=admin&password=secret

Run the simulation with the following terminal command:

The result will be that the single request sent by the simulation fails:

The reason for this is that POST requests to the URL in question are not allowed.

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

Leave a Reply

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