Mercurial > hg
annotate mercurial/lsprof.py @ 12929:515c2786e1cf
revsets: let parents() return parents of working dir
This patch makes the 'set' argument to revset function parents() optional.
Like p1() and p2(), if no argument is given, returns the parent(s) of the
working directory.
Morally equivalent to 'p1()+p2()', as expected.
author | Kevin Bullock <kbullock@ringworld.org> |
---|---|
date | Thu, 04 Nov 2010 17:09:00 -0500 |
parents | 9905fb060ace |
children | b1dcc5ab86cd |
rev | line source |
---|---|
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
1 import sys |
6212 | 2 from _lsprof import Profiler, profiler_entry |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
3 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
4 __all__ = ['profile', 'Stats'] |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
5 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
6 def profile(f, *args, **kwds): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
7 """XXX docstring""" |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
8 p = Profiler() |
5992
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
9 p.enable(subcalls=True, builtins=True) |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
10 try: |
5992
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
11 f(*args, **kwds) |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
12 finally: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
13 p.disable() |
5992
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
14 return Stats(p.getstats()) |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
15 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
16 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
17 class Stats(object): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
18 """XXX docstring""" |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
19 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
20 def __init__(self, data): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
21 self.data = data |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
22 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
23 def sort(self, crit="inlinetime"): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
24 """XXX docstring""" |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
25 if crit not in profiler_entry.__dict__: |
7008
8fee8ff13d37
use Exception(args)-style raising consistently (py3k compatibility)
Peter Ruibal <peter.ruibal@intel.com>
parents:
6212
diff
changeset
|
26 raise ValueError("Can't sort by %s" % crit) |
9032
1fa80c5428b8
compat: use 'key' argument instead of 'cmp' when sorting a list
Alejandro Santos <alejolp@alejolp.com>
parents:
7875
diff
changeset
|
27 self.data.sort(key=lambda x: getattr(x, crit), reverse=True) |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
28 for e in self.data: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
29 if e.calls: |
9032
1fa80c5428b8
compat: use 'key' argument instead of 'cmp' when sorting a list
Alejandro Santos <alejolp@alejolp.com>
parents:
7875
diff
changeset
|
30 e.calls.sort(key=lambda x: getattr(x, crit), reverse=True) |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
31 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
32 def pprint(self, top=None, file=None, limit=None, climit=None): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
33 """XXX docstring""" |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
34 if file is None: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
35 file = sys.stdout |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
36 d = self.data |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
37 if top is not None: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
38 d = d[:top] |
5992
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
39 cols = "% 12s %12s %11.4f %11.4f %s\n" |
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
40 hcols = "% 12s %12s %12s %12s %s\n" |
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
41 file.write(hcols % ("CallCount", "Recursive", "Total(ms)", |
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
42 "Inline(ms)", "module:lineno(function)")) |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
43 count = 0 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
44 for e in d: |
5992
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
45 file.write(cols % (e.callcount, e.reccallcount, e.totaltime, |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
46 e.inlinetime, label(e.code))) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
47 count += 1 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
48 if limit is not None and count == limit: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
49 return |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
50 ccount = 0 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
51 if e.calls: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
52 for se in e.calls: |
5992
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
53 file.write(cols % ("+%s" % se.callcount, se.reccallcount, |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
54 se.totaltime, se.inlinetime, |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
55 "+%s" % label(se.code))) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
56 count += 1 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
57 ccount += 1 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
58 if limit is not None and count == limit: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
59 return |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
60 if climit is not None and ccount == climit: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
61 break |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
62 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
63 def freeze(self): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
64 """Replace all references to code objects with string |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
65 descriptions; this makes it possible to pickle the instance.""" |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
66 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
67 # this code is probably rather ickier than it needs to be! |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
68 for i in range(len(self.data)): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
69 e = self.data[i] |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
70 if not isinstance(e.code, str): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
71 self.data[i] = type(e)((label(e.code),) + e[1:]) |
5992
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
72 if e.calls: |
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
73 for j in range(len(e.calls)): |
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
74 se = e.calls[j] |
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
75 if not isinstance(se.code, str): |
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
76 e.calls[j] = type(se)((label(se.code),) + se[1:]) |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
77 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
78 _fn2mod = {} |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
79 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
80 def label(code): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
81 if isinstance(code, str): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
82 return code |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
83 try: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
84 mname = _fn2mod[code.co_filename] |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
85 except KeyError: |
9314
3f93f6838639
lsprof: make profile not die when imported modules changes (issue1774)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7875
diff
changeset
|
86 for k, v in list(sys.modules.iteritems()): |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
87 if v is None: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
88 continue |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
89 if not hasattr(v, '__file__'): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
90 continue |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
91 if not isinstance(v.__file__, str): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
92 continue |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
93 if v.__file__.startswith(code.co_filename): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
94 mname = _fn2mod[code.co_filename] = k |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
95 break |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
96 else: |
10339
23e608f42f2c
fix spaces/identation issues
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
9316
diff
changeset
|
97 mname = _fn2mod[code.co_filename] = '<%s>' % code.co_filename |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
98 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
99 return '%s:%d(%s)' % (mname, code.co_firstlineno, code.co_name) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
100 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
101 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
102 if __name__ == '__main__': |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
103 import os |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
104 sys.argv = sys.argv[1:] |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
105 if not sys.argv: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
106 print >> sys.stderr, "usage: lsprof.py <script> <arguments...>" |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
107 sys.exit(2) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
108 sys.path.insert(0, os.path.abspath(os.path.dirname(sys.argv[0]))) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
109 stats = profile(execfile, sys.argv[0], globals(), locals()) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
110 stats.sort() |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
111 stats.pprint() |