/loggerhead/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerhead/trunk

« back to all changes in this revision

Viewing changes to loggerhead/middleware/profile.py

  • Committer: Martin Albisetti
  • Date: 2008-11-14 21:45:00 UTC
  • mfrom: (232.1.8 dev-tools)
  • Revision ID: martin.albisetti@canonical.com-20081114214500-0fm3t669c8l7ykzr
Added --profile switch to profile Loggerhead. (Paul Hummer, Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
'''Profiling middleware for paste.'''
 
2
import cgi
 
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()
 
33