Skip to content

Instantly share code, notes, and snippets.

@mtimkovich
Created January 12, 2021 03:08
Show Gist options
  • Save mtimkovich/f81097ef4d19c0e6a3a51f84bd8673e9 to your computer and use it in GitHub Desktop.
Save mtimkovich/f81097ef4d19c0e6a3a51f84bd8673e9 to your computer and use it in GitHub Desktop.
Test if our ping is good enough to play Rocket League
import platform
import re
import subprocess
from statistics import mean
def pings(*, url='google.com', count=10):
if platform.system() == 'Windows':
flag = '-n'
else:
flag = '-c'
result = subprocess.run(['ping', flag, str(count), url],
capture_output=True)
match = re.findall(r'time=([\d.]+)', str(result.stdout))
return sorted(float(n) for n in match)
def diagnose(ping):
# TODO: Packet loss.
errors = []
if ping[-1] - ping[0] >= 20:
errors.append('Latency Variation')
if ping[-1] >= 100:
errors.append('High Latency')
return errors
if __name__ == '__main__':
ping = pings()
errors = diagnose(ping)
print(f'Min = {ping[0]} ms, Max = {ping[-1]} ms, Avg = {mean(ping):.2f} ms')
if errors:
print(f"Errors: {', '.join(errors)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment