Added flag for CSV
--csv Includes a timestamp and the same fields provided by --simple. Any error output will still show up in verbose form. Only successful runs will be a one-line CSV .
This commit is contained in:
parent
756f04da76
commit
836fe2b1b3
|
@ -23,6 +23,7 @@ shutdown_event = None
|
||||||
|
|
||||||
import math
|
import math
|
||||||
import time
|
import time
|
||||||
|
import datetime
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import threading
|
import threading
|
||||||
|
@ -456,6 +457,8 @@ def speedtest():
|
||||||
parser.add_argument('--simple', action='store_true',
|
parser.add_argument('--simple', action='store_true',
|
||||||
help='Suppress verbose output, only show basic '
|
help='Suppress verbose output, only show basic '
|
||||||
'information')
|
'information')
|
||||||
|
parser.add_argument('--csv', action='store_true',
|
||||||
|
help='Same as simple, formatted as CSV')
|
||||||
parser.add_argument('--list', action='store_true',
|
parser.add_argument('--list', action='store_true',
|
||||||
help='Display a list of speedtest.net servers '
|
help='Display a list of speedtest.net servers '
|
||||||
'sorted by distance')
|
'sorted by distance')
|
||||||
|
@ -472,6 +475,9 @@ def speedtest():
|
||||||
args = options
|
args = options
|
||||||
del options
|
del options
|
||||||
|
|
||||||
|
simple = args.simple or args.csv
|
||||||
|
csv = args.csv and ['%s'%datetime.datetime.now(),]
|
||||||
|
|
||||||
# Print the version and exit
|
# Print the version and exit
|
||||||
if args.version:
|
if args.version:
|
||||||
version()
|
version()
|
||||||
|
@ -481,7 +487,7 @@ def speedtest():
|
||||||
source = args.source
|
source = args.source
|
||||||
socket.socket = bound_socket
|
socket.socket = bound_socket
|
||||||
|
|
||||||
if not args.simple:
|
if not simple:
|
||||||
print_('Retrieving speedtest.net configuration...')
|
print_('Retrieving speedtest.net configuration...')
|
||||||
try:
|
try:
|
||||||
config = getConfig()
|
config = getConfig()
|
||||||
|
@ -489,7 +495,7 @@ def speedtest():
|
||||||
print_('Cannot retrieve speedtest configuration')
|
print_('Cannot retrieve speedtest configuration')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if not args.simple:
|
if not simple:
|
||||||
print_('Retrieving speedtest.net server list...')
|
print_('Retrieving speedtest.net server list...')
|
||||||
if args.list or args.server:
|
if args.list or args.server:
|
||||||
servers = closestServers(config['client'], True)
|
servers = closestServers(config['client'], True)
|
||||||
|
@ -513,7 +519,7 @@ def speedtest():
|
||||||
else:
|
else:
|
||||||
servers = closestServers(config['client'])
|
servers = closestServers(config['client'])
|
||||||
|
|
||||||
if not args.simple:
|
if not simple:
|
||||||
print_('Testing from %(isp)s (%(ip)s)...' % config['client'])
|
print_('Testing from %(isp)s (%(ip)s)...' % config['client'])
|
||||||
|
|
||||||
if args.server:
|
if args.server:
|
||||||
|
@ -555,11 +561,11 @@ def speedtest():
|
||||||
except:
|
except:
|
||||||
best = servers[0]
|
best = servers[0]
|
||||||
else:
|
else:
|
||||||
if not args.simple:
|
if not simple:
|
||||||
print_('Selecting best server based on ping...')
|
print_('Selecting best server based on ping...')
|
||||||
best = getBestServer(servers)
|
best = getBestServer(servers)
|
||||||
|
|
||||||
if not args.simple:
|
if not simple:
|
||||||
# Python 2.7 and newer seem to be ok with the resultant encoding
|
# Python 2.7 and newer seem to be ok with the resultant encoding
|
||||||
# from parsing the XML, but older versions have some issues.
|
# from parsing the XML, but older versions have some issues.
|
||||||
# This block should detect whether we need to encode or not
|
# This block should detect whether we need to encode or not
|
||||||
|
@ -571,7 +577,10 @@ def speedtest():
|
||||||
print_('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: '
|
print_('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: '
|
||||||
'%(latency)s ms' % best)
|
'%(latency)s ms' % best)
|
||||||
else:
|
else:
|
||||||
print_('Ping: %(latency)s ms' % best)
|
if args.csv:
|
||||||
|
csv.append('%(latency)s' % best)
|
||||||
|
else:
|
||||||
|
print_('Ping: %(latency)s ms' % best)
|
||||||
|
|
||||||
sizes = [350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000]
|
sizes = [350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000]
|
||||||
urls = []
|
urls = []
|
||||||
|
@ -579,25 +588,36 @@ def speedtest():
|
||||||
for i in range(0, 4):
|
for i in range(0, 4):
|
||||||
urls.append('%s/random%sx%s.jpg' %
|
urls.append('%s/random%sx%s.jpg' %
|
||||||
(os.path.dirname(best['url']), size, size))
|
(os.path.dirname(best['url']), size, size))
|
||||||
if not args.simple:
|
|
||||||
|
to_mbps = lambda speed: '%0.2f' % ((speed/1000/1000)*8)
|
||||||
|
|
||||||
|
if not simple:
|
||||||
print_('Testing download speed', end='')
|
print_('Testing download speed', end='')
|
||||||
dlspeed = downloadSpeed(urls, args.simple)
|
dlspeed = downloadSpeed(urls, simple)
|
||||||
if not args.simple:
|
if not simple:
|
||||||
print_()
|
print_()
|
||||||
print_('Download: %0.2f Mbit/s' % ((dlspeed / 1000 / 1000) * 8))
|
if args.csv:
|
||||||
|
csv.append(to_mbps(dlspeed))
|
||||||
|
else:
|
||||||
|
print_('Download: %s Mbit/s' % to_mbps(dlspeed))
|
||||||
|
|
||||||
sizesizes = [int(.25 * 1000 * 1000), int(.5 * 1000 * 1000)]
|
sizesizes = [int(.25 * 1000 * 1000), int(.5 * 1000 * 1000)]
|
||||||
sizes = []
|
sizes = []
|
||||||
for size in sizesizes:
|
for size in sizesizes:
|
||||||
for i in range(0, 25):
|
for i in range(0, 25):
|
||||||
sizes.append(size)
|
sizes.append(size)
|
||||||
if not args.simple:
|
if not simple:
|
||||||
print_('Testing upload speed', end='')
|
print_('Testing upload speed', end='')
|
||||||
ulspeed = uploadSpeed(best['url'], sizes, args.simple)
|
ulspeed = uploadSpeed(best['url'], sizes, simple)
|
||||||
if not args.simple:
|
if not simple:
|
||||||
print_()
|
print_()
|
||||||
print_('Upload: %0.2f Mbit/s' % ((ulspeed / 1000 / 1000) * 8))
|
if args.csv:
|
||||||
|
csv.append(to_mbps(ulspeed))
|
||||||
|
else:
|
||||||
|
print_('Upload: %s Mbit/s' % to_mbps(ulspeed))
|
||||||
|
|
||||||
|
if args.csv:
|
||||||
|
print_(','.join(csv))
|
||||||
if args.share and args.mini:
|
if args.share and args.mini:
|
||||||
print_('Cannot generate a speedtest.net share results image while '
|
print_('Cannot generate a speedtest.net share results image while '
|
||||||
'testing against a Speedtest Mini server')
|
'testing against a Speedtest Mini server')
|
||||||
|
|
Loading…
Reference in New Issue