Mercurial > public > mercurial-scm > hg-stable
diff mercurial/profiling.py @ 32455:f40dc6f7c12f
profiling: allow loading profiling extension before everything else
6d642ecf1a89 makes profiler start early without loading extensions. That
makes it impossible for an extension to add customized profilers.
This patch adds a special case: if a profiler is not found but an extension
with the same name could be loaded, load that extension first, and expect it
to have a "profile" contextmanager method. This allows customized profilers
and extension setup time is still profiled.
author | Jun Wu <quark@fb.com> |
---|---|
date | Mon, 22 May 2017 01:17:49 -0700 |
parents | 22fbca1d11ed |
children | 4483696dacee |
line wrap: on
line diff
--- a/mercurial/profiling.py Mon May 22 00:51:56 2017 -0700 +++ b/mercurial/profiling.py Mon May 22 01:17:49 2017 -0700 @@ -13,9 +13,21 @@ from . import ( encoding, error, + extensions, util, ) +def _loadprofiler(ui, profiler): + """load profiler extension. return profile method, or None on failure""" + extname = profiler + extensions.loadall(ui, whitelist=[extname]) + try: + mod = extensions.find(extname) + except KeyError: + return None + else: + return getattr(mod, 'profile', None) + @contextlib.contextmanager def lsprofile(ui, fp): format = ui.config('profiling', 'format', default='text') @@ -137,11 +149,15 @@ manager exits, profiling results will be written to the configured output. """ profiler = encoding.environ.get('HGPROF') + proffn = None if profiler is None: profiler = ui.config('profiling', 'type', default='stat') if profiler not in ('ls', 'stat', 'flame'): - ui.warn(_("unrecognized profiler '%s' - ignored\n") % profiler) - profiler = 'stat' + # try load profiler from extension with the same name + proffn = _loadprofiler(ui, profiler) + if proffn is None: + ui.warn(_("unrecognized profiler '%s' - ignored\n") % profiler) + profiler = 'stat' output = ui.config('profiling', 'output') @@ -154,7 +170,9 @@ fp = ui.ferr try: - if profiler == 'ls': + if proffn is not None: + pass + elif profiler == 'ls': proffn = lsprofile elif profiler == 'flame': proffn = flameprofile