else/RateMeter/speedtest.py

46 lines
1.5 KiB
Python
Raw Normal View History

2016-11-18 06:24:52 +00:00
from voussoirkit import bytestring
2016-05-10 08:00:29 +00:00
import downloady
import ratemeter
import requests
2016-07-05 07:24:08 +00:00
import sys
2016-05-10 08:00:29 +00:00
import time
2016-07-05 07:24:08 +00:00
if len(sys.argv) == 2:
URL = sys.argv[1]
else:
URL = 'http://cdn.speedof.me/sample32768k.bin?r=0.881750426312'
2016-05-21 19:51:36 +00:00
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
2016-09-05 23:37:07 +00:00
class P:
def __init__(self, bytes_total):
self.bytes_total = bytes_total
def step(self, bytes_downloaded):
if g.start is None:
g.start = time.time()
percent = 100 * bytes_downloaded / self.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)
if now > g.last or (bytes_downloaded >= self.bytes_total):
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())
2016-05-10 08:00:29 +00:00
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-09-05 23:37:07 +00:00
downloady.download_file(URL, 'nul', callback_progress=P)