-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathcprofilev.py
executable file
·247 lines (201 loc) · 7.34 KB
/
cprofilev.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env python
import argparse
import bottle
import cProfile
import os
import pstats
import re
import sys
import threading
try:
from cStringIO import StringIO
except ImportError:
try:
from StringIO import StringIO
except ImportError:
# Python 3 compatibility.
from io import StringIO
VERSION = '1.0.7'
__doc__ = """\
An easier way to use cProfile.
Outputs a simpler html view of profiled stats.
Able to show stats while the code is still running!
"""
STATS_TEMPLATE = """\
<html>
<head>
<title>{{ title }} | cProfile Results</title>
</head>
<body>
<pre>{{ !stats }}</pre>
% if callers:
<h2>Called By:</h2>
<pre>{{ !callers }}</pre>
% if callees:
<h2>Called:</h2>
<pre>{{ !callees }}</pre>
</body>
</html>"""
SORT_KEY = 'sort'
FUNC_NAME_KEY = 'func_name'
class Stats(object):
"""Wrapper around pstats.Stats class."""
IGNORE_FUNC_NAMES = ['function', '']
DEFAULT_SORT_ARG = 'cumulative'
SORT_ARGS = {
'ncalls': 'calls',
'tottime': 'time',
'cumtime': 'cumulative',
'filename': 'module',
'lineno': 'nfl',
}
STATS_LINE_REGEX = r'(.*)\((.*)\)$'
HEADER_LINE_REGEX = r'ncalls|tottime|cumtime'
def __init__(self, profile_output=None, profile_obj=None):
self.profile = profile_output or profile_obj
self.stream = StringIO()
self.stats = pstats.Stats(self.profile, stream=self.stream)
def read_stream(self):
value = self.stream.getvalue()
self.stream.seek(0)
self.stream.truncate()
return value
def read(self):
output = self.read_stream()
lines = output.splitlines(True)
return "".join(map(self.process_line, lines))
@classmethod
def process_line(cls, line):
# Format header lines (such that clicking on a column header sorts by
# that column).
if re.search(cls.HEADER_LINE_REGEX, line):
for key, val in cls.SORT_ARGS.items():
url_link = bottle.template(
"<a href='{{ url }}'>{{ key }}</a>",
url=cls.get_updated_href(SORT_KEY, val),
key=key)
line = line.replace(key, url_link)
# Format stat lines (such that clicking on the function name drills into
# the function call).
match = re.search(cls.STATS_LINE_REGEX, line)
if match:
prefix = match.group(1)
func_name = match.group(2)
if func_name not in cls.IGNORE_FUNC_NAMES:
url_link = bottle.template(
"<a href='{{ url }}'>{{ func_name }}</a>",
url=cls.get_updated_href(FUNC_NAME_KEY, func_name),
func_name=func_name)
line = bottle.template(
"{{ prefix }}({{ !url_link }})\n",
prefix=prefix, url_link=url_link)
return line
@classmethod
def get_updated_href(cls, key, val):
href = '?'
query = dict(bottle.request.query)
query[key] = val
for key in query.keys():
href += '%s=%s&' % (key, query[key])
return href[:-1]
def show(self, restriction=''):
self.stats.print_stats(restriction)
return self
def show_callers(self, func_name):
self.stats.print_callers(func_name)
return self
def show_callees(self, func_name):
self.stats.print_callees(func_name)
return self
def sort(self, sort=''):
sort = sort or self.DEFAULT_SORT_ARG
self.stats.sort_stats(sort)
return self
class CProfileV(object):
def __init__(self, profile, title, address='127.0.0.1', port=4000):
self.profile = profile
self.title = title
self.port = port
self.address = address
# Bottle webserver.
self.app = bottle.Bottle()
self.app.route('/')(self.route_handler)
def route_handler(self):
self.stats = Stats(self.profile)
func_name = bottle.request.query.get(FUNC_NAME_KEY) or ''
sort = bottle.request.query.get(SORT_KEY) or ''
self.stats.sort(sort)
callers = self.stats.show_callers(func_name).read() if func_name else ''
callees = self.stats.show_callees(func_name).read() if func_name else ''
data = {
'title': self.title,
'stats': self.stats.sort(sort).show(func_name).read(),
'callers': callers,
'callees': callees,
}
return bottle.template(STATS_TEMPLATE, **data)
def start(self):
self.app.run(host=self.address, port=self.port, quiet=True)
def main():
parser = argparse.ArgumentParser(
description='An easier way to use cProfile.',
usage='%(prog)s [--version] [-a ADDRESS] [-p PORT] scriptfile [arg] ...',
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('--version', action='version', version=VERSION)
parser.add_argument('-a', '--address', type=str, default='127.0.0.1',
help='The address to listen on. (defaults to 127.0.0.1).')
parser.add_argument('-p', '--port', type=int, default=4000,
help='The port to listen on. (defaults to 4000).')
# Preserve v0 functionality using a flag.
parser.add_argument('-f', '--file', type=str,
help='cProfile output to view.\nIf specified, the scriptfile provided will be ignored.')
parser.add_argument('remainder', nargs=argparse.REMAINDER,
help='The python script file to run and profile.',
metavar="scriptfile")
args = parser.parse_args()
if not sys.argv[1:]:
parser.print_help()
sys.exit(2)
info = '[cProfileV]: cProfile output available at http://%s:%s' % \
(args.address, args.port)
# v0 mode: Render profile output.
if args.file:
# Note: The info message is sent to stderr to keep stdout clean in case
# the profiled script writes some output to stdout
sys.stderr.write(info + "\n")
cprofilev = CProfileV(args.file, title=args.file, address=args.address, port=args.port)
cprofilev.start()
return
# v1 mode: Start script and render profile output.
sys.argv[:] = args.remainder
if len(args.remainder) < 0:
parser.print_help()
sys.exit(2)
profile = cProfile.Profile()
progname = args.remainder[0]
sys.path.insert(0, os.path.dirname(progname))
with open(progname, 'rb') as fp:
try:
code = compile(fp.read(), progname, 'exec')
except (TypeError, SyntaxError) as e:
sys.stderr.write(
'[cProfileV]: there was a problem compiling your scriptfile.' + '\n\n'
)
parser.print_help()
sys.exit(2)
# Note: The info message is sent to stderr to keep stdout clean in case
# the profiled script writes some output to stdout
sys.stderr.write(info + "\n")
globs = {
'__file__': progname,
'__name__': '__main__',
'__package__': None,
}
# Start the given program in a separate thread.
progthread = threading.Thread(target=profile.runctx, args=(code, globs, None))
progthread.setDaemon(True)
progthread.start()
cprofilev = CProfileV(profile, title=progname, address=args.address, port=args.port)
cprofilev.start()
if __name__ == '__main__':
main()