Verify system performance under load to prevent bottlenecks
Preconditions (3)
✅ Expected Result: The system maintains response times under 2 seconds and error rate below 1% throughout the test, indicating no bottlenecks
from locust import HttpUser, task, between, events class WebsiteUser(HttpUser): wait_time = between(1, 2) @task def load_main_page(self): with self.client.get("/", catch_response=True) as response: if response.status_code != 200: response.failure(f"Failed with status {response.status_code}") @events.test_stop.add_listener def on_test_stop(environment, **kwargs): stats = environment.runner.stats avg_response_time = stats.total.avg_response_time error_rate = stats.total.fail_ratio * 100 assert avg_response_time < 2000, f"Average response time too high: {avg_response_time} ms" assert error_rate < 1, f"Error rate too high: {error_rate}%"
This script uses Locust to simulate users visiting the main page.
The WebsiteUser class defines user behavior with a wait time between requests.
The load_main_page task sends a GET request to the home page and marks failure if status is not 200.
At test stop, the on_test_stop listener checks average response time and error rate.
Assertions ensure the system meets performance criteria, preventing bottlenecks.
Now add data-driven testing with 3 different URLs to simulate varied user behavior
users = 1000
response_times = [0.2, 0.3, 0.5, 0.4, 0.6]
avg_response = sum(response_times) / len(response_times)
if avg_response > 0.4:
result = "Bottleneck detected"
else:
result = "Performance acceptable"result after running this code?Pass the test and this pattern moves unknown