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