Added speed formatter to change the unit of the speed appropriately.

This commit is contained in:
Parth Verma 2017-12-02 23:09:14 +05:30
parent 20e5d12a5c
commit 3c05ee2eda
1 changed files with 21 additions and 14 deletions

View File

@ -924,7 +924,7 @@ class Speedtest(object):
d = distance(self.lat_lon, d = distance(self.lat_lon,
(float(attrib.get('lat')), (float(attrib.get('lat')),
float(attrib.get('lon')))) float(attrib.get('lon'))))
except: except Exception:
continue continue
attrib['d'] = d attrib['d'] = d
@ -974,7 +974,7 @@ class Speedtest(object):
for ext in ['php', 'asp', 'aspx', 'jsp']: for ext in ['php', 'asp', 'aspx', 'jsp']:
try: try:
f = urlopen('%s/speedtest/upload.%s' % (url, ext)) f = urlopen('%s/speedtest/upload.%s' % (url, ext))
except: except Exception:
pass pass
else: else:
data = f.read().strip().decode() data = f.read().strip().decode()
@ -1320,6 +1320,17 @@ def validate_optional_args(args):
'unavailable' % (info[0], arg)) 'unavailable' % (info[0], arg))
def format_speed(speed_bytes_per_second, unit):
base = 1024 if unit[0] == 'byte' else 1000
seq = ['', 'K', 'M', 'G']
i = 0
speed = speed_bytes_per_second / unit[1]
while not (1 < speed < base):
i += 1
speed /= base
return '%0.2f %s%s/s' % (speed, seq[i], unit[0])
def printer(string, quiet=False, debug=False, **kwargs): def printer(string, quiet=False, debug=False, **kwargs):
"""Helper function to print a string only when not quiet""" """Helper function to print a string only when not quiet"""
@ -1457,10 +1468,10 @@ def shell():
printer('Testing download speed', quiet, printer('Testing download speed', quiet,
end=('', '\n')[bool(debug)]) end=('', '\n')[bool(debug)])
speedtest.download(callback=callback) speedtest.download(callback=callback)
printer('Download: %0.2f M%s/s' % printer('Download: %s' %
((results.download / 1000.0 / 1000.0) / args.units[1], format_speed(results.download, args.units),
args.units[0]),
quiet) quiet)
else: else:
printer('Skipping download test') printer('Skipping download test')
@ -1468,20 +1479,16 @@ def shell():
printer('Testing upload speed', quiet, printer('Testing upload speed', quiet,
end=('', '\n')[bool(debug)]) end=('', '\n')[bool(debug)])
speedtest.upload(callback=callback, pre_allocate=args.pre_allocate) speedtest.upload(callback=callback, pre_allocate=args.pre_allocate)
printer('Upload: %0.2f M%s/s' % printer('Upload: %s' %
((results.upload / 1000.0 / 1000.0) / args.units[1], format_speed(results.upload, args.units),
args.units[0]),
quiet) quiet)
else: else:
printer('Skipping upload test') printer('Skipping upload test')
if args.simple: if args.simple:
print_('Ping: %s ms\nDownload: %0.2f M%s/s\nUpload: %0.2f M%s/s' % print_('Ping: %s ms\nDownload: %s\nUpload: %s' %
(results.ping, (results.ping, format_speed(results.download, args.units),
(results.download / 1000.0 / 1000.0) / args.units[1], format_speed(results.upload, args.units)))
args.units[0],
(results.upload / 1000.0 / 1000.0) / args.units[1],
args.units[0]))
elif args.csv: elif args.csv:
print_(results.csv(delimiter=args.csv_delimiter)) print_(results.csv(delimiter=args.csv_delimiter))
elif args.json: elif args.json: