From e0a3b03af6bf6159532218f81ab032a91950fd21 Mon Sep 17 00:00:00 2001 From: Ergin ALTINTAS Date: Mon, 18 Feb 2019 06:35:34 +0300 Subject: [PATCH 1/4] 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: From 320d57a388ff1197de83161d557e23c90106d0b8 Mon Sep 17 00:00:00 2001 From: Ergin ALTINTAS Date: Mon, 25 Feb 2019 05:27:48 +0300 Subject: [PATCH 2/4] Changed "--simple" implemantation by removing two "quiet" parameters in the appropireate lines. So that humanized printing also work for "--simple" mode and "--simple" mode doesnt print "0.00 Mbytes" for the untested functions. --- speedtest.py | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/speedtest.py b/speedtest.py index 1ac0ac8..8bd5464 100755 --- a/speedtest.py +++ b/speedtest.py @@ -1730,8 +1730,8 @@ def printer(string, quiet=False, debug=False, error=False, **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_list=['','Kilo','Mega','Giga','Tera', 'Peta', 'Exa', 'Zeta', 'Yotta'] # Long versions + prefix_list=['','K','M','G','T', 'P', 'E', 'Z', 'Y'] # Short versions prefix_index = 0 while (big_number >= 1000) and (prefix_index <= len(prefix_list)): # we are trying get below 1000 prefix_index += 1 @@ -1851,15 +1851,18 @@ def shell(): printer('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: ' '%(latency)s ms' % results.server, quiet) + if args.simple: + printer('Ping: %s ms' % results.ping) + if args.download: printer('Testing download speed', quiet, end=('', '\n')[bool(debug)]) speedtest.download(callback=callback) if args.human: - resultstr = humanized(results.download / args.units[1]) + resultstr_down = 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) + resultstr_down = '%0.2f ' % (results.download / args.units[1]) + printer('Download: %s%s/s' % (resultstr_down, args.units[0])) else: printer('Skipping download test', quiet) @@ -1868,10 +1871,10 @@ def shell(): end=('', '\n')[bool(debug)]) speedtest.upload(callback=callback, pre_allocate=args.pre_allocate) if args.human: - resultstr = humanized(results.upload / args.units[1]) + resultstr_up = 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) + resultstr_up = '%0.2f ' % (results.upload / args.units[1]) + printer('Upload: %s%s/s' % (resultstr_up, args.units[0])) else: printer('Skipping upload test', quiet) @@ -1880,13 +1883,6 @@ def shell(): if not args.simple and args.share: results.share() - if args.simple: - printer('Ping: %s ms\nDownload: %0.2f M%s/s\nUpload: %0.2f M%s/s' % - (results.ping, - (results.download / 1000.0 / 1000.0) / args.units[1], - args.units[0], - (results.upload / 1000.0 / 1000.0) / args.units[1], - args.units[0])) elif args.csv: printer(results.csv(delimiter=args.csv_delimiter)) elif args.json: @@ -1897,7 +1893,7 @@ def shell(): def main(): - + try: shell() except KeyboardInterrupt: From 30f219bef125a4990b5429aa27f8f477471b68c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ergin=20ALTINTA=C5=9E?= Date: Mon, 25 Feb 2019 05:44:20 +0300 Subject: [PATCH 3/4] Update speedtest.py --- speedtest.py | 1 - 1 file changed, 1 deletion(-) diff --git a/speedtest.py b/speedtest.py index 8bd5464..751c571 100755 --- a/speedtest.py +++ b/speedtest.py @@ -1893,7 +1893,6 @@ def shell(): def main(): - try: shell() except KeyboardInterrupt: From d5f36a76795649a2b90d4ea41516394f7d73b56c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ergin=20ALTINTA=C5=9E?= Date: Mon, 25 Feb 2019 05:44:55 +0300 Subject: [PATCH 4/4] Update README.rst --- README.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/README.rst b/README.rst index 3e57422..57dffad 100644 --- a/README.rst +++ b/README.rst @@ -95,6 +95,7 @@ Usage --share Generate and provide a URL to the speedtest.net share results image, not displayed with --csv --simple Suppress verbose output, only show basic information + --no-human Do not print (values) as human readable --csv Suppress verbose output, only show basic information in CSV format. Speeds listed in bit/s and not affected by --bytes