client


Overview

The **`client`** file is a lightweight asynchronous HTTP load testing script. Its primary purpose is to measure the responsiveness and throughput of an HTTP server running locally by issuing a rapid sequence of HTTP GET requests to the server’s root endpoint. It runs for a fixed duration (5 seconds) and reports the total number of requests completed in that interval.

This script uses Python’s `asyncio` for asynchronous concurrency and the `httpx` library’s asynchronous client to maximize request throughput without blocking. It is designed to be used alongside an HTTP server (such as the Flask app example in the integration) to validate server performance under load.


Detailed Explanation

Global Variables

Variable

Description

`port`

The port number of the target HTTP server, passed as a command-line argument.

`url`

The full URL constructed using the port, targeting `http://127.0.0.1:`.

timeout

An `httpx.Timeout` object setting a 5-second timeout per request to avoid stalling.

`client`

An instance of `httpx.AsyncClient` configured with the timeout for sending async HTTP requests.

`stop_time`

The absolute timestamp 5 seconds in the future when the test should stop.

`TEST_MESSAGE`

A constant string message displayed to the user indicating test progress.


Function: async def main()

Purpose

Runs the asynchronous load test by sending repeated GET requests to the target URL until the timeout (`stop_time`) is reached. It keeps count of successful requests and prints results.

Parameters

Return Value

Behavior

  1. Writes the startup message TEST_MESSAGE to standard output and flushes to ensure immediate display.

  2. Initializes a counter count to zero.

  3. Enters a loop that runs until the current time exceeds stop_time.

  4. On each iteration:

    • Sends an asynchronous GET request to the url using the shared client.

    • Increments the count of successful requests.

  5. After the loop ends, outputs a carriage return (\r) to overwrite the prior message line and prints a summary message with the total requests made.

Usage Example

$ python3 client 8001
http test running... ok, 12345 requests made

Here, the client targets a server running on localhost port 8001 and runs for 5 seconds, printing how many requests were completed.


Implementation Details


Interaction with Other System Components


Mermaid Diagram

This class-free file centers around a single asynchronous function and global variables. A flowchart showing the main execution workflow and function relationships is appropriate:

flowchart TD
    Start["Start Script"] --> ParsePort["Parse port from sys.argv"]
    ParsePort --> Setup["Setup URL, Timeout, AsyncClient"]
    Setup --> SetStopTime["Calculate stop_time (now + 5 sec)"]
    SetStopTime --> MainFunc["Run async main()"]

    subgraph AsyncMain [async def main()]
        direction TB
        PrintStart["Print TEST_MESSAGE to stdout"]
        LoopCheck["while time < stop_time"]
        SendRequest["await client.get(url)"]
        IncrementCount["count += 1"]
        LoopCheck -->|True| SendRequest --> IncrementCount --> LoopCheck
        LoopCheck -->|False| PrintEnd["Print final count message"]
    end

    MainFunc --> End["Close event loop and exit"]

Summary

The **`client`** file is a concise asynchronous HTTP load generator that sends as many HTTP GET requests as possible to a given local server within 5 seconds. It leverages `asyncio` and `httpx.AsyncClient` for high throughput, providing a simple but effective way to measure server responsiveness and request handling capacity. This script is a key component in an HTTP server integration testing suite, facilitating performance benchmarking and validation of JSON serialization under concurrent load.


Appendix: How to Run

python3 client <port>

End of Documentation for client