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