Skip to content

Commit

Permalink
examples: elapsed time
Browse files Browse the repository at this point in the history
  • Loading branch information
mgates3 committed May 27, 2024
1 parent 16c3696 commit a5b3ded
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions examples/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import subprocess
import io
import time
import sys

timer = time.time()

Expand All @@ -30,10 +31,26 @@

opts = parser.parse_args()

# ------------------------------------------------------------------------------
# When stdout is redirected to file instead of TTY console,
# and stderr is still going to a TTY console,
# print extra summary messages to stderr.
output_redirected = sys.stderr.isatty() and not sys.stdout.isatty()

# ------------------------------------------------------------------------------
# if output is redirected, prints to both stderr and stdout;
# otherwise prints to just stdout.
def print_tee( *args ):
global output_redirected
print( *args )
if (output_redirected):
print( *args, file=sys.stderr )
# end

#-------------------------------------------------------------------------------
def run_test( cmd ):
print( '-' * 80 )
print( ' '.join( cmd ) )
print_tee( '-' * 80 )
print_tee( ' '.join( cmd ) )
p = subprocess.Popen( cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT )
p_out = io.TextIOWrapper( p.stdout, encoding='utf-8' )
Expand All @@ -46,9 +63,9 @@ def run_test( cmd ):
err = p.wait()

if (err != 0):
print( 'FAILED, exit code', err )
print_tee( 'FAILED, exit code', err )
else:
print( 'passed' )
print_tee( 'passed' )

print( output )
return err
Expand Down Expand Up @@ -118,16 +135,17 @@ def run_test( cmd ):

types = opts.type.split()
for test in tests:
t = time.time()
cmd = runner + test.split() + types
err = run_test( cmd )
t = time.time() - t
print_tee( 'Elapsed %02d:%05.2f mm:ss' % (t // 60, t % 60) )
if (err):
failed_tests.append( test )
# end

timer = time.time() - timer
mins = timer // 60
secs = timer % 60
print( 'Elapsed %02d:%02d' % (mins, secs) )
print_tee( 'Total Elapsed %02d:%05.2f mm:ss' % (timer // 60, timer % 60) )

# print summary of failures
nfailed = len( failed_tests )
Expand Down

0 comments on commit a5b3ded

Please sign in to comment.