Mercurial > hg
annotate mercurial/lsprof.py @ 12184:025ca07351ea stable
convert: show example splice, author, and branch map entries in help
Also document that
- empty lines are skipped and comment are supported in author map
- whitespace is not allowed in branch map entries since we split on it
when parsing the file
author | Martin Geisler <mg@lazybytes.net> |
---|---|
date | Fri, 10 Sep 2010 00:22:46 +0200 |
parents | 23e608f42f2c |
children | 9905fb060ace |
rev | line source |
---|---|
5992
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
1 #! /usr/bin/env python |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
2 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
3 import sys |
6212 | 4 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
|
5 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
6 __all__ = ['profile', 'Stats'] |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
7 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
8 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
|
9 """XXX docstring""" |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
10 p = Profiler() |
5992
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
11 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
|
12 try: |
5992
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
13 f(*args, **kwds) |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
14 finally: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
15 p.disable() |
5992
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
16 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
|
17 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
18 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
19 class Stats(object): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
20 """XXX docstring""" |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
21 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
22 def __init__(self, data): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
23 self.data = data |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
24 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
25 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
|
26 """XXX docstring""" |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
27 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
|
28 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
|
29 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
|
30 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
|
31 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
|
32 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
|
33 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
34 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
|
35 """XXX docstring""" |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
36 if file is None: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
37 file = sys.stdout |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
38 d = self.data |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
39 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
|
40 d = d[:top] |
5992
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
41 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
|
42 hcols = "% 12s %12s %12s %12s %s\n" |
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
43 file.write(hcols % ("CallCount", "Recursive", "Total(ms)", |
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
44 "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
|
45 count = 0 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
46 for e in d: |
5992
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
47 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
|
48 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
|
49 count += 1 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
50 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
|
51 return |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
52 ccount = 0 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
53 if e.calls: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
54 for se in e.calls: |
5992
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
55 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
|
56 se.totaltime, se.inlinetime, |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
57 "+%s" % label(se.code))) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
58 count += 1 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
59 ccount += 1 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
60 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
|
61 return |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
62 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
|
63 break |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
64 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
65 def freeze(self): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
66 """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
|
67 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
|
68 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
69 # 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
|
70 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
|
71 e = self.data[i] |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
72 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
|
73 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
|
74 if e.calls: |
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
75 for j in range(len(e.calls)): |
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
76 se = e.calls[j] |
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
77 if not isinstance(se.code, str): |
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
78 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
|
79 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
80 _fn2mod = {} |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
81 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
82 def label(code): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
83 if isinstance(code, str): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
84 return code |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
85 try: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
86 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
|
87 except KeyError: |
9314
3f93f6838639
lsprof: make profile not die when imported modules changes (issue1774)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7875
diff
changeset
|
88 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
|
89 if v is None: |
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 hasattr(v, '__file__'): |
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 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
|
94 continue |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
95 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
|
96 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
|
97 break |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
98 else: |
10339
23e608f42f2c
fix spaces/identation issues
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
9316
diff
changeset
|
99 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
|
100 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
101 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
|
102 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
103 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
104 if __name__ == '__main__': |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
105 import os |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
106 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
|
107 if not sys.argv: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
108 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
|
109 sys.exit(2) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
110 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
|
111 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
|
112 stats.sort() |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
113 stats.pprint() |