29
28
def profile(f, *args, **kwds):
30
"""Run a function profile.
32
Exceptions are not caught: If you need stats even when exceptions are to be
33
raised, passing in a closure that will catch the exceptions and transform
34
them appropriately for your driver function.
36
:return: The functions return value and a stats object.
38
30
global _g_threadmap
40
32
p.enable(subcalls=True)
46
38
for pp in _g_threadmap.values():
48
40
threading.setprofile(None)
51
43
for tid, pp in _g_threadmap.items():
52
44
threads[tid] = Stats(pp.getstats(), {})
126
118
otherwise the format is given by the filename extension.
128
120
if format is None:
129
basename = os.path.basename(filename)
130
if basename.startswith('callgrind.out'):
121
if filename.startswith('callgrind.out'):
131
122
format = "callgrind"
133
124
ext = os.path.splitext(filename)[1]
152
143
This code is taken from http://ddaa.net/blog/python/lsprof-calltree
153
144
with the changes made by J.P. Calderone and Itamar applied. Note that
154
isinstance(code, str) needs to be used at times to determine if the code
145
isinstance(code, str) needs to be used at times to determine if the code
155
146
object is actually an external code object (with a filename, etc.) or
156
147
a Python built-in.
172
163
for entry in self.data:
173
164
totaltime = int(entry.totaltime * 1000)
174
165
max_cost = max(max_cost, totaltime)
175
self.out_file.write('summary: %d\n' % (max_cost,))
166
print >> self.out_file, 'summary: %d' % (max_cost,)
177
168
def _entry(self, entry):
178
169
out_file = self.out_file
179
170
code = entry.code
180
171
inlinetime = int(entry.inlinetime * 1000)
181
#out_file.write('ob=%s\n' % (code.co_filename,))
182
if isinstance(code, str):
183
out_file.write('fi=~\n')
185
out_file.write('fi=%s\n' % (code.co_filename,))
186
out_file.write('fn=%s\n' % (label(code, True),))
187
if isinstance(code, str):
188
out_file.write('0 %s\n' % (inlinetime,))
190
out_file.write('%d %d\n' % (code.co_firstlineno, inlinetime))
172
#print >> out_file, 'ob=%s' % (code.co_filename,)
173
if isinstance(code, str):
174
print >> out_file, 'fi=~'
176
print >> out_file, 'fi=%s' % (code.co_filename,)
177
print >> out_file, 'fn=%s' % (label(code, True),)
178
if isinstance(code, str):
179
print >> out_file, '0 ', inlinetime
181
print >> out_file, '%d %d' % (code.co_firstlineno, inlinetime)
191
182
# recursive calls are counted in entry.calls
193
184
calls = entry.calls
199
190
lineno = code.co_firstlineno
200
191
for subentry in calls:
201
192
self._subentry(lineno, subentry)
204
195
def _subentry(self, lineno, subentry):
205
196
out_file = self.out_file
206
197
code = subentry.code
207
198
totaltime = int(subentry.totaltime * 1000)
208
#out_file.write('cob=%s\n' % (code.co_filename,))
209
out_file.write('cfn=%s\n' % (label(code, True),))
199
#print >> out_file, 'cob=%s' % (code.co_filename,)
200
print >> out_file, 'cfn=%s' % (label(code, True),)
210
201
if isinstance(code, str):
211
out_file.write('cfi=~\n')
212
out_file.write('calls=%d 0\n' % (subentry.callcount,))
202
print >> out_file, 'cfi=~'
203
print >> out_file, 'calls=%d 0' % (subentry.callcount,)
214
out_file.write('cfi=%s\n' % (code.co_filename,))
215
out_file.write('calls=%d %d\n' % (
216
subentry.callcount, code.co_firstlineno))
217
out_file.write('%d %d\n' % (lineno, totaltime))
205
print >> out_file, 'cfi=%s' % (code.co_filename,)
206
print >> out_file, 'calls=%d %d' % (
207
subentry.callcount, code.co_firstlineno)
208
print >> out_file, '%d %d' % (lineno, totaltime)
247
238
sys.argv = sys.argv[1:]
249
sys.stderr.write("usage: lsprof.py <script> <arguments...>\n")
240
print >> sys.stderr, "usage: lsprof.py <script> <arguments...>"
251
242
sys.path.insert(0, os.path.abspath(os.path.dirname(sys.argv[0])))
252
243
stats = profile(execfile, sys.argv[0], globals(), locals())