Mercurial > public > mercurial-scm > hg
comparison mercurial/lsprof.py @ 5992:30c40ba10963
updating lsprof.py from remote repository
author | Dirkjan Ochtman <dirkjan@ochtman.nl> |
---|---|
date | Fri, 01 Feb 2008 10:31:09 +0100 |
parents | 976b6b2a1613 |
children | e75aab656f46 |
comparison
equal
deleted
inserted
replaced
5991:31726c27e40e | 5992:30c40ba10963 |
---|---|
1 # this is copied from the lsprof distro because somehow | 1 #! /usr/bin/env python |
2 # it is not installed by distutils | |
3 # | |
4 # small modifications made | |
5 | 2 |
6 import sys | 3 import sys |
7 try: | 4 from _lsprof import Profiler, profiler_entry, profiler_subentry |
8 from _lsprof import Profiler, profiler_entry, profiler_subentry | |
9 except ImportError, inst: | |
10 import packagescan | |
11 if packagescan.scan_in_progress: | |
12 raise packagescan.SkipPackage('_lsprof not available') | |
13 raise | |
14 | 5 |
15 __all__ = ['profile', 'Stats'] | 6 __all__ = ['profile', 'Stats'] |
16 | 7 |
17 def profile(f, *args, **kwds): | 8 def profile(f, *args, **kwds): |
18 """XXX docstring""" | 9 """XXX docstring""" |
19 p = Profiler() | 10 p = Profiler() |
20 p.enable(subcalls=True) | 11 p.enable(subcalls=True, builtins=True) |
21 try: | 12 try: |
22 ret = f(*args, **kwds) | 13 f(*args, **kwds) |
23 finally: | 14 finally: |
24 p.disable() | 15 p.disable() |
25 return ret, Stats(p.getstats()) | 16 return Stats(p.getstats()) |
26 | 17 |
27 | 18 |
28 class Stats(object): | 19 class Stats(object): |
29 """XXX docstring""" | 20 """XXX docstring""" |
30 | 21 |
47 if file is None: | 38 if file is None: |
48 file = sys.stdout | 39 file = sys.stdout |
49 d = self.data | 40 d = self.data |
50 if top is not None: | 41 if top is not None: |
51 d = d[:top] | 42 d = d[:top] |
52 cols = "% 12s %11.4f %11.4f %s\n" | 43 cols = "% 12s %12s %11.4f %11.4f %s\n" |
53 hcols = "% 12s %12s %12s %s\n" | 44 hcols = "% 12s %12s %12s %12s %s\n" |
54 cols2 = "+%12s %11.4f %11.4f + %s\n" | 45 cols2 = "+%12s %12s %11.4f %11.4f + %s\n" |
55 file.write(hcols % ("CallCount", "Total(s)", | 46 file.write(hcols % ("CallCount", "Recursive", "Total(ms)", |
56 "Inline(s)", "module:lineno(function)")) | 47 "Inline(ms)", "module:lineno(function)")) |
57 count = 0 | 48 count = 0 |
58 for e in d: | 49 for e in d: |
59 file.write(cols % (e.callcount, e.totaltime, | 50 file.write(cols % (e.callcount, e.reccallcount, e.totaltime, |
60 e.inlinetime, label(e.code))) | 51 e.inlinetime, label(e.code))) |
61 count += 1 | 52 count += 1 |
62 if limit is not None and count == limit: | 53 if limit is not None and count == limit: |
63 return | 54 return |
64 ccount = 0 | 55 ccount = 0 |
65 if e.calls: | 56 if e.calls: |
66 for se in e.calls: | 57 for se in e.calls: |
67 file.write(cols % ("+%s" % se.callcount, | 58 file.write(cols % ("+%s" % se.callcount, se.reccallcount, |
68 se.totaltime, se.inlinetime, | 59 se.totaltime, se.inlinetime, |
69 "+%s" % label(se.code))) | 60 "+%s" % label(se.code))) |
70 count += 1 | 61 count += 1 |
71 ccount += 1 | 62 ccount += 1 |
72 if limit is not None and count == limit: | 63 if limit is not None and count == limit: |
81 # this code is probably rather ickier than it needs to be! | 72 # this code is probably rather ickier than it needs to be! |
82 for i in range(len(self.data)): | 73 for i in range(len(self.data)): |
83 e = self.data[i] | 74 e = self.data[i] |
84 if not isinstance(e.code, str): | 75 if not isinstance(e.code, str): |
85 self.data[i] = type(e)((label(e.code),) + e[1:]) | 76 self.data[i] = type(e)((label(e.code),) + e[1:]) |
86 if e.calls: | 77 if e.calls: |
87 for j in range(len(e.calls)): | 78 for j in range(len(e.calls)): |
88 se = e.calls[j] | 79 se = e.calls[j] |
89 if not isinstance(se.code, str): | 80 if not isinstance(se.code, str): |
90 e.calls[j] = type(se)((label(se.code),) + se[1:]) | 81 e.calls[j] = type(se)((label(se.code),) + se[1:]) |
91 | 82 |
92 _fn2mod = {} | 83 _fn2mod = {} |
93 | 84 |
94 def label(code): | 85 def label(code): |
95 if isinstance(code, str): | 86 if isinstance(code, str): |
96 return code | 87 return code |
97 try: | 88 try: |
98 mname = _fn2mod[code.co_filename] | 89 mname = _fn2mod[code.co_filename] |
99 except KeyError: | 90 except KeyError: |
100 for k, v in sys.modules.iteritems(): | 91 for k, v in sys.modules.items(): |
101 if v is None: | 92 if v is None: |
102 continue | 93 continue |
103 if not hasattr(v, '__file__'): | 94 if not hasattr(v, '__file__'): |
104 continue | 95 continue |
105 if not isinstance(v.__file__, str): | 96 if not isinstance(v.__file__, str): |