From e0a3b03af6bf6159532218f81ab032a91950fd21 Mon Sep 17 00:00:00 2001 From: Ergin ALTINTAS Date: Mon, 18 Feb 2019 06:35:34 +0300 Subject: [PATCH] Instead of just using Mbit/s or Mbyte/s to show the speed, we can use Kbit/s or Kbyte/s for slow connections and Gbit/s or Gbyte/s for fast connections. - added "humanized" function which converts big number to kilo, mega, giga equivalent etc. - added "--no-human" parameter which shows the speed using just bytes or bits - when "--no-human" parameter isn't used, we shod the humanized version of speed. otherwise we show just plain bits or bytes (which will be more usable by machines) --- speedtest.py | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/speedtest.py b/speedtest.py index 7a99c99..1ac0ac8 100755 --- a/speedtest.py +++ b/speedtest.py @@ -1636,6 +1636,9 @@ def parse_args(): parser.add_argument('--simple', action='store_true', default=False, help='Suppress verbose output, only show basic ' 'information') + parser.add_argument('--no-human', dest='human', default=True, + action='store_const', const=False, + help='Do not print (values) as human readable') parser.add_argument('--csv', action='store_true', default=False, help='Suppress verbose output, only show basic ' 'information in CSV format. Speeds listed in ' @@ -1724,6 +1727,18 @@ def printer(string, quiet=False, debug=False, error=False, **kwargs): print_(out, **kwargs) +def humanized(big_number): +# Thanks to "T" on page http://code.activestate.com/recipes/577081-humanized-representation-of-a-number-of-bytes/ +# Convert big numbers to human readble shorter ones using Kilo, Mega, Giga etc. + prefix_list=['','Kilo','Mega','Giga','Tera', 'Peta', 'Exa', 'Zeta', 'Yotta'] # Long version +# prefix_list=['','K','M','G','T', 'P', 'E', 'Z', 'Y'] # Short version + prefix_index = 0 + while (big_number >= 1000) and (prefix_index <= len(prefix_list)): # we are trying get below 1000 + prefix_index += 1 + big_number = big_number / 1024.0 + return "%.2f %s"%(big_number, prefix_list[prefix_index]) + + def shell(): """Run the full speedtest.net test""" @@ -1840,10 +1855,11 @@ def shell(): printer('Testing download speed', quiet, end=('', '\n')[bool(debug)]) speedtest.download(callback=callback) - printer('Download: %0.2f M%s/s' % - ((results.download / 1000.0 / 1000.0) / args.units[1], - args.units[0]), - quiet) + if args.human: + resultstr = humanized(results.download / args.units[1]) + else: + resultstr = '%0.2f ' % (results.download / args.units[1]) # ? why not 1024.0 + printer('Download: %s%s/s' % (resultstr, args.units[0]), quiet) else: printer('Skipping download test', quiet) @@ -1851,10 +1867,11 @@ def shell(): printer('Testing upload speed', quiet, end=('', '\n')[bool(debug)]) speedtest.upload(callback=callback, pre_allocate=args.pre_allocate) - printer('Upload: %0.2f M%s/s' % - ((results.upload / 1000.0 / 1000.0) / args.units[1], - args.units[0]), - quiet) + if args.human: + resultstr = humanized(results.upload / args.units[1]) + else: + resultstr = '%0.2f ' % (results.upload / args.units[1]) # ? why not 1024.0 + printer('Upload: %s%s/s' % (resultstr, args.units[0]), quiet) else: printer('Skipping upload test', quiet) @@ -1880,6 +1897,7 @@ def shell(): def main(): + try: shell() except KeyboardInterrupt: