Skip to content

Instantly share code, notes, and snippets.

@Lokno
Created March 25, 2019 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lokno/671c78cafa3f4ff7bc13306a03a05e9e to your computer and use it in GitHub Desktop.
Save Lokno/671c78cafa3f4ff7bc13306a03a05e9e to your computer and use it in GitHub Desktop.
Python 3 script for running an application multiple times
# Script for running an application multiple times
#
# usage run_handler.py "<app + args>" <times> [output.csv]
#
# This script runs the a process multiple times and reports the
# average running time. Optionally, a file name can be provided,
# and the script will append the execution of each completed time
# into a file of that name.
#
# The script further estimates its total running time after the
# the first run of the process completes, and will prompt the
# user to continue if the total time exceeds threadhold_alert seconds
import subprocess
import sys
from pathlib import Path
import time
import signal
def sigint_handler(signal, frame):
print('Interrupted by user. Aborting...')
sys.exit(0)
# If the total running time of the script exceeds this threshold
# the user is prompted to continue
threshold_alert = 3600
if __name__ == '__main__':
argc = len(sys.argv)
if argc != 3 and argc != 4:
print('usage %s "<app + args>" <times> [output.csv]' % sys.argv[0])
sys.exit(-1)
app_list = sys.argv[1].split(' ')
times = float(sys.argv[2])
if int(times) < 1:
print("Error: Invalid number of iterations. Aborting...")
sys.exit(-1)
total_time = 0.0
signal.signal(signal.SIGINT, sigint_handler)
# remove output file of the same name
if argc == 4:
output_file = sys.argv[3]
p = Path(output_file)
if p.exists():
prompt = "%s exists. Overwrite? (y/n): " % output_file
text = input(prompt)
while text.lower() != 'y' and text.lower() != 'n':
text = input(prompt)
if text == 'n':
print("Aborting...")
sys.exit(0)
p.unlink()
print("Executing %s %d times..." % (app_list[0],int(times)))
print("Running first time...")
i = int(times)
while i > 0:
start_time = time.time()
try:
result = subprocess.run(app_list, stdout=subprocess.PIPE)
except FileNotFoundError:
print("Error: %s not found" % app_list[0])
sys.exit(-1)
elapsed_time = time.time()-start_time
total_time += elapsed_time
# estimate running time
if i == int(times):
est_time = elapsed_time*(times-1.0)
if est_time > 3600:
est_str = "Est. Running time %0.3f hours." % (est_time/3600)
elif est_time > 60:
est_str = "Est. Running time %0.3f minutes." % (est_time/60)
else:
est_str = "Est. Running time %0.3f seconds." % (est_time)
if elapsed_time*(times-1.0) > threshold_alert:
prompt = est_str + " Continue? (y/n): "
text = input(prompt)
while text.lower() != 'y' and text.lower() != 'n':
text = input(prompt)
if text == 'n':
print("Aborting...")
sys.exit(0)
print("Executing...")
else:
print(est_str)
if argc == 4:
with open(output_file,'a') as f:
f.write(str(elapsed_time) + '\n')
i -= 1
print("Done!")
print("Average time = %f" % (total_time / times))
if argc == 4:
print("Execution times written to %s" % output_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment