diff options
Diffstat (limited to 'cherrypy/lib/profiler.py')
| -rw-r--r-- | cherrypy/lib/profiler.py | 77 |
1 files changed, 41 insertions, 36 deletions
diff --git a/cherrypy/lib/profiler.py b/cherrypy/lib/profiler.py index a3477454..fccf2eb8 100644 --- a/cherrypy/lib/profiler.py +++ b/cherrypy/lib/profiler.py @@ -10,9 +10,9 @@ You can profile any of your pages as follows:: class Root: p = profiler.Profiler("/path/to/profile/dir") + @cherrypy.expose def index(self): self.p.run(self._index) - index.exposed = True def _index(self): return "Hello, world!" @@ -33,29 +33,36 @@ module from the command line, it will call ``serve()`` for you. """ +import io +import os +import os.path +import sys +import warnings + +import cherrypy -def new_func_strip_path(func_name): - """Make profiler output more readable by adding `__init__` modules' parents - """ - filename, line, name = func_name - if filename.endswith("__init__.py"): - return os.path.basename(filename[:-12]) + filename[-12:], line, name - return os.path.basename(filename), line, name try: import profile import pstats + + def new_func_strip_path(func_name): + """Make profiler output more readable by adding `__init__` modules' parents + """ + filename, line, name = func_name + if filename.endswith('__init__.py'): + return ( + os.path.basename(filename[:-12]) + filename[-12:], + line, + name, + ) + return os.path.basename(filename), line, name + pstats.func_strip_path = new_func_strip_path except ImportError: profile = None pstats = None -import os -import os.path -import sys -import warnings - -from cherrypy._cpcompat import StringIO _count = 0 @@ -64,7 +71,7 @@ class Profiler(object): def __init__(self, path=None): if not path: - path = os.path.join(os.path.dirname(__file__), "profile") + path = os.path.join(os.path.dirname(__file__), 'profile') self.path = path if not os.path.exists(path): os.makedirs(path) @@ -73,7 +80,7 @@ class Profiler(object): """Dump profile data into self.path.""" global _count c = _count = _count + 1 - path = os.path.join(self.path, "cp_%04d.prof" % c) + path = os.path.join(self.path, 'cp_%04d.prof' % c) prof = profile.Profile() result = prof.runcall(func, *args, **params) prof.dump_stats(path) @@ -83,12 +90,12 @@ class Profiler(object): """:rtype: list of available profiles. """ return [f for f in os.listdir(self.path) - if f.startswith("cp_") and f.endswith(".prof")] + if f.startswith('cp_') and f.endswith('.prof')] def stats(self, filename, sortby='cumulative'): """:rtype stats(index): output of print_stats() for the given profile. """ - sio = StringIO() + sio = io.StringIO() if sys.version_info >= (2, 5): s = pstats.Stats(os.path.join(self.path, filename), stream=sio) s.strip_dirs() @@ -110,6 +117,7 @@ class Profiler(object): sio.close() return response + @cherrypy.expose def index(self): return """<html> <head><title>CherryPy profile data</title></head> @@ -119,23 +127,21 @@ class Profiler(object): </frameset> </html> """ - index.exposed = True + @cherrypy.expose def menu(self): - yield "<h2>Profiling runs</h2>" - yield "<p>Click on one of the runs below to see profiling data.</p>" + yield '<h2>Profiling runs</h2>' + yield '<p>Click on one of the runs below to see profiling data.</p>' runs = self.statfiles() runs.sort() for i in runs: yield "<a href='report?filename=%s' target='main'>%s</a><br />" % ( i, i) - menu.exposed = True + @cherrypy.expose def report(self, filename): - import cherrypy cherrypy.response.headers['Content-Type'] = 'text/plain' return self.stats(filename) - report.exposed = True class ProfileAggregator(Profiler): @@ -147,7 +153,7 @@ class ProfileAggregator(Profiler): self.profiler = profile.Profile() def run(self, func, *args, **params): - path = os.path.join(self.path, "cp_%04d.prof" % self.count) + path = os.path.join(self.path, 'cp_%04d.prof' % self.count) result = self.profiler.runcall(func, *args, **params) self.profiler.dump_stats(path) return result @@ -172,11 +178,11 @@ class make_app: """ if profile is None or pstats is None: - msg = ("Your installation of Python does not have a profile " + msg = ('Your installation of Python does not have a profile ' "module. If you're on Debian, try " - "`sudo apt-get install python-profiler`. " - "See http://www.cherrypy.org/wiki/ProfilingOnDebian " - "for details.") + '`sudo apt-get install python-profiler`. ' + 'See http://www.cherrypy.org/wiki/ProfilingOnDebian ' + 'for details.') warnings.warn(msg) self.nextapp = nextapp @@ -197,20 +203,19 @@ class make_app: def serve(path=None, port=8080): if profile is None or pstats is None: - msg = ("Your installation of Python does not have a profile module. " + msg = ('Your installation of Python does not have a profile module. ' "If you're on Debian, try " - "`sudo apt-get install python-profiler`. " - "See http://www.cherrypy.org/wiki/ProfilingOnDebian " - "for details.") + '`sudo apt-get install python-profiler`. ' + 'See http://www.cherrypy.org/wiki/ProfilingOnDebian ' + 'for details.') warnings.warn(msg) - import cherrypy cherrypy.config.update({'server.socket_port': int(port), 'server.thread_pool': 10, - 'environment': "production", + 'environment': 'production', }) cherrypy.quickstart(Profiler(path)) -if __name__ == "__main__": +if __name__ == '__main__': serve(*tuple(sys.argv[1:])) |
