Mercurial > kallithea
annotate rhodecode/lib/profiler.py @ 3774:60335b702a00 beta
invalidation: don't create CacheInvalidation records on startup
Creating the records early gave an advantage before lightweight was introduced.
With lightweight it is no longer necessary.
The records will be created on demand anyway and there is no reason to create and
maintain them before they are used.
author | Mads Kiilerich <madski@unity3d.com> |
---|---|
date | Wed, 03 Apr 2013 15:56:12 +0200 |
parents | 2c2bdaeca912 |
children |
rev | line source |
---|---|
15 | 1 from __future__ import with_statement |
2 | |
2215
2c2bdaeca912
code-review initial
Marcin Kuzminski <marcin@python-works.com>
parents:
1307
diff
changeset
|
3 import gc |
2c2bdaeca912
code-review initial
Marcin Kuzminski <marcin@python-works.com>
parents:
1307
diff
changeset
|
4 import objgraph |
15 | 5 import cProfile |
6 import pstats | |
7 import cgi | |
8 import pprint | |
9 import threading | |
10 | |
11 from StringIO import StringIO | |
12 | |
1307 | 13 |
15 | 14 class ProfilingMiddleware(object): |
15 def __init__(self, app): | |
16 self.lock = threading.Lock() | |
17 self.app = app | |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
18 |
15 | 19 def __call__(self, environ, start_response): |
20 with self.lock: | |
21 profiler = cProfile.Profile() | |
1307 | 22 |
15 | 23 def run_app(*a, **kw): |
24 self.response = self.app(environ, start_response) | |
25 | |
26 profiler.runcall(run_app, environ, start_response) | |
27 | |
28 profiler.snapshot_stats() | |
29 | |
30 stats = pstats.Stats(profiler) | |
2215
2c2bdaeca912
code-review initial
Marcin Kuzminski <marcin@python-works.com>
parents:
1307
diff
changeset
|
31 stats.sort_stats('calls') #cummulative |
15 | 32 |
33 # Redirect output | |
34 out = StringIO() | |
35 stats.stream = out | |
36 | |
37 stats.print_stats() | |
38 | |
39 resp = ''.join(self.response) | |
40 | |
41 # Lets at least only put this on html-like responses. | |
42 if resp.strip().startswith('<'): | |
43 ## The profiling info is just appended to the response. | |
44 ## Browsers don't mind this. | |
1307 | 45 resp += ('<pre style="text-align:left; ' |
46 'border-top: 4px dashed red; padding: 1em;">') | |
15 | 47 resp += cgi.escape(out.getvalue(), True) |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
48 |
2215
2c2bdaeca912
code-review initial
Marcin Kuzminski <marcin@python-works.com>
parents:
1307
diff
changeset
|
49 ct = objgraph.show_most_common_types() |
2c2bdaeca912
code-review initial
Marcin Kuzminski <marcin@python-works.com>
parents:
1307
diff
changeset
|
50 print ct |
2c2bdaeca912
code-review initial
Marcin Kuzminski <marcin@python-works.com>
parents:
1307
diff
changeset
|
51 |
2c2bdaeca912
code-review initial
Marcin Kuzminski <marcin@python-works.com>
parents:
1307
diff
changeset
|
52 resp += ct if ct else '---' |
2c2bdaeca912
code-review initial
Marcin Kuzminski <marcin@python-works.com>
parents:
1307
diff
changeset
|
53 |
15 | 54 output = StringIO() |
55 pprint.pprint(environ, output, depth=3) | |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
56 |
15 | 57 resp += cgi.escape(output.getvalue(), True) |
58 resp += '</pre>' | |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
59 |
15 | 60 return resp |