summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorianb <devnull@localhost>2005-08-22 22:14:25 +0000
committerianb <devnull@localhost>2005-08-22 22:14:25 +0000
commitc2e03f3152e18cb4c7b239809aa44d9f5d23ac28 (patch)
tree404b2eae31e254f155f8d07406be56592ddede32
parent467705922e042dfc0c5f41c36115422f86c613ae (diff)
downloadpaste-c2e03f3152e18cb4c7b239809aa44d9f5d23ac28.tar.gz
paste.deployified profilemiddleware
-rw-r--r--paste/profilemiddleware.py34
-rw-r--r--setup.py1
-rw-r--r--tests/test_profilemiddleware.py20
3 files changed, 35 insertions, 20 deletions
diff --git a/paste/profilemiddleware.py b/paste/profilemiddleware.py
index 415ec2b..69d5bce 100644
--- a/paste/profilemiddleware.py
+++ b/paste/profilemiddleware.py
@@ -31,21 +31,15 @@ class ProfileMiddleware(object):
style = ('background-color: #ff9; color: #000; '
'border: 2px solid #000; padding: 5px;')
- config_profile_log_filename = metadata.Config("""
- The filename to write profiling data to.
- """, default='profile.log')
- config_profile_limit = metadata.Config("""
- Only this number of function calls will be displayed.
- """, default=40)
-
- def __init__(self, application):
- self.application = application
+ def __init__(self, app, global_conf,
+ log_filename='profile.log.tmp',
+ limit=40):
+ self.app = app
self.lock = threading.Lock()
+ self.log_filename = log_filename
+ self.limit = limit
def __call__(self, environ, start_response):
- prof_filename = environ['paste.config'].get(
- 'profile_log_filename', 'profile.log')
- display_limit = environ['paste.config'].get('profile_limit', 40)
response = []
body = []
def replace_start_response(status, headers):
@@ -53,10 +47,10 @@ class ProfileMiddleware(object):
start_response(status, headers)
return body.append
def run_app():
- body.extend(self.application(environ, replace_start_response))
+ body.extend(self.app(environ, replace_start_response))
self.lock.acquire()
try:
- prof = hotshot.Profile(prof_filename)
+ prof = hotshot.Profile(self.log_filename)
prof.addinfo('URL', environ.get('PATH_INFO', ''))
try:
prof.runcall(run_app)
@@ -68,12 +62,12 @@ class ProfileMiddleware(object):
if not content_type.startswith('text/html'):
# We can't add info to non-HTML output
return [body]
- stats = hotshot.stats.load(prof_filename)
+ stats = hotshot.stats.load(self.log_filename)
stats.strip_dirs()
stats.sort_stats('time', 'calls')
- output = capture_output(stats.print_stats, display_limit)
+ output = capture_output(stats.print_stats, self.limit)
output_callers = capture_output(
- stats.print_callers, display_limit)
+ stats.print_callers, self.limit)
body += '<pre style="%s">%s\n%s</pre>' % (
self.style, cgi.escape(output), cgi.escape(output_callers))
return [body]
@@ -116,9 +110,9 @@ def profile_decorator(**options):
add_info:
If given, this info will be added to the report (for your
own tracking). Default: none.
- prof_filename:
+ log_filename:
The temporary filename to log profiling data to. Default;
- ``./tmp_profile_data.log``
+ ``./profile_data.log.tmp``
"""
def decorator(func):
@@ -144,7 +138,7 @@ class DecoratedProfile(object):
def profile(self, func, *args, **kw):
ops = self.options
- prof_filename = ops.get('prof_filename', 'tmp_profile_data.log')
+ prof_filename = ops.get('log_filename', 'profile_data.log.tmp')
prof = hotshot.Profile(prof_filename)
prof.addinfo('Function Call',
self.format_function(func, *args, **kw))
diff --git a/setup.py b/setup.py
index 4162480..7349214 100644
--- a/setup.py
+++ b/setup.py
@@ -61,6 +61,7 @@ functionality.
lint=paste.lint:middleware
login=paste.login:middleware
printdebug=paste.printdebug:PrintDebugMiddleware
+ profile=paste.profilemiddleware:ProfileMiddleware
""",
},
)
diff --git a/tests/test_profilemiddleware.py b/tests/test_profilemiddleware.py
new file mode 100644
index 0000000..6832f35
--- /dev/null
+++ b/tests/test_profilemiddleware.py
@@ -0,0 +1,20 @@
+from paste.fixture import *
+from paste.profilemiddleware import *
+
+def simple_app(environ, start_response):
+ start_response('200 OK', [('content-type', 'text/plain')])
+ return ['all ok']
+
+def long_func():
+ for i in range(1000):
+ pass
+ return 'test'
+
+def test_profile():
+ app = TestApp(ProfileMiddleware(simple_app, {}))
+ app.get('/')
+
+def test_decorator():
+ value = profile_decorator()(long_func)()
+ assert value == 'test'
+