~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/middleware/profile.py

  • Committer: Matt Nordhoff
  • Date: 2009-06-17 23:10:07 UTC
  • mto: This revision was merged to the branch mainline in revision 367.
  • Revision ID: mnordhoff@mattnordhoff.com-20090617231007-09y132sof4ix98po
Obey the http_serve setting for serving over hpss

Show diffs side-by-side

added added

removed removed

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