changeset 32805:37ec8f24d912

profile: introduce a knob to control if the context is actually profiling This is a step toward allowing context where the profiling in enabled withing the context range. This also open the way to kill the dedicated "maybeprofile" context manager and keep only one of 'profile' and 'maybeprofile'.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Fri, 09 Jun 2017 11:41:47 +0100
parents 086c1ef0f666
children 0ead06d54ffe
files mercurial/profiling.py
diffstat 1 files changed, 5 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/profiling.py	Fri Jun 09 11:39:53 2017 +0100
+++ b/mercurial/profiling.py	Fri Jun 09 11:41:47 2017 +0100
@@ -147,17 +147,19 @@
     Profiling is active when the context manager is active. When the context
     manager exits, profiling results will be written to the configured output.
     """
-    def __init__(self, ui):
+    def __init__(self, ui, enabled=True):
         self._ui = ui
         self._output = None
         self._fp = None
         self._profiler = None
+        self._enabled = enabled
         self._entered = False
         self._started = False
 
     def __enter__(self):
         self._entered = True
-        self.start()
+        if self._enabled:
+            self.start()
 
     def start(self):
         """Start profiling.
@@ -228,8 +230,5 @@
     just use a single code path for calling into code you may want to profile
     and this function determines whether to start profiling.
     """
-    if ui.configbool('profiling', 'enabled'):
-        with profile(ui):
-            yield
-    else:
+    with profile(ui, enabled=ui.configbool('profiling', 'enabled')):
         yield