Track User Access Stats in Locust: Real‑Time Dashboard, CSV Export, Custom Metrics
Locust provides a built‑in web UI for real‑time monitoring of metrics such as RPS, average response time, failures, and active users, supports headless CSV export for deeper analysis, and allows custom statistics via event listeners in your test scripts, enabling comprehensive user access reporting.
Locust includes a built‑in web interface (default at http://localhost:8089) that displays real‑time statistics during a load test, such as Requests Per Second (RPS), average response time, number of failed requests, and the count of currently active users.
To start Locust and open the dashboard, run: locust -f your_locustfile.py Then open the URL in a browser, set the desired number of concurrent users and spawn rate, and click “Start swarming” to begin the test.
Export Test Results as CSV
Locust can run in headless mode and export results to CSV files for offline analysis using the --csv option. Example command:
locust -f your_locustfile.py --headless -u 100 -r 10 -t 1m --csv=resultsThis runs a test with 100 users, spawning 10 users per second for 1 minute, producing results_stats.csv (summary statistics) and results_stats_history.csv (time‑series data such as RPS and response time).
Customize Statistics in Your Locust Script
If the default metrics are insufficient, you can add custom logic by registering event listeners in your test script. The example below shows how to record success/failure counts for a specific endpoint and print details via listeners.
from locust import HttpUser, task, between, events
class WebsiteUser(HttpUser):
wait_time = between(1, 5)
@task
def my_task(self):
with self.client.get("/my_endpoint", catch_response=True) as response:
if response.status_code != 200:
response.failure("Got wrong response")
else:
response.success()
# Define event handlers
@events.test_start.add_listener
def on_test_start(environment, **kwargs):
print("Test Started")
@events.request_success.add_listener
def on_request_success(request_type, name, response_time, response_length, **kwargs):
print(f"Request succeeded: {name}, Response time: {response_time}")
@events.request_failure.add_listener
def on_request_failure(request_type, name, response_time, exception, **kwargs):
print(f"Request failed: {name}, Exception: {exception}")This script not only sends requests but also marks them as success or failure based on the status code and registers listeners that output detailed information for each request.
Analyze Exported Data
The generated CSV files can be opened in Excel or any data‑analysis tool. You can create charts to visualize RPS trends, average response times, and other metrics over time, helping you understand system behavior under load.
Conclusion
By combining Locust’s real‑time web UI, CSV export capability, and custom event‑driven statistics, you can comprehensively monitor user access patterns, capture detailed performance data, and perform in‑depth analysis to accurately assess system performance.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
