Commit ccc22dba authored by 's avatar

Added profiling support.

parent 7cb9b668
......@@ -83,7 +83,7 @@
#
##############################################################################
__doc__="""System management components"""
__version__='$Revision: 1.56 $'[11:-2]
__version__='$Revision: 1.57 $'[11:-2]
import sys,os,time,string,Globals, Acquisition, os
......@@ -96,6 +96,7 @@ from OFS import SimpleItem
from App.Dialogs import MessageDialog
from Product import ProductFolder
from version_txt import version_txt
from cStringIO import StringIO
try: import thread
except: get_ident=lambda: 0
......@@ -395,3 +396,19 @@ class ApplicationManager(Folder,CacheManager):
REQUEST['RESPONSE'].redirect(REQUEST['URL1']+'/manage_main')
# Profiling support
manage_profile=HTMLFile('profile', globals())
def manage_profile_stats(self, sort='time', limit=200):
"""Return profile data if available"""
stats=getattr(sys, '_ps_', None)
if stats is None:
return None
output=StringIO()
stdout=sys.stdout
sys.stdout=output
stats.strip_dirs().sort_stats(sort).print_stats(limit)
sys.stdout.flush()
sys.stdout=stdout
return output.getvalue()
<html>
<head>
<title>Profiler Information</title>
<style type="text/css">
<!--
.header {
font-weight: bold;
font-size: 10pt;
}
.cell {
font-size: 10pt;
}
-->
</style>
</head>
<body bgcolor="#FFFFFF" link="#000099" vlink="#555555">
<!--#var manage_tabs-->
<h3>Profile Information</h3>
<p>
<!--#let sort="REQUEST.get('sort', 'time')"
limit="REQUEST.get('limit', 100)"
stats="manage_profile_stats(sort, limit)"-->
<!--#if stats-->
<form action="<!--#var URL-->" method="POST">
<table>
<tr>
<td><strong>Sort</strong>:
<select name="sort">
<!--#in "('time', 'cumulative', 'calls', 'pcalls',
'name', 'file', 'module', 'line',
'nfl', 'stdname')"-->
<option value="<!--#var sequence-item-->"<!--#if
"sort==_['sequence-item']"--> selected<!--#/if-->><!--#var
sequence-item-->
<!--#/in-->
</select>
</td>
<td><strong>Limit</strong>:
<select name="limit:int">
<!--#in "(100, 200, 300, 400, 500)"-->
<option value="<!--#var sequence-item-->"<!--#if
"limit==_['sequence-item']"--> selected<!--#/if-->><!--#var
sequence-item-->
<!--#/in-->
</select>
</td>
<td>
<input type="submit" name="submit" value="Update">
</td>
</tr>
</table>
</form>
<hr>
<pre>
<!--#var stats-->
</pre>
<!--#else-->
<em>
Profiling is not currently enabled or there is not yet any profiling
data to report. To enable profiling, restart the Zope process with
the environment variable PROFILE_PUBLISHER defined. The value of this
variable should be the full system path to a file that will be used
to dump a profile report when the process restarts or exits.
</em>
<!--#/if-->
<!--#/let-->
</body>
</html>
......@@ -84,8 +84,8 @@
##############################################################################
__doc__="""Python Object Publisher -- Publish Python objects on web servers
$Id: Publish.py,v 1.141 1999/09/01 14:51:33 jim Exp $"""
__version__='$Revision: 1.141 $'[11:-2]
$Id: Publish.py,v 1.142 1999/09/08 14:52:07 brian Exp $"""
__version__='$Revision: 1.142 $'[11:-2]
import sys, os
from string import lower, atoi, rfind, strip
......@@ -317,3 +317,65 @@ def get_module_info(module_name, modules={},
finally:
tb=None
release()
# ZPublisher profiler support
# ---------------------------
if os.environ.get('PROFILE_PUBLISHER', None):
import profile, pstats
_pfile=os.environ['PROFILE_PUBLISHER']
_plock=allocate_lock()
_pfunc=publish_module
_pstat=None
def pm(module_name, stdin, stdout, stderr,
environ, debug, request, response):
try:
r=_pfunc(module_name, stdin=stdin, stdout=stdout,
stderr=stderr, environ=environ, debug=debug,
request=request, response=response)
except: r=None
sys._pr_=r
def publish_module(module_name, stdin=sys.stdin, stdout=sys.stdout,
stderr=sys.stderr, environ=os.environ, debug=0,
request=None, response=None):
_plock.acquire()
try:
if request is not None:
path_info=request.get('PATH_INFO')
else: path_info=environ.get('PATH_INFO')
if path_info[-14:]=='manage_profile':
return _pfunc(module_name, stdin=stdin, stdout=stdout,
stderr=stderr, environ=environ, debug=debug,
request=request, response=response)
pobj=profile.Profile()
pobj.runcall(pm, module_name, stdin, stdout, stderr,
environ, debug, request, response)
result=sys._pr_
pobj.create_stats()
if _pstat is None:
global _pstat
_pstat=sys._ps_=pstats.Stats(pobj)
else: _pstat.add(pobj)
finally:
_plock.release()
if result is None:
try:
error=sys.exc_info()
file=open(_pfile, 'w')
sys.stdout=file
_pstat.strip_dirs().sort_stats('cumulative').print_stats(250)
_pstat.strip_dirs().sort_stats('time').print_stats(250)
file.flush()
file.close()
except: pass
raise error[0], error[1], error[2]
return result
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment