Executing performance tests using Gatling is just the beginning; truly effective performance testing goes beyond simply running simulations and reviewing response times or throughput. To ensure that your application behaves correctly under load and delivers the expected results, it's crucial to incorporate checks and assertions into your test scenarios. These mechanisms allow you to validate not just performance metrics, but also the functional accuracy of responses—such as verifying status codes, response content, and specific data values. By using Gatling's built-in capabilities for checks and assertions, you can detect subtle issues, ensure data integrity, and gain confidence that your system performs reliably under various load conditions. This adds a critical layer of validation to your performance testing strategy, making your tests more robust, insightful, and trustworthy.
Checks in Gatling
Checks in Gatling are a fundamental part of performance testing, allowing you to validate server responses and extract necessary data during test execution. These checks are crucial for ensuring that your application behaves correctly, even when subjected to heavy load. They are typically defined within the chain builder, where they are executed alongside HTTP requests to verify the correctness of the response.
You can use checks to perform a variety of important tasks, including:
-
Verifying that the HTTP status code matches the expected value.
-
Extracting and validating fields from JSON responses to ensure data accuracy.
-
Confirming that the response time remains within an acceptable performance threshold.
-
Validating specific headers in the response for security or content checks.
-
Extracting values from the response and storing them in the session for use in later requests.
Gatling also provides useful helpers:
-
Use
optional()
to mark a check as non-mandatory—helpful when the data may or may not be present. -
Use
checkIf()
to apply a check conditionally, based on specific logic or prior responses.
These features make checks not only a validation tool but also a dynamic way to adapt your test flow based on server behavior.
Let’s explore some practical examples of how to use these checks effectively in your Gatling scenarios.
public ChainBuilder getCarById(String id) {
return exec(http("Get car")
.get(cars + "/" + id)
.check(status().is(HttpResponseStatus.OK.code()))
.check(jmesPath("brand").is("ford")) // verifies value of brand from json response is ford
.check(jmesPath("brand").saveAs("brand")) // saves value in session variable brand
.check(responseTimeInMillis().lte(2000)) // response time is<= to 2 seconds
.check(header("Content-Type").is("application/json")) // verifies value of response header
.check(jmesPath("brand").optional().saveAs("ford")) // makes the brand check optional
.checkIf(session -> session.getString("brand").contains("ford")) //if the value of session variable
.then(jmesPath("mobile").saveAs("mobileNumber"))); // is ford then saves mobile number
}
Assertions in Gatling
Assertions in Gatling
Assertions in Gatling serve as a vital post-execution validation mechanism, allowing you to confirm whether the system under test has met its performance goals. Unlike checks—which validate individual responses during the test—assertions evaluate the overall results once the simulation has completed. This is crucial for determining whether your application has maintained the desired level of performance under the defined load conditions.
You can use assertions to assess metrics such as global response time, error rates, throughput, number of successful or failed requests, and more. Gatling offers a robust set of built-in assertions, including validations for maximum, mean, and percentile-based response times. You can also define custom assertions tailored to specific business or technical requirements, giving you greater flexibility in your testing strategy.
Key Features of Assertions:
✔ Performed after the simulation ends—ideal for global performance evaluation.
✔ Declared within the setUp()
block of the simulation.
✔ Can validate a wide range of metrics: response times, failure rates, request counts, and more.
✔ Provide a pass/fail outcome to help you quickly assess whether performance targets have been achieved.
✔ Easily integrated into CI/CD pipelines for automated performance gatekeeping.
Let’s now dive into how assertions are implemented in Gatling and explore some examples that demonstrate their usage in real-world scenarios.
{
setUp(
scn.injectOpen(
constantUsersPerSec(1 * multiplier).during(loadDuration)
).protocols(httpProtocol)
).assertions(
global().responseTime().mean().lt(2000), // mean response time is less than 2 sec
global().responseTime().percentile4().lt(1800), // 99 percentile is less than 1.8 sec.
global().failedRequests().percent().lte(3.0), // failed requests are less than 3 %
global().successfulRequests().percent().gt(98.0), // successful requests > 98%
global().requestsPerSec().gt(10.0) // requests per sec are greater than 10 per sec
);
}
Assertions and checks play a critical role in performance testing, helping to verify that your application behaves correctly and meets its performance goals. With Gatling, these features are seamlessly integrated into your testing framework, enabling you to validate key metrics such as response times, success rates, and other custom business logic. This ensures that your system not only handles traffic under load but also delivers the expected results for end-users.
By incorporating both assertions and checks into your test scenarios, you gain valuable insight into how your application behaves under various conditions, giving you the confidence that it will perform reliably in production environments. Additionally, these validations help you identify any issues early, before they impact your users.
So, take the time to implement assertions and checks in your Gatling tests to enhance your load testing process and achieve better, more accurate results.
That wraps up our article! Best of luck with your load testing endeavors! 🚀