import os
import sys
import getopt
import re
import subprocess
import time
import statistics
def print_usage():
my_print( " " )
my_print( " %s" % script_name )
my_print( " " )
my_print( " Field G. Van Zee" )
my_print( " " )
my_print( " Repeatedly run a test driver and accumulate statistics for the" )
my_print( " output." )
my_print( " " )
my_print( " Usage:" )
my_print( " " )
my_print( " %s [options] drivername" % script_name )
my_print( " " )
my_print( " Arguments:" )
my_print( " " )
my_print( " drivername The filename/path of the test driver to run. The" )
my_print( " test driver must output its performance data to" )
my_print( " standard output." )
my_print( " " )
my_print( " The following options are accepted:" )
my_print( " " )
my_print( " -c num performance column index" )
my_print( " Find the performance result in column index <num> of" )
my_print( " the test driver's output. Here, a column is defined" )
my_print( " as a contiguous sequence of non-whitespace characters," )
my_print( " with the column indices beginning at 0. By default," )
my_print( " the second-to-last column index in the output is used." )
my_print( " " )
my_print( " -d delay sleep() delay" )
my_print( " Wait <delay> seconds after each execution of the" )
my_print( " test driver. The default delay is 0." )
my_print( " " )
my_print( " -n niter number of iterations" )
my_print( " Execute the test driver <niter> times. The default" )
my_print( " value is 10." )
my_print( " " )
my_print( " -q quiet; summary only" )
my_print( " Do not output statistics after every new execution of" )
my_print( " the test driver; instead, only output the final values" )
my_print( " after all iterations are complete. The default is to" )
my_print( " output updated statistics after each iteration." )
my_print( " " )
my_print( " -h help" )
my_print( " Output this information and exit." )
my_print( " " )
def my_print( s ):
sys.stdout.write( "%s\n" % s )
script_name = None
output_name = None
def main():
global script_name
global output_name
path, script_name = os.path.split(sys.argv[0])
output_name = script_name
perf_col = -1
delay = 0
niter = 10
quiet = False
try:
opts, args = getopt.getopt( sys.argv[1:], "c:d:n:hq" )
except getopt.GetoptError as err:
my_print( str(err) ) print_usage()
sys.exit(2)
for opt, optarg in opts:
if opt == "-c":
perf_col = optarg
elif opt == "-d":
delay = optarg
elif opt == "-n":
niter = optarg
elif opt == "-q":
quiet = True
elif opt == "-h":
print_usage()
sys.exit()
else:
print_usage()
sys.exit()
if len( args ) != 1:
print_usage()
sys.exit()
driverfile = args[0]
iters = range( int(niter) )
p = subprocess.run( driverfile, stdout=subprocess.PIPE )
lines0 = p.stdout.decode().splitlines()
num_lines0 = int(len(lines0))
aperf = []
for i in range( num_lines0 ):
aperf.append( [] )
for it in iters:
p = subprocess.run( driverfile, stdout=subprocess.PIPE )
lines = p.stdout.decode().splitlines()
for i in range( num_lines0 ):
line = lines[i]
words = line.split()
if perf_col == -1:
perf = words[ len(words)-2 ]
else:
perf = words[ int(perf_col) ]
if float(perf) == float('Inf') or \
float(perf) == -float('Inf') or \
float(perf) == float('NaN'): perf = 0.0
aperf[i].append( float(perf) )
avgp = statistics.mean( aperf[i] )
maxp = max( aperf[i] )
minp = min( aperf[i] )
if len( aperf[i] ) > 1: stdp = statistics.stdev( aperf[i] )
else: stdp = 0.0
search = '%8s' % perf
newline = re.sub( str(search), ' %7.2f %7.2f %7.2f %6.2f', line )
found_index = False
for word in words:
if re.match( '1:', word ):
index_str = word
found_index = True
break
if found_index:
last_col = int(index_str[2]) + 3
new_index_str = '1:%1s' % last_col
newline = re.sub( index_str, new_index_str, newline )
if not quiet:
print( newline % ( float(minp), float(avgp), float(maxp), float(stdp) ) )
sys.stdout.flush()
time.sleep( int(delay) )
if quiet:
for i in range( num_lines0 ):
line = lines0[i]
words = line.split()
if perf_col == -1:
perf = words[ len(words)-2 ]
else:
perf = words[ int(perf_col) ]
avgp = statistics.mean( aperf[i] )
maxp = max( aperf[i] )
minp = min( aperf[i] )
if len( aperf[i] ) > 1: stdp = statistics.stdev( aperf[i] )
else: stdp = 0.0
search = '%8s' % perf
newline = re.sub( str(search), ' %7.2f %7.2f %7.2f %6.2f', line )
found_index = False
for word in words:
if re.match( '1:', word ):
index_str = word
found_index = True
break
if found_index:
last_col = int(index_str[2]) + 3
new_index_str = '1:%1s' % last_col
newline = re.sub( index_str, new_index_str, newline )
print( newline % ( float(minp), float(avgp), float(maxp), float(stdp) ) )
sys.stdout.flush()
return 0
if __name__ == "__main__":
main()