Mercurial > hg
view mercurial/lsprof.py @ 43299:83bb1e89ab9b
copies: compute the exact set of revision to walk
This change make the code clearer by removing the revision queue. It comes
without very noticeable performance impact. However the simpler code will be
easier to update in later changesets.
revision: large amount; added files: large amount; rename small amount; c3b14617fbd7 9ba6ab77fd29
before: ! wall 1.430082 comb 1.430000 user 1.390000 sys 0.040000 (median of 10)
after: ! wall 1.405192 comb 1.410000 user 1.390000 sys 0.020000 (median of 10)
revision: large amount; added files: small amount; rename small amount; c3b14617fbd7 f650a9b140d2
before: ! wall 1.971366 comb 1.970000 user 1.950000 sys 0.020000 (median of 10)
after: ! wall 1.892541 comb 1.890000 user 1.870000 sys 0.020000 (median of 10)
revision: large amount; added files: large amount; rename large amount; 08ea3258278e d9fa043f30c0
before: ! wall 0.252594 comb 0.250000 user 0.240000 sys 0.010000 (median of 38)
after: ! wall 0.240075 comb 0.240000 user 0.240000 sys 0.000000 (median of 40)
revision: small amount; added files: large amount; rename large amount; df6f7a526b60 a83dc6a2d56f
before: ! wall 0.013100 comb 0.010000 user 0.010000 sys 0.000000 (median of 226)
after: ! wall 0.013247 comb 0.010000 user 0.010000 sys 0.000000 (median of 223)
revision: small amount; added files: large amount; rename small amount; 4aa4e1f8e19a 169138063d63
before: ! wall 0.001633 comb 0.000000 user 0.000000 sys 0.000000 (median of 1000)
after: ! wall 0.001670 comb 0.000000 user 0.000000 sys 0.000000 (median of 1000)
revision: small amount; added files: small amount; rename small amount; 4bc173b045a6 964879152e2e
before: ! wall 0.000078 comb 0.000000 user 0.000000 sys 0.000000 (median of 11984)
after: ! wall 0.000119 comb 0.000000 user 0.000000 sys 0.000000 (median of 7982)
revision: medium amount; added files: large amount; rename medium amount; c95f1ced15f2 2c68e87c3efe
before: ! wall 0.207093 comb 0.210000 user 0.210000 sys 0.000000 (median of 47)
after: ! wall 0.201551 comb 0.200000 user 0.200000 sys 0.000000 (median of 48)
revision: medium amount; added files: medium amount; rename small amount; d343da0c55a8 d7746d32bf9d
before: ! wall 0.038462 comb 0.040000 user 0.040000 sys 0.000000 (median of 100)
after: ! wall 0.036578 comb 0.030000 user 0.030000 sys 0.000000 (median of 100)
Differential Revision: https://phab.mercurial-scm.org/D7076
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Sat, 12 Oct 2019 18:35:14 +0200 |
parents | dc6d866b8cb8 |
children | 313e3a279828 |
line wrap: on
line source
from __future__ import absolute_import, print_function import _lsprof import sys from .pycompat import getattr from . import pycompat Profiler = _lsprof.Profiler # PyPy doesn't expose profiler_entry from the module. profiler_entry = getattr(_lsprof, 'profiler_entry', None) __all__ = [b'profile', b'Stats'] def profile(f, *args, **kwds): """XXX docstring""" p = Profiler() p.enable(subcalls=True, builtins=True) try: f(*args, **kwds) finally: p.disable() return Stats(p.getstats()) class Stats(object): """XXX docstring""" def __init__(self, data): self.data = data def sort(self, crit=r"inlinetime"): """XXX docstring""" # profiler_entries isn't defined when running under PyPy. if profiler_entry: if crit not in profiler_entry.__dict__: raise ValueError(b"Can't sort by %s" % crit) elif self.data and not getattr(self.data[0], crit, None): raise ValueError(b"Can't sort by %s" % crit) self.data.sort(key=lambda x: getattr(x, crit), reverse=True) for e in self.data: if e.calls: e.calls.sort(key=lambda x: getattr(x, crit), reverse=True) def pprint(self, top=None, file=None, limit=None, climit=None): """XXX docstring""" if file is None: file = sys.stdout d = self.data if top is not None: d = d[:top] cols = b"% 12d %12d %11.4f %11.4f %s\n" hcols = b"% 12s %12s %12s %12s %s\n" file.write( hcols % ( b"CallCount", b"Recursive", b"Total(s)", b"Inline(s)", b"module:lineno(function)", ) ) count = 0 for e in d: file.write( cols % ( e.callcount, e.reccallcount, e.totaltime, e.inlinetime, label(e.code), ) ) count += 1 if limit is not None and count == limit: return ccount = 0 if climit and e.calls: for se in e.calls: file.write( cols % ( se.callcount, se.reccallcount, se.totaltime, se.inlinetime, b" %s" % label(se.code), ) ) count += 1 ccount += 1 if limit is not None and count == limit: return if climit is not None and ccount == climit: break def freeze(self): """Replace all references to code objects with string descriptions; this makes it possible to pickle the instance.""" # this code is probably rather ickier than it needs to be! for i in range(len(self.data)): e = self.data[i] if not isinstance(e.code, str): self.data[i] = type(e)((label(e.code),) + e[1:]) if e.calls: for j in range(len(e.calls)): se = e.calls[j] if not isinstance(se.code, str): e.calls[j] = type(se)((label(se.code),) + se[1:]) _fn2mod = {} def label(code): if isinstance(code, str): if sys.version_info.major >= 3: code = code.encode('latin-1') return code try: mname = _fn2mod[code.co_filename] except KeyError: for k, v in list(pycompat.iteritems(sys.modules)): if v is None: continue if not isinstance(getattr(v, '__file__', None), str): continue if v.__file__.startswith(code.co_filename): mname = _fn2mod[code.co_filename] = k break else: mname = _fn2mod[code.co_filename] = r'<%s>' % code.co_filename res = r'%s:%d(%s)' % (mname, code.co_firstlineno, code.co_name) if sys.version_info.major >= 3: res = res.encode('latin-1') return res