Skip to content

Instantly share code, notes, and snippets.

@comerford
Last active May 17, 2023 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save comerford/7fdf9a9f53d273584ee7a2c31bbf8446 to your computer and use it in GitHub Desktop.
Save comerford/7fdf9a9f53d273584ee7a2c31bbf8446 to your computer and use it in GitHub Desktop.
Python script to ping the Agones UDP ping endpoint and return the RTT
import socket
import time
import sys
# Set the default port
DEFAULT_PORT = 7000
# Set the number of iterations used to calculate an average
ITERATIONS = 3
# Check if file path was provided as command-line argument
if len(sys.argv) > 1:
file_path = sys.argv[1]
else:
print("Please provide a file path containing a list of addresses.")
sys.exit(1)
# Read the addresses from the file
try:
with open(file_path, "r") as file:
addresses = [line.strip() for line in file if line.strip()]
except FileNotFoundError:
print("File not found:", file_path)
sys.exit(1)
# Set the payload message
MESSAGE = b"Hello Agones"
# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Set a timeout value for the socket
sock.settimeout(5)
# Iterate through each address
for add in addresses:
total_rtt = 0 # Initialize total RTT for averaging
# Perform iterations and measure round trip time
for _ in range(ITERATIONS):
# Send the UDP packet and measure round trip time
start_time = time.time()
sock.sendto(MESSAGE, (add, DEFAULT_PORT))
try:
data, server = sock.recvfrom(1024)
end_time = time.time()
rtt = (end_time - start_time) * 1000 # Calculate the RTT in milliseconds
total_rtt += rtt
print("Address:", add)
print("Round Trip Time: {:.2f} milliseconds".format(rtt))
print("Received:", data.decode())
except socket.timeout:
print("Address:", add)
print("Timeout: No response from the server")
# Calculate and print the average RTT
average_rtt = total_rtt / ITERATIONS
print("Address:", add)
print("Average Round Trip Time: {:.2f} milliseconds".format(average_rtt))
# Close the socket
sock.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment