~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/middleware/profile.py

  • Committer: Matt Nordhoff
  • Date: 2009-10-17 06:19:40 UTC
  • mto: (329.2.2 trailing-whitespace)
  • mto: This revision was merged to the branch mainline in revision 392.
  • Revision ID: mnordhoff@mattnordhoff.com-20091017061940-w9tcvy0xs1irno3y
Some random PEP 8 and otehr stylistic changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Profiling middleware for Paste."""
 
2
 
 
3
import threading
 
4
 
 
5
from bzrlib.lsprof import profile
 
6
 
 
7
class LSProfMiddleware(object):
 
8
    """Paste middleware for profiling with lsprof."""
 
9
 
 
10
    def __init__(self, app, global_conf=None):
 
11
        self.app = app
 
12
        self.lock = threading.Lock()
 
13
        self.__count = 0
 
14
 
 
15
    def __run_app(self, environ, start_response):
 
16
        app_iter = self.app(environ, start_response)
 
17
        try:
 
18
            return list(app_iter)
 
19
        finally:
 
20
            if getattr(app_iter, 'close', None):
 
21
                app_iter.close()
 
22
 
 
23
    def __call__(self, environ, start_response):
 
24
        """Run a request."""
 
25
        self.lock.acquire()
 
26
        try:
 
27
            ret, stats = profile(self.__run_app, environ, start_response)
 
28
            self.__count += 1
 
29
            stats.save("%d-stats.callgrind" % self.__count, format="callgrind")
 
30
            return ret
 
31
        finally:
 
32
            self.lock.release()