From 2b18cc806d899ad80a35dfc8ea3f0e37ffa87079 Mon Sep 17 00:00:00 2001 From: Bart Swedrowski Date: Sat, 14 Jun 2014 16:01:24 +0100 Subject: [PATCH] Add CSV output option --csv option will enable --simple and print result in CSV format. Useful if you want to eg. run periodic tests (eg. from cron) and log results for future analysis. --- speedtest_cli.py | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/speedtest_cli.py b/speedtest_cli.py index 6c89b1d..ed75de4 100755 --- a/speedtest_cli.py +++ b/speedtest_cli.py @@ -460,6 +460,9 @@ def speedtest(): parser.add_argument('--simple', action='store_true', help='Suppress verbose output, only show basic ' 'information') + parser.add_argument('--csv', action='store_true', + help='Format results as CSV (ping, download, upload); ' + 'enables --simple as well') parser.add_argument('--list', action='store_true', help='Display a list of speedtest.net servers ' 'sorted by distance') @@ -485,6 +488,10 @@ def speedtest(): source = args.source socket.socket = bound_socket + # --csv enables --simple + if args.csv: + args.simple = True + if not args.simple: print_('Retrieving speedtest.net configuration...') try: @@ -588,7 +595,10 @@ def speedtest(): print_('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: ' '%(latency)s ms' % best) else: - print_('Ping: %(latency)s ms' % best) + if args.csv: + print_('%(latency)s,' % best, end='') + else: + print_('Ping: %(latency)s ms' % best) sizes = [350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000] urls = [] @@ -599,10 +609,14 @@ def speedtest(): if not args.simple: print_('Testing download speed', end='') dlspeed = downloadSpeed(urls, args.simple) - if not args.simple: - print_() - print_('Download: %0.2f M%s/s' % - ((dlspeed / 1000 / 1000) * args.units[1], args.units[0])) + + if args.csv: + print_('%0.2f,' % ((dlspeed / 1000 / 1000) * args.units[1]), end='') + else: + if not args.simple: + print_() + print_('Download: %0.2f M%s/s' % + ((dlspeed / 1000 / 1000) * args.units[1], args.units[0])) sizesizes = [int(.25 * 1000 * 1000), int(.5 * 1000 * 1000)] sizes = [] @@ -612,10 +626,14 @@ def speedtest(): if not args.simple: print_('Testing upload speed', end='') ulspeed = uploadSpeed(best['url'], sizes, args.simple) - if not args.simple: - print_() - print_('Upload: %0.2f M%s/s' % - ((ulspeed / 1000 / 1000) * args.units[1], args.units[0])) + + if args.csv: + print_('%0.2f' % ((ulspeed / 1000 / 1000) * args.units[1])) + else: + if not args.simple: + print_() + print_('Upload: %0.2f M%s/s' % + ((ulspeed / 1000 / 1000) * args.units[1], args.units[0])) if args.share and args.mini: print_('Cannot generate a speedtest.net share results image while '