Measuring network metrics accurately across global edges requires more than just calling basic HTTP client methods. When microservices depend on real-time execution, small spikes in DNS resolution or TLS handshake times can degrade system telemetry.
In this guide, we'll break down how to structure a lightweight, asynchronous network performance testing suite in Python, containerized with Docker for multi-region deployment.
1. Deconstructing the Latency Timeline
When executing web requests, total round-trip time (RTT) is split into distinct network phases:
- DNS Resolution: Resolving domain names to IP addresses.
- TCP Connection: Completing the 3-way handshake.
- TLS Negotiation: Certificate exchange and encryption setup (for HTTPS).
- Time to First Byte (TTFB): Server processing and initial response transmission.
To get precise metrics, your monitoring engine must measure each segment independently rather than relying on standard wall-clock execution time.
2. Asynchronous Inspection in Python
Using Python’s aiohttp combined with high-resolution system timers (time.perf_counter()), we can isolate network stages:
python import asyncio import time import aiohttp async def inspect_endpoint(url: str): metrics = {} t0 = time.perf_counter() async with aiohttp.ClientSession() as session: t_connect = time.perf_counter() async with session.get(url) as response: t_first_byte = time.perf_counter() content = await response.read() t_done = time.perf_counter() metrics['connection_setup'] = round((t_connect - t0) * 1000, 2) metrics['ttfb'] = round((t_first_byte - t_connect) * 1000, 2) metrics['download_time'] = round((t_done - t_first_byte) * 1000, 2) metrics['total_ms'] = round((t_done - t0) * 1000, 2) metrics['status'] = response.status return metrics # Run standard check if __name__ == '__main__': result = asyncio.run(inspect_endpoint('[https://api.github.com](https://api.github.com)')) print("Telemetry Data:", result) 