else/RateMeter/speedtest.py

39 lines
1.3 KiB
Python
Raw Normal View History

2016-05-10 08:00:29 +00:00
import bytestring
import downloady
import ratemeter
import requests
import time
2016-05-21 19:51:36 +00:00
URL = 'http://cdn.speedof.me/sample32768k.bin?r=0.881750426312'
METER = ratemeter.RateMeter(span=5)
2016-05-10 08:00:29 +00:00
METER_2 = ratemeter.RateMeter(span=None)
class G:
pass
g = G()
g.total = 0
g.start = None
2016-05-21 19:51:36 +00:00
g.last = time.time()
2016-05-10 08:00:29 +00:00
def callback_progress(bytes_downloaded, bytes_total):
if g.start is None:
g.start = time.time()
percent = 100 * bytes_downloaded / bytes_total
percent = '%07.3f%%:' % percent
chunk = bytes_downloaded - g.total
g.total = bytes_downloaded
METER.digest(chunk)
METER_2.digest(chunk)
now = round(time.time(), 1)
2016-05-21 19:51:36 +00:00
if now > g.last or (bytes_downloaded >= bytes_total):
2016-05-10 08:00:29 +00:00
g.last = now
percent = percent.rjust(9, ' ')
rate = bytestring.bytestring(METER.report()[2]).rjust(15, ' ')
rate2 = bytestring.bytestring(METER_2.report()[2]).rjust(15, ' ')
elapsed = str(round(now-g.start, 1)).rjust(10, ' ')
print(percent, rate, rate2, elapsed, end='\r', flush=True)
#print(METER.report(), METER_2.report())
print(URL)
2016-05-21 19:51:36 +00:00
print('Progress'.rjust(9, ' '), 'bps over 5s'.rjust(15, ' '), 'bps overall'.rjust(15, ' '), 'elapsed'.rjust(10, ' '))
2016-05-10 08:00:29 +00:00
downloady.download_file(URL, 'nul', callback_progress=callback_progress)