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