[PATCH 3 of 6] profile: introduce a knob to control if the context is actually profiling

Pierre-Yves David pierre-yves.david at ens-lyon.org
Fri Jun 9 08:32:59 EDT 2017


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at octobus.net>
# Date 1497004907 -3600
#      Fri Jun 09 11:41:47 2017 +0100
# Node ID 83187f3e565e3f5cfa68056554cd0c6dff97daa4
# Parent  5b9718803532b81e240e126913608385a32352bd
# EXP-Topic profile
# Available At https://www.mercurial-scm.org/repo/users/marmoute/mercurial/
#              hg pull https://www.mercurial-scm.org/repo/users/marmoute/mercurial/ -r 83187f3e565e
profile: introduce a knob to control if the context is actually profiling

This will is a step toward allowing context where the profiling in enabled
withing the context range.

This also open the way to killing the dedicated "maybeprofile" context manager
and keep only one of 'profile' and 'maybeprofile'.

diff --git a/mercurial/profiling.py b/mercurial/profiling.py
--- a/mercurial/profiling.py
+++ b/mercurial/profiling.py
@@ -147,17 +147,19 @@ class profile(object):
     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 @@ def maybeprofile(ui):
     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


More information about the Mercurial-devel mailing list