##// END OF EJS Templates
profiling: add a context manager that no-ops if profiling isn't enabled...
Gregory Szorc -
r29784:e3501546 default
parent child Browse files
Show More
@@ -898,21 +898,12 b' def _dispatch(req):'
898 repo.close()
898 repo.close()
899
899
900 def _runcommand(ui, options, cmd, cmdfunc):
900 def _runcommand(ui, options, cmd, cmdfunc):
901 """Enables the profiler if applicable.
901 """Run a command function, possibly with profiling enabled."""
902
902 with profiling.maybeprofile(ui):
903 ``profiling.enabled`` - boolean config that enables or disables profiling
904 """
905 def checkargs():
906 try:
903 try:
907 return cmdfunc()
904 return cmdfunc()
908 except error.SignatureError:
905 except error.SignatureError:
909 raise error.CommandError(cmd, _("invalid arguments"))
906 raise error.CommandError(cmd, _('invalid arguments'))
910
911 if ui.configbool('profiling', 'enabled'):
912 with profiling.profile(ui):
913 return checkargs()
914 else:
915 return checkargs()
916
907
917 def _exceptionwarning(ui):
908 def _exceptionwarning(ui):
918 """Produce a warning message for the current active exception"""
909 """Produce a warning message for the current active exception"""
@@ -1393,6 +1393,12 b" collected during profiling, while 'profi"
1393 statistical text report generated from the profiling data. The
1393 statistical text report generated from the profiling data. The
1394 profiling is done using lsprof.
1394 profiling is done using lsprof.
1395
1395
1396 ``enabled``
1397 Enable the profiler.
1398 (default: false)
1399
1400 This is equivalent to passing ``--profile`` on the command line.
1401
1396 ``type``
1402 ``type``
1397 The type of profiler to use.
1403 The type of profiler to use.
1398 (default: ls)
1404 (default: ls)
@@ -143,3 +143,20 b' def profile(ui):'
143 val = val.replace('%', '%%')
143 val = val.replace('%', '%%')
144 ui.log('profile', val)
144 ui.log('profile', val)
145 fp.close()
145 fp.close()
146
147 @contextlib.contextmanager
148 def maybeprofile(ui):
149 """Profile if enabled, else do nothing.
150
151 This context manager can be used to optionally profile if profiling
152 is enabled. Otherwise, it does nothing.
153
154 The purpose of this context manager is to make calling code simpler:
155 just use a single code path for calling into code you may want to profile
156 and this function determines whether to start profiling.
157 """
158 if ui.configbool('profiling', 'enabled'):
159 with profile(ui):
160 yield
161 else:
162 yield
@@ -96,7 +96,6 b''
96 */mercurial/dispatch.py:* in _dispatch (glob)
96 */mercurial/dispatch.py:* in _dispatch (glob)
97 */mercurial/dispatch.py:* in runcommand (glob)
97 */mercurial/dispatch.py:* in runcommand (glob)
98 */mercurial/dispatch.py:* in _runcommand (glob)
98 */mercurial/dispatch.py:* in _runcommand (glob)
99 */mercurial/dispatch.py:* in checkargs (glob)
100 */mercurial/dispatch.py:* in <lambda> (glob)
99 */mercurial/dispatch.py:* in <lambda> (glob)
101 */mercurial/util.py:* in check (glob)
100 */mercurial/util.py:* in check (glob)
102 $TESTTMP/buggylocking.py:* in buggylocking (glob)
101 $TESTTMP/buggylocking.py:* in buggylocking (glob)
@@ -132,7 +131,6 b''
132 */mercurial/dispatch.py:* in _dispatch (glob)
131 */mercurial/dispatch.py:* in _dispatch (glob)
133 */mercurial/dispatch.py:* in runcommand (glob)
132 */mercurial/dispatch.py:* in runcommand (glob)
134 */mercurial/dispatch.py:* in _runcommand (glob)
133 */mercurial/dispatch.py:* in _runcommand (glob)
135 */mercurial/dispatch.py:* in checkargs (glob)
136 */mercurial/dispatch.py:* in <lambda> (glob)
134 */mercurial/dispatch.py:* in <lambda> (glob)
137 */mercurial/util.py:* in check (glob)
135 */mercurial/util.py:* in check (glob)
138 $TESTTMP/buggylocking.py:* in oldanddeprecated (glob)
136 $TESTTMP/buggylocking.py:* in oldanddeprecated (glob)
@@ -156,7 +154,6 b''
156 */mercurial/dispatch.py:* in _dispatch (glob)
154 */mercurial/dispatch.py:* in _dispatch (glob)
157 */mercurial/dispatch.py:* in runcommand (glob)
155 */mercurial/dispatch.py:* in runcommand (glob)
158 */mercurial/dispatch.py:* in _runcommand (glob)
156 */mercurial/dispatch.py:* in _runcommand (glob)
159 */mercurial/dispatch.py:* in checkargs (glob)
160 */mercurial/dispatch.py:* in <lambda> (glob)
157 */mercurial/dispatch.py:* in <lambda> (glob)
161 */mercurial/util.py:* in check (glob)
158 */mercurial/util.py:* in check (glob)
162 $TESTTMP/buggylocking.py:* in oldanddeprecated (glob)
159 $TESTTMP/buggylocking.py:* in oldanddeprecated (glob)
General Comments 0
You need to be logged in to leave comments. Login now