| 1 |
""" |
|---|
| 2 |
Copyright (c) 2004, CherryPy Team (team@cherrypy.org) |
|---|
| 3 |
All rights reserved. |
|---|
| 4 |
|
|---|
| 5 |
Redistribution and use in source and binary forms, with or without modification, |
|---|
| 6 |
are permitted provided that the following conditions are met: |
|---|
| 7 |
|
|---|
| 8 |
* Redistributions of source code must retain the above copyright notice, |
|---|
| 9 |
this list of conditions and the following disclaimer. |
|---|
| 10 |
* Redistributions in binary form must reproduce the above copyright notice, |
|---|
| 11 |
this list of conditions and the following disclaimer in the documentation |
|---|
| 12 |
and/or other materials provided with the distribution. |
|---|
| 13 |
* Neither the name of the CherryPy Team nor the names of its contributors |
|---|
| 14 |
may be used to endorse or promote products derived from this software |
|---|
| 15 |
without specific prior written permission. |
|---|
| 16 |
|
|---|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|---|
| 18 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|---|
| 19 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|---|
| 20 |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
|---|
| 21 |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|---|
| 22 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|---|
| 23 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|---|
| 24 |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
|---|
| 25 |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 26 |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 27 |
""" |
|---|
| 28 |
|
|---|
| 29 |
"""Profiler tools for CherryPy. |
|---|
| 30 |
|
|---|
| 31 |
CherryPy users |
|---|
| 32 |
============== |
|---|
| 33 |
|
|---|
| 34 |
You can profile any of your pages as follows: |
|---|
| 35 |
|
|---|
| 36 |
from cherrypy.lib import profile |
|---|
| 37 |
|
|---|
| 38 |
class Root: |
|---|
| 39 |
p = profile.Profiler("/path/to/profile/dir") |
|---|
| 40 |
|
|---|
| 41 |
def index(self): |
|---|
| 42 |
self.p.run(self._index) |
|---|
| 43 |
index.exposed = True |
|---|
| 44 |
|
|---|
| 45 |
def _index(self): |
|---|
| 46 |
return "Hello, world!" |
|---|
| 47 |
|
|---|
| 48 |
cherrypy.root = Root() |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
CherryPy developers |
|---|
| 52 |
=================== |
|---|
| 53 |
|
|---|
| 54 |
This module can be used whenever you make changes to CherryPy, to get a |
|---|
| 55 |
quick sanity-check on overall CP performance. Set the config entry: |
|---|
| 56 |
"profiling.on = True" to turn on profiling. Then, use the serve() |
|---|
| 57 |
function to browse the results in a web browser. If you run this |
|---|
| 58 |
module from the command line, it will call serve() for you. |
|---|
| 59 |
|
|---|
| 60 |
""" |
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
import hotshot |
|---|
| 64 |
import os, os.path |
|---|
| 65 |
import sys |
|---|
| 66 |
|
|---|
| 67 |
try: |
|---|
| 68 |
import cStringIO as StringIO |
|---|
| 69 |
except ImportError: |
|---|
| 70 |
import StringIO |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
class Profiler(object): |
|---|
| 74 |
|
|---|
| 75 |
def __init__(self, path=None): |
|---|
| 76 |
if not path: |
|---|
| 77 |
path = os.path.join(os.path.dirname(__file__), "profile") |
|---|
| 78 |
self.path = path |
|---|
| 79 |
if not os.path.exists(path): |
|---|
| 80 |
os.makedirs(path) |
|---|
| 81 |
self.count = 0 |
|---|
| 82 |
|
|---|
| 83 |
def run(self, func, *args): |
|---|
| 84 |
"""run(func, *args). Run func, dumping profile data into self.path.""" |
|---|
| 85 |
self.count += 1 |
|---|
| 86 |
path = os.path.join(self.path, "cp_%04d.prof" % self.count) |
|---|
| 87 |
prof = hotshot.Profile(path) |
|---|
| 88 |
prof.runcall(func, *args) |
|---|
| 89 |
prof.close() |
|---|
| 90 |
|
|---|
| 91 |
def statfiles(self): |
|---|
| 92 |
"""statfiles() -> list of available profiles.""" |
|---|
| 93 |
return [f for f in os.listdir(self.path) |
|---|
| 94 |
if f.startswith("cp_") and f.endswith(".prof")] |
|---|
| 95 |
|
|---|
| 96 |
def stats(self, filename, sortby='cumulative'): |
|---|
| 97 |
"""stats(index) -> output of print_stats() for the given profile.""" |
|---|
| 98 |
from hotshot.stats import load |
|---|
| 99 |
s = load(os.path.join(self.path, filename)) |
|---|
| 100 |
s.strip_dirs() |
|---|
| 101 |
s.sort_stats(sortby) |
|---|
| 102 |
oldout = sys.stdout |
|---|
| 103 |
try: |
|---|
| 104 |
sys.stdout = sio = StringIO.StringIO() |
|---|
| 105 |
s.print_stats() |
|---|
| 106 |
finally: |
|---|
| 107 |
sys.stdout = oldout |
|---|
| 108 |
response = sio.getvalue() |
|---|
| 109 |
sio.close() |
|---|
| 110 |
return response |
|---|
| 111 |
|
|---|
| 112 |
def index(self): |
|---|
| 113 |
return """<html> |
|---|
| 114 |
<head><title>CherryPy profile data</title></head> |
|---|
| 115 |
<frameset cols='200, 1*'> |
|---|
| 116 |
<frame src='menu' /> |
|---|
| 117 |
<frame name='main' src='' /> |
|---|
| 118 |
</frameset> |
|---|
| 119 |
</html> |
|---|
| 120 |
""" |
|---|
| 121 |
index.exposed = True |
|---|
| 122 |
|
|---|
| 123 |
def menu(self): |
|---|
| 124 |
yield "<h2>Profiling runs</h2>" |
|---|
| 125 |
yield "<p>Click on one of the runs below to see profiling data.</p>" |
|---|
| 126 |
runs = self.statfiles() |
|---|
| 127 |
runs.sort() |
|---|
| 128 |
for i in runs: |
|---|
| 129 |
yield "<a href='report?filename=%s' target='main'>%s</a><br />" % (i, i) |
|---|
| 130 |
menu.exposed = True |
|---|
| 131 |
|
|---|
| 132 |
def report(self, filename): |
|---|
| 133 |
import cherrypy |
|---|
| 134 |
cherrypy.response.headerMap['Content-Type'] = 'text/plain' |
|---|
| 135 |
return self.stats(filename) |
|---|
| 136 |
report.exposed = True |
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
def serve(path=None, port=8080): |
|---|
| 140 |
import cherrypy |
|---|
| 141 |
cherrypy.root = Profiler(path) |
|---|
| 142 |
cherrypy.config.update({'server.socketPort': port, |
|---|
| 143 |
'server.threadPool': 10, |
|---|
| 144 |
'server.environment': "production", |
|---|
| 145 |
'session.storageType': "ram", |
|---|
| 146 |
}) |
|---|
| 147 |
cherrypy.server.start() |
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
if __name__ == "__main__": |
|---|
| 151 |
serve(*tuple(sys.argv[1:])) |
|---|
| 152 |
|
|---|