Skip to content

Instantly share code, notes, and snippets.

@loyd
Created February 12, 2019 14:33
Show Gist options
  • Save loyd/0f0c606d73d882da6a302f44b7880afb to your computer and use it in GitHub Desktop.
Save loyd/0f0c606d73d882da6a302f44b7880afb to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from subprocess import check_output
from json import loads
from time import sleep
from datetime import datetime
class Stats:
def __init__(self):
self.min = float('inf')
self.max = float('-inf')
self.sum = 0
self.cnt = 0
def append(self, value):
self.min = min(self.min, value)
self.max = max(self.max, value)
self.sum += value
self.cnt += 1
@property
def avg(self):
return self.sum / self.cnt
def __str__(self):
p = lambda v: round(v * 1e-6)
return '{:>2} {:>2} {:>2}'.format(p(self.avg), p(self.min), p(self.max))
def display(download, upload):
timestamp = datetime.strftime(datetime.now(), '%H:%M:%S')
print('{} Download {}\n Upload {}'.format(timestamp, download, upload))
print('time direction avg min max (Mbit/s)')
try:
download = Stats()
upload = Stats()
while True:
raw = check_output(['speedtest', '--json'])
result = loads(raw)
download.append(result['download'])
upload.append(result['upload'])
display(download, upload)
sleep(100)
except KeyboardInterrupt:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment