This Python script measures the average latency between block confirmation and event reception from a Base blockchain RPC endpoint using WebSocket methods.
- Real-time Monitoring: Uses WebSocket connections for real-time block and event monitoring
- Latency Measurement: Calculates precise latency between block confirmation and event reception
- Statistical Analysis: Provides comprehensive statistics including mean, median, min, max, and standard deviation
- Detailed Logging: Comprehensive logging for debugging and monitoring
- Configurable: Customizable endpoint, duration, and block limits
- Clone or download this repository
- Install the required dependencies:
pip install -r requirements.txt
Run the script with default settings (5 minutes test on mainnet):
python base_latency_test.py
# Test for 10 minutes on mainnet
python base_latency_test.py --duration 600
# Test on a custom endpoint
python base_latency_test.py --endpoint wss://your-base-endpoint.com
# Test for 2 minutes on testnet
python base_latency_test.py --endpoint wss://goerli.base.org --duration 120
# Test with maximum block limit
python base_latency_test.py --duration 300 --blocks 50
--endpoint
: Base RPC WebSocket endpoint (default:wss://mainnet.base.org
)--duration
: Test duration in seconds (default: 300)--blocks
: Maximum number of blocks to test (optional)
- Connection: Establishes WebSocket connection to the Base RPC endpoint
- Subscription: Subscribes to new block headers (
newHeads
) and logs/events - Monitoring: Listens for incoming messages and tracks:
- Block confirmation timestamps
- Event reception timestamps
- Calculates latency between them
- Statistics: Computes comprehensive latency statistics
- Reporting: Displays detailed results with measurements
============================================================
BASE BLOCKCHAIN LATENCY TEST RESULTS
============================================================
Total Measurements: 15
Average Latency: 245.67ms
Median Latency: 238.45ms
Min Latency: 189.23ms
Max Latency: 312.89ms
Standard Deviation: 34.56ms
Detailed Measurements:
------------------------------------------------------------
Block #12345678 | Hash: 0x1a2b3c4d5e... | Latency: 234.56ms | Time: 2024-01-15 14:30:25
Block #12345679 | Hash: 0x6f7g8h9i0j... | Latency: 267.89ms | Time: 2024-01-15 14:30:30
...
============================================================
- Mainnet:
wss://mainnet.base.org
- Testnet:
wss://goerli.base.org
- Custom: Any WebSocket-enabled Base RPC endpoint
eth_subscribe
withnewHeads
- Subscribe to new block headerseth_subscribe
withlogs
- Subscribe to contract events/logseth_getBlockByHash
- Get detailed block information
The script measures latency as:
Latency = Event Reception Time - Block Confirmation Time
- Automatic reconnection on connection failures
- Graceful handling of malformed messages
- Timeout protection for message reception
- Comprehensive error logging
- Connection Failed: Check if the endpoint is correct and accessible
- No Events Received: Some blocks may not contain events; the script will continue monitoring
- High Latency: Network conditions or endpoint performance may affect latency
Enable debug logging by modifying the logging level in the script:
logging.basicConfig(level=logging.DEBUG)
- Python 3.7+
websockets
library- Internet connection to Base RPC endpoint
This project is open source and available under the MIT License.