Skip to content

Commit

Permalink
Added self-profile (-profile): #1331
Browse files Browse the repository at this point in the history
  • Loading branch information
gfursin committed Oct 18, 2024
1 parent 0e31803 commit 93c2eab
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions cm/cmind/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ def x(self, i, out = None):
'h', 'help', 'version', 'out', 'j', 'json',
'save_to_json_file', 'save_to_yaml_file', 'common',
'ignore_inheritance', 'log', 'logfile', 'raise', 'repro',
'f', 'time']]
'f', 'time', 'profile']]

if len(unknown_control_flags)>0:
unknown_control_flags_str = ','.join(unknown_control_flags)
Expand All @@ -855,11 +855,13 @@ def x(self, i, out = None):
if control.pop('f', ''):
i['f'] = True

self_time = control.pop('time', False)
self_time = control.get('time', False)
if not x_was_called and self_time:
import time
self_time1 = time.time()

self_profile = control.get('profile', False)

# Check repro
use_log = str(control_flags.pop('log', '')).strip().lower()
log_file = control_flags.pop('logfile', '')
Expand Down Expand Up @@ -933,6 +935,13 @@ def x(self, i, out = None):
self.log(f"x input: {spaces} ({i})", "debug")

# Call access helper
if not x_was_called and self_profile:
# https://docs.python.org/3/library/profile.html#module-cProfile
import cProfile, pstats, io
from pstats import SortKey
profile = cProfile.Profile()
profile.enable()

r = self._x(i, control)

if not self.logger == None:
Expand All @@ -941,6 +950,17 @@ def x(self, i, out = None):
self.state['recursion'] = recursion

if not x_was_called:
if self_profile:
profile.disable()
s = io.StringIO()
sortby = SortKey.CUMULATIVE
ps = pstats.Stats(profile, stream=s).sort_stats(sortby)
ps.print_stats(32)
print ('')
print ('CMX profile:')
print ('')
print (s.getvalue())

# Very first call (not recursive)
# Check if output to json and save file

Expand Down

0 comments on commit 93c2eab

Please sign in to comment.