Elevate Your Load Testing Game: A Guide to Gatling Assertions for Response Verification and Performance Metrics

Harshit Shah
4 min readJul 26, 2024

--

As a Quality enthusiast with focus on performance I’ve found Gatling to be an incredibly powerful tool for load testing and performance validation. One of the key capabilities of Gatling is its robust assertion framework, which allows you to verify the responses from your application under test and ensure that the average response times are within acceptable thresholds.

Verifying Response Content

Gatling provides several ways to assert the content of the responses you receive during your load tests. The most common approach is to use the check() method to validate the response body.For example, let's say you have a JSON response that should contain a specific value for the result field. You can use the jsonPath() function to extract that value and assert it as below.

This will ensure that the result field in the JSON response is equal to "success".

You can also use regular expressions to validate the response content as below.

Verifying Response Status Codes

Checking the HTTP status code of the responses is another common use case for Gatling assertions. You can use the status() function to assert that the status code is within an expected range.

This will ensure that the response has a 200 OK status code.

You can also check for specific error codes, such as 4xx or 5xx responses:

Verifying Average Response Times

One of the key performance metrics you’ll want to monitor during your load tests is the average response time. Gatling makes it easy to assert that the average response time is within a specified threshold. Here’s an example of how to do this.

This will ensure that the response time for the “Get Data” request is less than 500 milliseconds.

You can also check the overall average response time for all requests in your scenario:

Above will assert that the maximum response time across all requests is less than 2 seconds, and the average response time is less than 500 milliseconds.

Verifying Successful Request %

Below is the way on how we can check success rate should be greater than 95%.

Above assertion help in evaluating the overall health and reliability of your API during a load test. Let’s break the above compoment as below which hold true for most of the above cases too.

Global Scope:

  • global: This keyword specifies that the assertion applies to all requests executed within the current scope (usually the Thread Group). This means it evaluates the success rate across all simulated users.

Metric and Statistic:

  • successfulRequests.percent: This defines the metric and statistic being evaluated.
  • successfulRequests: This refers to the total number of requests that received a successful response code (typically 2xx codes).
  • .percent: This calculates the percentage of successful requests out of the total number of requests executed.

Comparison Operator and Threshold:

  • .gt(95): This defines the comparison logic and threshold for success.
  • .gt: This operator stands for "greater than."
  • (95): This sets the minimum acceptable success rate as 95%. In other words, at least 95% of all requests must receive successful response codes for the test to pass this assertion.

Putting it All Together

Here’s an example of a complete Gatling scenario that demonstrates how to use these assertion techniques.

In this example, we have achieved below

  1. Verify that the login request returns a 200 OK status code and extracts the authentication token from the response.
  2. Verify that the “Get Data” request returns a successful response (with a “success” value for the result field) and has a response time less than 500 milliseconds.
  3. Assert that the overall maximum response time is less than 2 seconds and the average response time is less than 500 milliseconds.

--

--

Harshit Shah
Harshit Shah

Written by Harshit Shah

Passionate about Testing and learning new tools and technologies.https://harshitautomation.github.io/

No responses yet