Show More
@@ -0,0 +1,53 b'' | |||
|
1 | from __future__ import with_statement | |
|
2 | ||
|
3 | import cProfile | |
|
4 | import pstats | |
|
5 | import cgi | |
|
6 | import pprint | |
|
7 | import threading | |
|
8 | ||
|
9 | from StringIO import StringIO | |
|
10 | ||
|
11 | class ProfilingMiddleware(object): | |
|
12 | def __init__(self, app): | |
|
13 | self.lock = threading.Lock() | |
|
14 | self.app = app | |
|
15 | ||
|
16 | ||
|
17 | def __call__(self, environ, start_response): | |
|
18 | with self.lock: | |
|
19 | profiler = cProfile.Profile() | |
|
20 | def run_app(*a, **kw): | |
|
21 | self.response = self.app(environ, start_response) | |
|
22 | ||
|
23 | profiler.runcall(run_app, environ, start_response) | |
|
24 | ||
|
25 | profiler.snapshot_stats() | |
|
26 | ||
|
27 | stats = pstats.Stats(profiler) | |
|
28 | stats.sort_stats('cumulative') | |
|
29 | ||
|
30 | # Redirect output | |
|
31 | out = StringIO() | |
|
32 | stats.stream = out | |
|
33 | ||
|
34 | stats.print_stats() | |
|
35 | ||
|
36 | resp = ''.join(self.response) | |
|
37 | ||
|
38 | # Lets at least only put this on html-like responses. | |
|
39 | if resp.strip().startswith('<'): | |
|
40 | ## The profiling info is just appended to the response. | |
|
41 | ## Browsers don't mind this. | |
|
42 | resp += '<pre style="text-align:left; border-top: 4px dashed red; padding: 1em;">' | |
|
43 | resp += cgi.escape(out.getvalue(), True) | |
|
44 | ||
|
45 | output = StringIO() | |
|
46 | pprint.pprint(environ, output, depth=3) | |
|
47 | ||
|
48 | resp += cgi.escape(output.getvalue(), True) | |
|
49 | resp += '</pre>' | |
|
50 | ||
|
51 | return resp | |
|
52 | ||
|
53 |
@@ -45,16 +45,16 b' cache_dir = %(here)s/data' | |||
|
45 | 45 | keys = root, routes, pylons_app, sqlalchemy |
|
46 | 46 | |
|
47 | 47 | [handlers] |
|
48 | keys = console | |
|
48 | keys = console,chainsaw | |
|
49 | 49 | |
|
50 | 50 | [formatters] |
|
51 | keys = generic | |
|
51 | keys = generic,xmllayout | |
|
52 | 52 | |
|
53 | 53 | ############# |
|
54 | 54 | ## LOGGERS ## |
|
55 | 55 | ############# |
|
56 | 56 | [logger_root] |
|
57 |
level = |
|
|
57 | level = NOTSET | |
|
58 | 58 | handlers = console |
|
59 | 59 | |
|
60 | 60 | [logger_routes] |
@@ -84,6 +84,12 b' args = (sys.stderr,)' | |||
|
84 | 84 | level = NOTSET |
|
85 | 85 | formatter = generic |
|
86 | 86 | |
|
87 | [handler_chainsaw] | |
|
88 | class = xmllayout.RawSocketHandler | |
|
89 | args = ('localhost', 4448) | |
|
90 | level = NOTSET | |
|
91 | formatter = xmllayout | |
|
92 | ||
|
87 | 93 | ################ |
|
88 | 94 | ## FORMATTERS ## |
|
89 | 95 | ################ |
@@ -92,3 +98,6 b' formatter = generic' | |||
|
92 | 98 | format = %(asctime)s,%(msecs)03d %(levelname)-5.5s [%(name)s] %(message)s |
|
93 | 99 | datefmt = %H:%M:%S |
|
94 | 100 | |
|
101 | [formatter_xmllayout] | |
|
102 | class = xmllayout.XMLLayout | |
|
103 |
General Comments 0
You need to be logged in to leave comments.
Login now