Skip to content

Commit

Permalink
Added profiling
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-l-p committed Dec 13, 2023
1 parent e0ae0de commit 006bf77
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
17 changes: 15 additions & 2 deletions sharpy/sharpy_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ def main(args=None, sharpy_input_dict=None):
import sharpy.postproc
import sharpy.generators
import sharpy.controllers

# Profiling
prof_enable = True

if prof_enable:
import cProfile, sharpy.utils.profile_analyse
pr = cProfile.Profile()
pr.enable()

# ------------

try:
Expand Down Expand Up @@ -148,6 +157,11 @@ def main(args=None, sharpy_input_dict=None):
cout.cout_wrap('FINISHED - CPU process time = %f6 seconds' % cpu_time, 2)
finish_writer()

# End profiling and analse output using string arguments
if prof_enable:
pr.disable()
prof_data = sharpy.utils.profile_analyse.profile_analse(pr, settings)

except Exception as e:
try:
logdir = settings['SHARPy']['log_folder'] + '/' + settings['SHARPy']['case']
Expand All @@ -165,8 +179,7 @@ def main(args=None, sharpy_input_dict=None):
logging.info('SHARPy Error Log')
logging.error("Exception occurred", exc_info=True)
raise e

return data
return data, prof_data



Expand Down
38 changes: 38 additions & 0 deletions sharpy/utils/profile_analyse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import string, csv, string, os, numpy as np, sys, io, cProfile, pstats
from pstats import SortKey

def profile_analse(pr, settings, names = ['beam', 'uvlm']):

prof_dir = settings['SHARPy']['route'] + settings['SHARPy']['case'] + '_profile.txt'
sys.stdout = open(prof_dir, 'w')
s = io.StringIO()
ps = pstats.Stats(pr, stream=s).sort_stats('tottime')
ps.print_stats()
print(s.getvalue())
sys.stdout.close()
sys.stdout = sys.__stdout__
if names[-1] != "other":
names.append("other")
t_names = np.zeros(len(names), dtype=float)
t_total = 0.0

with open(prof_dir, newline='\n') as f:
csv_f = csv.reader(f, delimiter=' ', skipinitialspace=True)

for row in csv_f:
try:
t_total += float(list(row)[1])
for i in range(len(names)-1):
if list(row)[5].find(names[i]) != -1:
t_names[i] += float(list(row)[1])
except: ()

t_names[-1] = t_total - np.sum(t_names)

print("\n")
for i in range(len(names)):
print(names[i] + ' time = ' + f'{t_names[i]:.3f}' + ' seconds (' + f'{t_names[i]/t_total*100:.3f}' + '%)')
print('\nTotal time = ' + f'{t_total:.3f}' + ' seconds\n')


return {'names':names, 't_names':t_names, 't_total':t_total}

0 comments on commit 006bf77

Please sign in to comment.