I'm focused on the CLI tool's core functionality, starting with the goal of benchmarking web server performance. I've begun to zero in on requests per second (RPS) and latency, which seem like essential metrics to capture initially. Next, I must consider a means of sending requests and tracking these metrics.
Refining Core Logic Design
\n\n
I'm now solidifying the core logic. I'll make the worker function send requests and measure response times, then use a concurrency limiter. The next task is figuring out how to output the statistics: RPS, latency, and success/error rates. I should design this with flexibility in mind, and also keep in mind a future plan to incorporate more advanced statistical analysis.
Implementing Async Requests
\n\n
I'm making progress, and I've sketched out the initial async request function using aiohttp. I've also incorporated the semaphore to control concurrency. Right now, I'm working to flesh out the main loop for calling the fetch function and collecting the metrics. I'm focusing on handling errors gracefully and keeping the code readable, so I can adapt and extend it later.
Planning Feature Enhancements
\n\n
I'm now contemplating additional features. I'm focusing on adding a progress bar, most likely tqdm, to enhance user experience. I'm also planning to incorporate more robust error handling and modularize the components to improve code structure and maintainability, so that the code can handle errors like bad URLs and connection drops more effectively.
Defining Request Metrics and Output
\n\n
I'm now diving into the specifics of measuring latency (min, max, and percentiles), RPS, and error rates. Designing the output format, I need to balance brevity with the inclusion of helpful information. Considering the statistics module for initial calculations, and I'll think about how to structure the output to be easy to read in a terminal window, or maybe in a future iteration, configurable for CSV output.
Developing Tool Architecture
\n\n
My current thinking centers on designing the tool's architecture. I'm focusing on a class-based structure, which I find will make passing state cleaner. I'm thinking of incorporating a LoadTester class to manage requests and a StatsCalculator class to process the results. This helps modularize the distinct parts. The key will be ensuring that this structure is clear and scalable.
Here is a complete, robust Python CLI tool for benchmarking web server performance.
Unlike simple synchronous scripts, this tool uses asyncio and aiohttp. This allows it to open thousands of concurrent connections without blocking, providing a true stress test similar to tools like Apache Bench (ab) or wrk.
Prerequisites
You will need to install aiohttp to handle asynchronous HTTP requests.
bash
pip install aiohttp
The Python Code (pybench.py)
Save the following code into a file named pybench.py.
python
How to Use It
Basic Usage:
Run 100 requests with a concurrency of 10 (simulating 10 active users).
Asyncio & Aiohttp:
Standard Python requests is "blocking" (synchronous). If the server takes 1 second to reply, the script does nothing for 1 second. With asyncio, the script fires the request and immediately moves to the next task while waiting for the response. This allows Python to generate tremendous load from a single CPU core.
Semaphore:
We use asyncio.Semaphore(concurrency). Without this, if you asked for 10,000 requests, the script would try to open 10,000 socket connections instantly, likely crashing your OS or the script before the server even feels it. The semaphore acts as a gatekeeper, ensuring only N requests are active at any specific millisecond.
Metrics:
RPS (Requests Per Second): Use this to measure throughput.
P99 Latency: This is arguably more important than Average. It tells you: "99% of my users saw a speed faster than X". It helps identify outliers and "stuttering."