#!/usr/bin/env python # Copyright (c) 2007 RADLogic # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. """Generate human-readable stats output from a profile.run report file. This is useful for displaying reports generated with profile_it """ __author__ = 'Tim Wegener ' __date__ = '$Date: 2007/03/27 03:28:04 $' __version__ = '$Revision: 0.5 $' import sys import pstats import optparse def report_stats(profile_filename, sort_key=None, hotshot=False): """Generate pretty stats to stdout from profile.run report file. Arguments: profile_filename -- filename of report file generated by profile.run sort_key -- one of the following keys to sort on: (see python module docs for more info) 'calls' -- call count 'cumulative' -- cumulative time 'file' -- file name 'module' -- file name 'pcalls' -- primitive call count 'line' -- line number 'name' -- function name 'nfl' -- name/file/line 'stdname' -- standard name 'time' -- internal time hotshot -- Set this to read a hotshot report. """ if not hotshot: ps = pstats.Stats(profile_filename) else: import hotshot.stats ps = hotshot.stats.load(profile_filename) if sort_key: ps.sort_stats(sort_key) ps.print_stats() def main(): """Command-line front-end""" # Command line options usage = 'usage: %prog [options] profile_report_file' version = ('%prog ' + __version__.split()[1] + '\n' 'Copyright (C) 2004 RADLogic Pty. Ltd.\n' 'All rights reserved.') parser = optparse.OptionParser(usage=usage, version=version) parser.add_option('-k', '--key', default=None, help='sort stats by this key: one of' " 'calls' 'cumulative' 'files' 'modules' 'pcalls'" " 'line' 'name' 'nfl' 'stdname' 'time'") parser.add_option('--hotshot', action='store_true', help='read a hotshot log file') (options, args) = parser.parse_args() try: profile_report = args[0] except IndexError: sys.stderr.write('Error: profile_stats:' ' Must specify report filename.' ' Use --help for usage.\n') sys.exit(2) try: report_stats(profile_report, sort_key=options.key, hotshot=options.hotshot) except IOError, msg: sys.stderr.write('Error: profile_stats: %s\n' % msg) sys.exit(2) except KeyError, msg: sys.stderr.write('Error: profile_stats: Bad sort key: %s' % (msg) + ' Use --help for usage.\n') sys.exit(2) if __name__ == "__main__": main()