← Load Test Directory

Checking Response Text and Times

Learn how to validate response content and ensure timely responses using LoadForge.

You are now browsing the LoadForge locust test directory. You can use these tests as a starting point for your own tests, or use our AI wizard to generate one automatically.

World

Overview

In typical scenarios, LoadForge would treat any valid HTTP response code as a success. However, there might be situations where you need to ensure that the response not only returns a valid HTTP code but also contains a specific string or conforms to a certain response time.

In this guide, we will illustrate how to ensure a response contains the word "Success" and also demonstrate how to flag a test as failed if the response time exceeds 0.5 seconds. Monitoring and validating actual response times and content can be crucial to ensure that your web application or service meets performance criteria.

Verifying Response Content and Timings

Here is an example that demonstrates how to monitor the actual timings around a response. Using LoadForge, you can inspect the response content, review headers, monitor timings, and more. This code snippet would report a failure in your LoadForge test if the response does not meet the set criteria.

Code Snippet

import time
from locust import HttpUser, task, between

class QuickstartUser(HttpUser):
    wait_time = between(3, 5)

    @task(1)
    def index_page(self):
        with self.client.get("/", catch_response=True) as response:
            if "Success" not in response.text:
                response.failure("Expected response content not found")
            elif response.elapsed.total_seconds() > 0.5:
                response.failure("Response took longer than expected")

Setting Request Timeouts

Another approach to ensure your tests are accurate and realistic is to set timeouts on your client requests. This ensures that if a response takes longer than the specified timeout, it's flagged as a failure.

Example with Timeouts

from locust import HttpUser, task, between

class QuickstartUser(HttpUser):
    wait_time = between(5, 9)

    @task(1)
    def index_page(self):
        self.client.get('/page1', timeout=10)  # A max of 10 seconds for this request
        self.client.get('/page2', timeout=5)   # A max of 5 seconds for this request

Locust Integration

LoadForge harnesses the power of locust, an open-source performance testing tool. This means locust users can readily leverage the above scripts for their own needs. If you're already using locust, consider migrating your script to LoadForge to enhance your testing capabilities and experience!

Ready to run your test?
Launch your locust test at scale.