This example demonstrates how to measure network round-trip time (RTT) between client and server using timestamped ping/pong messages. It’s a simple but essential tool for testing network performance.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Walkercito/repod/llms.txt
Use this file to discover all available pages before exploring further.
What This Example Demonstrates
- RTT measurement: Server timestamps outgoing pings and calculates round-trip time
- Ping/pong pattern: Client immediately echoes server pings back
- Automatic termination: Client disconnects after receiving 10 pings
- Minimal overhead: Demonstrates the low latency of repod’s networking
- Connection lifecycle: Shows proper connection initialization and cleanup
Complete Code
How to Run
On localhost, you should see RTT times under 1-2 milliseconds. Over a network, times will vary based on distance and network conditions.
How It Works
- Server initiates: When a client connects, the server immediately sends the first ping
- Client echoes: The client receives the ping and sends it back unchanged
- Server measures: The server compares the current time with the stored timestamp to calculate RTT
- Repeat: Server sends the next ping, continuing the cycle
- Auto-disconnect: After 10 pings, the client closes the connection
Key Takeaways
- Timestamp storage: Server stores send times in a list indexed by ping count
- Echo pattern: Client simply sends received data back without modification
- Per-channel state: Each channel tracks its own ping count and timestamps
- Graceful shutdown: Client properly closes the connection using
connection.close() - RTT calculation:
time.time() - stored_timegives accurate round-trip latency - Network testing: Useful for diagnosing connection quality and overhead
Extending This Example
You could enhance this example to:- Calculate average, min, max, and standard deviation of RTT
- Send pings at regular intervals instead of continuously
- Measure one-way latency using synchronized clocks
- Test with different message sizes to measure bandwidth
- Plot RTT over time to visualize network stability