| 1 |
"""Profiler tools for CherryPy. |
|---|
| 2 |
|
|---|
| 3 |
CherryPy users |
|---|
| 4 |
============== |
|---|
| 5 |
|
|---|
| 6 |
You can profile any of your pages as follows: |
|---|
| 7 |
|
|---|
| 8 |
from cherrypy.lib import profile |
|---|
| 9 |
|
|---|
| 10 |
class Root: |
|---|
| 11 |
p = profile.Profiler("/path/to/profile/dir") |
|---|
| 12 |
|
|---|
| 13 |
def index(self): |
|---|
| 14 |
self.p.run(self._index) |
|---|
| 15 |
index.exposed = True |
|---|
| 16 |
|
|---|
| 17 |
def _index(self): |
|---|
| 18 |
return "Hello, world!" |
|---|
| 19 |
|
|---|
| 20 |
cherrypy.root = Root() |
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
CherryPy developers |
|---|
| 24 |
=================== |
|---|
| 25 |
|
|---|
| 26 |
This module can be used whenever you make changes to CherryPy, to get a |
|---|
| 27 |
quick sanity-check on overall CP performance. Set the config entry: |
|---|
| 28 |
"profiling.on = True" to turn on profiling. Then, use the serve() |
|---|
| 29 |
function to browse the results in a web browser. If you run this |
|---|
| 30 |
module from the command line, it will call serve() for you. |
|---|
| 31 |
|
|---|
| 32 |
""" |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
def new_func_strip_path(func_name): |
|---|
| 37 |
filename, line, name = func_name |
|---|
| 38 |
if filename.endswith("__init__.py"): |
|---|
| 39 |
return os.path.basename(filename[:-12]) + filename[-12:], line, name |
|---|
| 40 |
return os.path.basename(filename), line, name |
|---|
| 41 |
|
|---|
| 42 |
try: |
|---|
| 43 |
import profile |
|---|
| 44 |
import pstats |
|---|
| 45 |
pstats.func_strip_path = new_func_strip_path |
|---|
| 46 |
except ImportError: |
|---|
| 47 |
profile = None |
|---|
| 48 |
pstats = None |
|---|
| 49 |
import warnings |
|---|
| 50 |
msg = ("Your installation of Python doesn't have a profile module. " |
|---|
| 51 |
"If you're on Debian, you can apt-get python2.4-profiler from " |
|---|
| 52 |
"non-free in a separate step. See http://www.cherrypy.org/wiki/" |
|---|
| 53 |
"ProfilingOnDebian for details.") |
|---|
| 54 |
warnings.warn(msg) |
|---|
| 55 |
|
|---|
| 56 |
import os, os.path |
|---|
| 57 |
import sys |
|---|
| 58 |
|
|---|
| 59 |
try: |
|---|
| 60 |
import cStringIO as StringIO |
|---|
| 61 |
except ImportError: |
|---|
| 62 |
import StringIO |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
class Profiler(object): |
|---|
| 66 |
|
|---|
| 67 |
def __init__(self, path=None): |
|---|
| 68 |
if not path: |
|---|
| 69 |
path = os.path.join(os.path.dirname(__file__), "profile") |
|---|
| 70 |
self.path = path |
|---|
| 71 |
if not os.path.exists(path): |
|---|
| 72 |
os.makedirs(path) |
|---|
| 73 |
self.count = 0 |
|---|
| 74 |
|
|---|
| 75 |
def run(self, func, *args): |
|---|
| 76 |
"""run(func, *args). Run func, dumping profile data into self.path.""" |
|---|
| 77 |
self.count += 1 |
|---|
| 78 |
path = os.path.join(self.path, "cp_%04d.prof" % self.count) |
|---|
| 79 |
prof = profile.Profile() |
|---|
| 80 |
prof.runcall(func, *args) |
|---|
| 81 |
prof.dump_stats(path) |
|---|
| 82 |
|
|---|
| 83 |
def statfiles(self): |
|---|
| 84 |
"""statfiles() -> list of available profiles.""" |
|---|
| 85 |
return [f for f in os.listdir(self.path) |
|---|
| 86 |
if f.startswith("cp_") and f.endswith(".prof")] |
|---|
| 87 |
|
|---|
| 88 |
def stats(self, filename, sortby='cumulative'): |
|---|
| 89 |
"""stats(index) -> output of print_stats() for the given profile.""" |
|---|
| 90 |
s = pstats.Stats(os.path.join(self.path, filename)) |
|---|
| 91 |
s.strip_dirs() |
|---|
| 92 |
s.sort_stats(sortby) |
|---|
| 93 |
oldout = sys.stdout |
|---|
| 94 |
try: |
|---|
| 95 |
sys.stdout = sio = StringIO.StringIO() |
|---|
| 96 |
s.print_stats() |
|---|
| 97 |
finally: |
|---|
| 98 |
sys.stdout = oldout |
|---|
| 99 |
response = sio.getvalue() |
|---|
| 100 |
sio.close() |
|---|
| 101 |
return response |
|---|
| 102 |
|
|---|
| 103 |
def index(self): |
|---|
| 104 |
return """<html> |
|---|
| 105 |
<head><title>CherryPy profile data</title></head> |
|---|
| 106 |
<frameset cols='200, 1*'> |
|---|
| 107 |
<frame src='menu' /> |
|---|
| 108 |
<frame name='main' src='' /> |
|---|
| 109 |
</frameset> |
|---|
| 110 |
</html> |
|---|
| 111 |
""" |
|---|
| 112 |
index.exposed = True |
|---|
| 113 |
|
|---|
| 114 |
def menu(self): |
|---|
| 115 |
yield "<h2>Profiling runs</h2>" |
|---|
| 116 |
yield "<p>Click on one of the runs below to see profiling data.</p>" |
|---|
| 117 |
runs = self.statfiles() |
|---|
| 118 |
runs.sort() |
|---|
| 119 |
for i in runs: |
|---|
| 120 |
yield "<a href='report?filename=%s' target='main'>%s</a><br />" % (i, i) |
|---|
| 121 |
menu.exposed = True |
|---|
| 122 |
|
|---|
| 123 |
def report(self, filename): |
|---|
| 124 |
import cherrypy |
|---|
| 125 |
cherrypy.response.headers['Content-Type'] = 'text/plain' |
|---|
| 126 |
return self.stats(filename) |
|---|
| 127 |
report.exposed = True |
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
def serve(path=None, port=8080): |
|---|
| 131 |
import cherrypy |
|---|
| 132 |
cherrypy.root = Profiler(path) |
|---|
| 133 |
cherrypy.config.update({'server.socket_port': int(port), |
|---|
| 134 |
'server.thread_pool': 10, |
|---|
| 135 |
'server.environment': "production", |
|---|
| 136 |
'session.storageType': "ram", |
|---|
| 137 |
}) |
|---|
| 138 |
cherrypy.server.start() |
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
if __name__ == "__main__": |
|---|
| 142 |
serve(*tuple(sys.argv[1:])) |
|---|
| 143 |
|
|---|