~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/middleware/profile.py

  • Committer: Guillermo Gonzalez
  • Date: 2008-09-10 23:12:59 UTC
  • mfrom: (221 trunk)
  • mto: (217.1.9 logging)
  • mto: This revision was merged to the branch mainline in revision 226.
  • Revision ID: guillo.gonzo@gmail.com-20080910231259-4wnfmu30f4n90w6x
 * merge with trunk (resolve conflicts in NEWS and serve-branches)

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