author | Madhusudan.C.S <madhusudancs@gmail.com> |
Sun, 27 Sep 2009 01:23:58 +0530 | |
changeset 2978 | 2b3c39483f6f |
parent 2857 | bc793800116e |
permissions | -rw-r--r-- |
2832 | 1 |
import cProfile |
2 |
import ppstats |
|
3 |
||
4 |
from google.appengine.ext import webapp |
|
5 |
from google.appengine.api import memcache |
|
6 |
import google.appengine.ext.webapp.util |
|
7 |
||
2857
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
8 |
from email.MIMEMultipart import MIMEMultipart |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
9 |
from email.Message import Message |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
10 |
|
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
11 |
import httplib |
2832 | 12 |
import logging |
2857
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
13 |
import os.path |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
14 |
import random |
2832 | 15 |
import re |
16 |
import string |
|
17 |
import zlib |
|
18 |
||
19 |
mc_client = memcache.Client() |
|
20 |
||
21 |
alphanumeric = string.letters + string.digits |
|
22 |
||
23 |
global_profiler = None |
|
24 |
||
25 |
class GAEProfiler(object): |
|
26 |
_save_every = 10 |
|
27 |
||
28 |
def __init__(self): |
|
29 |
self.is_profiling = False |
|
30 |
self._profiler = None |
|
31 |
self.num_requests = 0 |
|
32 |
self.requests_profiled = 0 |
|
33 |
self.request_regex = None |
|
34 |
self.profile_key = ''.join([random.choice(alphanumeric) for x in range(4)]) |
|
35 |
||
36 |
def start_profiling(self, request_regex=None, num_requests=0): |
|
37 |
"start profiling with this object, setting # of requests and filter" |
|
38 |
if self.is_profiling: |
|
39 |
return |
|
40 |
||
41 |
self.is_profiling = True |
|
42 |
if self._profiler is None: |
|
43 |
self._profiler = cProfile.Profile() |
|
44 |
self.num_requests = num_requests |
|
45 |
if request_regex: |
|
46 |
self.request_regex = re.compile(request_regex) |
|
47 |
||
48 |
def stop_profiling(self): |
|
49 |
self.is_profiling = False |
|
50 |
||
51 |
def resume_profiling(self): |
|
52 |
self.is_profiling = True |
|
53 |
||
54 |
def has_profiler(self): |
|
55 |
return self._profiler is not None |
|
56 |
||
57 |
def get_pstats(self): |
|
58 |
"return a ppstats object from current profile data" |
|
59 |
gae_base_dir = '/'.join(webapp.__file__.split('/')[:-5]) |
|
60 |
sys_base_dir = '/'.join(logging.__file__.split('/')[:-2]) |
|
61 |
||
62 |
stats = ppstats.Stats(self._profiler) |
|
63 |
stats.hide_directory(gae_base_dir, 'GAEHome') |
|
64 |
stats.hide_directory(sys_base_dir, 'SysHome') |
|
65 |
stats.strip_dirs() |
|
66 |
return stats |
|
67 |
||
68 |
def runcall(self, func, *args, **kwargs): |
|
69 |
"profile one call, incrementing requests_profiled and maybe saving stats" |
|
70 |
self.requests_profiled += 1 |
|
71 |
if self._profiler: |
|
72 |
ret = self._profiler.runcall(func, *args, **kwargs) |
|
73 |
else: |
|
74 |
ret = func(*args, **kwargs) |
|
75 |
||
76 |
# if (self.requests_profiled % self._save_every) == 0 or \ |
|
77 |
# self.requests_profiled == self.num_requests: |
|
78 |
# self.save_pstats_to_memcache() |
|
79 |
self.save_pstats_to_memcache() |
|
80 |
return ret |
|
81 |
||
82 |
def should_profile_request(self): |
|
83 |
"check for # of requests profiled and that SCRIPT_NAME matches regex" |
|
84 |
env = dict(os.environ) |
|
85 |
script_name = env.get('SCRIPT_NAME', '') |
|
86 |
logging.info(script_name) |
|
87 |
||
88 |
if self.num_requests and self.requests_profiled >= self.num_requests: |
|
89 |
return False |
|
90 |
||
91 |
if self.request_regex and not self.request_regex.search(script_name): |
|
92 |
return False |
|
93 |
||
94 |
return True |
|
95 |
||
96 |
def save_pstats_to_memcache(self): |
|
97 |
"save stats from profiler object to memcache" |
|
98 |
ps = self.get_pstats() |
|
99 |
output = ps.dump_stats_pickle() |
|
100 |
compressed_data = zlib.compress(output, 3) |
|
101 |
cache_key = cache_key_for_profile(self.profile_key) |
|
102 |
mc_client.set(cache_key, compressed_data) |
|
103 |
logging.info("Saved pstats to memcache with key %s" % cache_key) |
|
104 |
||
105 |
||
106 |
||
107 |
def get_global_profiler(): |
|
108 |
global global_profiler |
|
109 |
if not global_profiler: |
|
110 |
global_profiler = GAEProfiler() |
|
111 |
||
112 |
return global_profiler |
|
113 |
||
2857
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
114 |
def new_global_profiler(): |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
115 |
global global_profiler |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
116 |
global_profiler = GAEProfiler() |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
117 |
return global_profiler |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
118 |
|
2832 | 119 |
def cache_key_for_profile(profile_key): |
120 |
"generate a memcache key" |
|
121 |
return "ProfileData.%s" % profile_key |
|
122 |
||
123 |
def load_pstats_from_memcache(profile_key): |
|
124 |
"retrieve ppstats object" |
|
125 |
mc_data = mc_client.get(cache_key_for_profile(profile_key)) |
|
126 |
if not mc_data: |
|
127 |
return None |
|
128 |
||
129 |
return ppstats.from_gz(mc_data) |
|
130 |
||
131 |
def get_stats_from_global_or_request(request_obj): |
|
132 |
"get pstats for a key, or the global pstats" |
|
133 |
key = request_obj.get('key', '') |
|
134 |
if key: |
|
2857
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
135 |
gp = GAEProfiler() |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
136 |
gp.profile_obj = load_pstats_from_memcache(key) |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
137 |
gp.profile_key = key |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
138 |
return gp |
2832 | 139 |
else: |
140 |
gp = get_global_profiler() |
|
141 |
if not gp.has_profiler(): |
|
142 |
return None |
|
2857
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
143 |
return gp |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
144 |
|
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
145 |
def mime_upload_data_as_file(field_name, filename, body): |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
146 |
part = Message() |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
147 |
part['Content-Disposition'] = 'form-data; name="%s"; filename="%s"' % (field_name, filename) |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
148 |
part['Content-Transfer-Encoding'] = 'binary' |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
149 |
part['Content-Type'] = 'application/octet-stream' |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
150 |
part['Content-Length'] = str(len(body)) |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
151 |
part.set_payload(body) |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
152 |
return part |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
153 |
|
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
154 |
def mime_form_value(name, value): |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
155 |
part = Message() |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
156 |
part['Content-Disposition'] = 'form-data; name="%s"' % name |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
157 |
part.set_payload(value) |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
158 |
return part |
2832 | 159 |
|
160 |
class show_profile(webapp.RequestHandler): |
|
161 |
def get(self): |
|
162 |
ps = get_stats_from_global_or_request(self.request) |
|
163 |
if not ps: |
|
164 |
self.response.out.write("<body><html><h3>No profiler.</h3><html></body>") |
|
165 |
return |
|
166 |
||
2857
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
167 |
ps.profile_obj.set_output(self.response.out) |
2832 | 168 |
sort = self.request.get('sort', 'time') |
2857
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
169 |
ps.profile_obj.sort_stats(sort) |
2832 | 170 |
self.response.out.write("<body><html><pre>\n") |
2857
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
171 |
ps.profile_obj.print_stats(30) |
2832 | 172 |
self.response.out.write("</pre></html></body>") |
173 |
||
174 |
class download_profile_data(webapp.RequestHandler): |
|
175 |
def get(self): |
|
176 |
ps = get_stats_from_global_or_request(self.request) |
|
177 |
if not ps: |
|
178 |
self.response.out.write("<body><html><h3>No profiler.</h3><html></body>") |
|
179 |
return |
|
180 |
||
2857
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
181 |
output = ps.profile_obj.dump_stats_pickle() |
2832 | 182 |
|
183 |
self.response.headers['Content-Type'] = 'application/octet-stream' |
|
184 |
||
185 |
self.response.out.write(output) |
|
186 |
||
2857
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
187 |
class send_profile_data(webapp.RequestHandler): |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
188 |
def get(self): |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
189 |
ps = get_stats_from_global_or_request(self.request) |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
190 |
if not ps: |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
191 |
self.response.out.write("<body><html><h3>No profiler.</h3><html></body>") |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
192 |
return |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
193 |
|
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
194 |
dest = self.request.get('dest', '') |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
195 |
if not dest: |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
196 |
self.response.out.write("<body><html>No destination</html></body>") |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
197 |
|
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
198 |
upload_form = MIMEMultipart('form-data') |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
199 |
|
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
200 |
upload_filename = 'profile.%s.pstats' % ps.profile_key |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
201 |
upload_field_name = 'profile_file' |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
202 |
|
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
203 |
upload_form.attach(mime_upload_data_as_file('profile_file', upload_field_name, zlib.compress(ps.profile_obj.dump_stats_pickle()))) |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
204 |
upload_form.attach(mime_form_value('key_only', '1')) |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
205 |
|
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
206 |
http_conn = httplib.HTTPConnection(dest) |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
207 |
http_conn.connect() |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
208 |
http_conn.request('POST', '/upload_profile', upload_form.as_string(), |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
209 |
{'Content-Type': 'multipart/form-data; boundary=%s' % upload_form.get_boundary()}) |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
210 |
|
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
211 |
http_resp = http_conn.getresponse() |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
212 |
remote_data = http_resp.read() |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
213 |
if http_resp.status == 200: |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
214 |
remote_url = "http://%s/view_profile?key=%s" % (dest, remote_data) |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
215 |
self.response.out.write("<html><body>Success! <a href='%s'>%s</a></body></html>" % (remote_url, remote_url)) |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
216 |
else: |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
217 |
self.response.out.write("Failure!\n%s: %s\n%s" % (http_resp.status, http_resp.reason, remote_data)) |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
218 |
|
2832 | 219 |
class show_profiler_status(webapp.RequestHandler): |
220 |
def get(self): |
|
221 |
gp = get_global_profiler() |
|
222 |
if not gp.has_profiler: |
|
223 |
self.response.out.write("<body><html><h3>No profiler.</h3><html></body>") |
|
224 |
return |
|
225 |
||
226 |
self.response.out.write("<html><body>") |
|
227 |
self.response.out.write("<b>Currently profiling:</b> %s<br>" % gp.is_profiling) |
|
228 |
self.response.out.write("<b>Profile Key</b>: %s<br>" % gp.profile_key) |
|
229 |
self.response.out.write("<b>Requests profiled so far:</b> %s<br>" % gp.requests_profiled) |
|
230 |
self.response.out.write("<b>Requests to profile:</b> %s<br>" % gp.num_requests) |
|
231 |
self.response.out.write("<b>Request regex:</b> %s<br>" % gp.request_regex) |
|
232 |
self.response.out.write("</body></html>") |
|
233 |
||
234 |
class start_profiler(webapp.RequestHandler): |
|
235 |
def get(self): |
|
2857
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
236 |
gp = new_global_profiler() |
2832 | 237 |
gp.start_profiling() |
2857
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
238 |
self.response.headers['Content-Type'] = "text/plain" |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
239 |
self.response.out.write("Started profiling (key: %s).\n" % gp.profile_key) |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
240 |
self.response.out.write("Retrieve saved results at <a href='/profiler/show?key=%(key)s'>/profiler/show?key=%(key)s).\n" % {'key':gp.profile_key}) |
2832 | 241 |
|
242 |
class stop_profiler(webapp.RequestHandler): |
|
243 |
def get(self): |
|
244 |
gp = get_global_profiler() |
|
245 |
gp.stop_profiling() |
|
246 |
self.request.out.write("Content-Type: text/plain\n\n") |
|
247 |
self.request.out.write("done.") |
|
248 |
||
249 |
class save_profile_data(webapp.RequestHandler): |
|
250 |
def get(self): |
|
251 |
gp = get_global_profiler() |
|
252 |
||
253 |
||
254 |
def _add_our_endpoints(application): |
|
255 |
"insert our URLs into the application map" |
|
256 |
url_mapping = [(regex.pattern, handler) for (regex, handler) in application._url_mapping] |
|
257 |
return webapp.WSGIApplication(url_mapping, debug=True) |
|
258 |
||
259 |
# |
|
260 |
# wrapper to for webapp applications |
|
261 |
# |
|
262 |
def run_wsgi_app(application): |
|
263 |
"proxy webapp.util's call to profile when needed" |
|
264 |
gp = get_global_profiler() |
|
265 |
if gp.is_profiling and gp.should_profile_request(): |
|
266 |
return gp.runcall(google.appengine.ext.webapp.util.run_wsgi_app, *(application,)) |
|
267 |
else: |
|
268 |
return google.appengine.ext.webapp.util.run_wsgi_app(application) |
|
269 |
||
270 |
# |
|
271 |
# middleware for django applications |
|
272 |
# |
|
273 |
||
274 |
class ProfileMiddleware(object): |
|
275 |
def __init__(self): |
|
276 |
self.profiler = None |
|
277 |
||
278 |
def process_request(self, request): |
|
279 |
self.profiler = get_global_profiler() |
|
280 |
||
281 |
def process_view(self, request, callback, callback_args, callback_kwargs): |
|
282 |
if self.profiler.is_profiling: |
|
283 |
return self.profiler.runcall(callback, request, *callback_args, **callback_kwargs) |
|
284 |
||
285 |
application = webapp.WSGIApplication( |
|
286 |
[('/profiler/start', start_profiler), |
|
287 |
('/profiler/stop', stop_profiler), |
|
288 |
('/profiler/show', show_profile), |
|
289 |
('/profiler/download', download_profile_data), |
|
290 |
('/profiler/status', show_profiler_status), |
|
2857
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2848
diff
changeset
|
291 |
('/profiler/send', send_profile_data), |
2832 | 292 |
], |
293 |
debug=True) |
|
294 |
||
295 |
||
296 |
def main(): |
|
297 |
google.appengine.ext.webapp.util.run_wsgi_app(application) |
|
298 |
||
299 |
if __name__ == '__main__': |
|
300 |
main() |