Show More
@@ -1,113 +1,111 b'' | |||||
1 | #! /usr/bin/env python |
|
|||
2 |
|
||||
3 |
|
|
1 | import sys | |
4 | from _lsprof import Profiler, profiler_entry |
|
2 | from _lsprof import Profiler, profiler_entry | |
5 |
|
3 | |||
6 | __all__ = ['profile', 'Stats'] |
|
4 | __all__ = ['profile', 'Stats'] | |
7 |
|
5 | |||
8 | def profile(f, *args, **kwds): |
|
6 | def profile(f, *args, **kwds): | |
9 | """XXX docstring""" |
|
7 | """XXX docstring""" | |
10 | p = Profiler() |
|
8 | p = Profiler() | |
11 | p.enable(subcalls=True, builtins=True) |
|
9 | p.enable(subcalls=True, builtins=True) | |
12 | try: |
|
10 | try: | |
13 | f(*args, **kwds) |
|
11 | f(*args, **kwds) | |
14 | finally: |
|
12 | finally: | |
15 | p.disable() |
|
13 | p.disable() | |
16 | return Stats(p.getstats()) |
|
14 | return Stats(p.getstats()) | |
17 |
|
15 | |||
18 |
|
16 | |||
19 | class Stats(object): |
|
17 | class Stats(object): | |
20 | """XXX docstring""" |
|
18 | """XXX docstring""" | |
21 |
|
19 | |||
22 | def __init__(self, data): |
|
20 | def __init__(self, data): | |
23 | self.data = data |
|
21 | self.data = data | |
24 |
|
22 | |||
25 | def sort(self, crit="inlinetime"): |
|
23 | def sort(self, crit="inlinetime"): | |
26 | """XXX docstring""" |
|
24 | """XXX docstring""" | |
27 | if crit not in profiler_entry.__dict__: |
|
25 | if crit not in profiler_entry.__dict__: | |
28 | raise ValueError("Can't sort by %s" % crit) |
|
26 | raise ValueError("Can't sort by %s" % crit) | |
29 | self.data.sort(key=lambda x: getattr(x, crit), reverse=True) |
|
27 | self.data.sort(key=lambda x: getattr(x, crit), reverse=True) | |
30 | for e in self.data: |
|
28 | for e in self.data: | |
31 | if e.calls: |
|
29 | if e.calls: | |
32 | e.calls.sort(key=lambda x: getattr(x, crit), reverse=True) |
|
30 | e.calls.sort(key=lambda x: getattr(x, crit), reverse=True) | |
33 |
|
31 | |||
34 | def pprint(self, top=None, file=None, limit=None, climit=None): |
|
32 | def pprint(self, top=None, file=None, limit=None, climit=None): | |
35 | """XXX docstring""" |
|
33 | """XXX docstring""" | |
36 | if file is None: |
|
34 | if file is None: | |
37 | file = sys.stdout |
|
35 | file = sys.stdout | |
38 | d = self.data |
|
36 | d = self.data | |
39 | if top is not None: |
|
37 | if top is not None: | |
40 | d = d[:top] |
|
38 | d = d[:top] | |
41 | cols = "% 12s %12s %11.4f %11.4f %s\n" |
|
39 | cols = "% 12s %12s %11.4f %11.4f %s\n" | |
42 | hcols = "% 12s %12s %12s %12s %s\n" |
|
40 | hcols = "% 12s %12s %12s %12s %s\n" | |
43 | file.write(hcols % ("CallCount", "Recursive", "Total(ms)", |
|
41 | file.write(hcols % ("CallCount", "Recursive", "Total(ms)", | |
44 | "Inline(ms)", "module:lineno(function)")) |
|
42 | "Inline(ms)", "module:lineno(function)")) | |
45 | count = 0 |
|
43 | count = 0 | |
46 | for e in d: |
|
44 | for e in d: | |
47 | file.write(cols % (e.callcount, e.reccallcount, e.totaltime, |
|
45 | file.write(cols % (e.callcount, e.reccallcount, e.totaltime, | |
48 | e.inlinetime, label(e.code))) |
|
46 | e.inlinetime, label(e.code))) | |
49 | count += 1 |
|
47 | count += 1 | |
50 | if limit is not None and count == limit: |
|
48 | if limit is not None and count == limit: | |
51 | return |
|
49 | return | |
52 | ccount = 0 |
|
50 | ccount = 0 | |
53 | if e.calls: |
|
51 | if e.calls: | |
54 | for se in e.calls: |
|
52 | for se in e.calls: | |
55 | file.write(cols % ("+%s" % se.callcount, se.reccallcount, |
|
53 | file.write(cols % ("+%s" % se.callcount, se.reccallcount, | |
56 | se.totaltime, se.inlinetime, |
|
54 | se.totaltime, se.inlinetime, | |
57 | "+%s" % label(se.code))) |
|
55 | "+%s" % label(se.code))) | |
58 | count += 1 |
|
56 | count += 1 | |
59 | ccount += 1 |
|
57 | ccount += 1 | |
60 | if limit is not None and count == limit: |
|
58 | if limit is not None and count == limit: | |
61 | return |
|
59 | return | |
62 | if climit is not None and ccount == climit: |
|
60 | if climit is not None and ccount == climit: | |
63 | break |
|
61 | break | |
64 |
|
62 | |||
65 | def freeze(self): |
|
63 | def freeze(self): | |
66 | """Replace all references to code objects with string |
|
64 | """Replace all references to code objects with string | |
67 | descriptions; this makes it possible to pickle the instance.""" |
|
65 | descriptions; this makes it possible to pickle the instance.""" | |
68 |
|
66 | |||
69 | # this code is probably rather ickier than it needs to be! |
|
67 | # this code is probably rather ickier than it needs to be! | |
70 | for i in range(len(self.data)): |
|
68 | for i in range(len(self.data)): | |
71 | e = self.data[i] |
|
69 | e = self.data[i] | |
72 | if not isinstance(e.code, str): |
|
70 | if not isinstance(e.code, str): | |
73 | self.data[i] = type(e)((label(e.code),) + e[1:]) |
|
71 | self.data[i] = type(e)((label(e.code),) + e[1:]) | |
74 | if e.calls: |
|
72 | if e.calls: | |
75 | for j in range(len(e.calls)): |
|
73 | for j in range(len(e.calls)): | |
76 | se = e.calls[j] |
|
74 | se = e.calls[j] | |
77 | if not isinstance(se.code, str): |
|
75 | if not isinstance(se.code, str): | |
78 | e.calls[j] = type(se)((label(se.code),) + se[1:]) |
|
76 | e.calls[j] = type(se)((label(se.code),) + se[1:]) | |
79 |
|
77 | |||
80 | _fn2mod = {} |
|
78 | _fn2mod = {} | |
81 |
|
79 | |||
82 | def label(code): |
|
80 | def label(code): | |
83 | if isinstance(code, str): |
|
81 | if isinstance(code, str): | |
84 | return code |
|
82 | return code | |
85 | try: |
|
83 | try: | |
86 | mname = _fn2mod[code.co_filename] |
|
84 | mname = _fn2mod[code.co_filename] | |
87 | except KeyError: |
|
85 | except KeyError: | |
88 | for k, v in list(sys.modules.iteritems()): |
|
86 | for k, v in list(sys.modules.iteritems()): | |
89 | if v is None: |
|
87 | if v is None: | |
90 | continue |
|
88 | continue | |
91 | if not hasattr(v, '__file__'): |
|
89 | if not hasattr(v, '__file__'): | |
92 | continue |
|
90 | continue | |
93 | if not isinstance(v.__file__, str): |
|
91 | if not isinstance(v.__file__, str): | |
94 | continue |
|
92 | continue | |
95 | if v.__file__.startswith(code.co_filename): |
|
93 | if v.__file__.startswith(code.co_filename): | |
96 | mname = _fn2mod[code.co_filename] = k |
|
94 | mname = _fn2mod[code.co_filename] = k | |
97 | break |
|
95 | break | |
98 | else: |
|
96 | else: | |
99 | mname = _fn2mod[code.co_filename] = '<%s>' % code.co_filename |
|
97 | mname = _fn2mod[code.co_filename] = '<%s>' % code.co_filename | |
100 |
|
98 | |||
101 | return '%s:%d(%s)' % (mname, code.co_firstlineno, code.co_name) |
|
99 | return '%s:%d(%s)' % (mname, code.co_firstlineno, code.co_name) | |
102 |
|
100 | |||
103 |
|
101 | |||
104 | if __name__ == '__main__': |
|
102 | if __name__ == '__main__': | |
105 | import os |
|
103 | import os | |
106 | sys.argv = sys.argv[1:] |
|
104 | sys.argv = sys.argv[1:] | |
107 | if not sys.argv: |
|
105 | if not sys.argv: | |
108 | print >> sys.stderr, "usage: lsprof.py <script> <arguments...>" |
|
106 | print >> sys.stderr, "usage: lsprof.py <script> <arguments...>" | |
109 | sys.exit(2) |
|
107 | sys.exit(2) | |
110 | sys.path.insert(0, os.path.abspath(os.path.dirname(sys.argv[0]))) |
|
108 | sys.path.insert(0, os.path.abspath(os.path.dirname(sys.argv[0]))) | |
111 | stats = profile(execfile, sys.argv[0], globals(), locals()) |
|
109 | stats = profile(execfile, sys.argv[0], globals(), locals()) | |
112 | stats.sort() |
|
110 | stats.sort() | |
113 | stats.pprint() |
|
111 | stats.pprint() |
General Comments 0
You need to be logged in to leave comments.
Login now