def runProfiler(func, args=tuple(), kw={}, verbose=True, nb_func=25, sort_by=('cumulative', 'calls')): profile_filename = "/tmp/profiler" prof = Profile(profile_filename) try: if verbose: print "[+] Run profiler" result = prof.runcall(func, *args, **kw) prof.close() if verbose: print "[+] Stop profiler" print "[+] Process data..." stat = loadStats(profile_filename) if verbose: print "[+] Strip..." stat.strip_dirs() if verbose: print "[+] Sort data..." stat.sort_stats(*sort_by) if verbose: print print "[+] Display statistics" print stat.print_stats(nb_func) return result finally: unlink(profile_filename)
def runTests(options): if options is None: options = defaultOptions() global REASONING_STRATEGY, GROUND_QUERY, SINGLE_TEST, DEBUG SINGLE_TEST = options.singleTest DEBUG = True # options.debug GROUND_QUERY = options.groundQuery REASONING_STRATEGY = options.strategy suite = unittest.makeSuite(OwlTestSuite) print("NTests: {}".format(suite.countTestCases())) if options.profile: # from profile import Profile from hotshot import Profile, stats p = Profile('fuxi.profile') # p = Profile() for i in range(options.runs): p.runcall(unittest.TextTestRunner(verbosity=5).run, suite) p.close() s = stats.load('fuxi.profile') # s = p.create_stats() s.strip_dirs() s.sort_stats('time', 'cumulative', 'pcalls') s.print_stats(.1) s.print_callers(.05) s.print_callees(.05) else: for i in range(options.runs): unittest.TextTestRunner(verbosity=5).run(suite)
def runProfiler(logger, func, args=tuple(), kw={}, verbose=True, nb_func=25, sort_by=('time', )): """ Run a function in a profiler and then display the functions sorted by time. """ profile_filename = "/tmp/profiler" prof = Profile(profile_filename) try: logger.warning("Run profiler") result = prof.runcall(func, *args, **kw) prof.close() logger.error("Profiler: Process data...") stat = loadStats(profile_filename) stat.strip_dirs() stat.sort_stats(*sort_by) logger.error("Profiler: Result:") log = StringIO() stat.stream = log stat.print_stats(nb_func) log.seek(0) for line in log: logger.error(line.rstrip()) return result finally: unlink(profile_filename)
def runTests(options): global GROUND_QUERY, SINGLE_TEST, DEBUG SINGLE_TEST = options.singleTest DEBUG = options.debug GROUND_QUERY = options.groundQuery suite = unittest.makeSuite(OwlTestSuite) if options.profile: #from profile import Profile from hotshot import Profile, stats p = Profile('fuxi.profile') #p = Profile() for i in range(options.runs): p.runcall(unittest.TextTestRunner(verbosity=5).run, suite) p.close() s = stats.load('fuxi.profile') # s=p.create_stats() s.strip_dirs() s.sort_stats('time', 'cumulative', 'pcalls') s.print_stats(.1) s.print_callers(.05) s.print_callees(.05) else: for i in range(options.runs): unittest.TextTestRunner(verbosity=5).run(suite)
def profileTests(): from hotshot import Profile, stats p = Profile('rdflib-mysql.profile') p.runcall(testRun) p.close() s = stats.load('rdflib-mysql.profile') s.strip_dirs() s.sort_stats('time', 'cumulative', 'pcalls') #s.sort_stats('time','pcalls') s.print_stats(.1) s.print_callers(.1) s.print_callees(.1)
def profile(func, name='main', runKCacheGrind=False): prof = Profile('/tmp/' + name + '.prof', lineevents=1, linetimings=1) try: prof.runcall(func) finally: prof.close() # For running KCacheGrind you need to install (aptitude): # - kcachegrind # - kcachegrind-convertors if runKCacheGrind: path = '/'.join(__file__.split('/')[:-1]) system( 'hotshot2calltree -o /tmp/%(name)s.out /tmp/%(name)s.prof; kcachegrind /tmp/%(name)s.out' % locals())
def __call__(self, environ, start_response): """Invoke the Controller""" # WSGIController.__call__ dispatches to the Controller method # the request is routed to. This routing information is # available in environ['pylons.routes_dict'] try: if g.OPT.requestProfiling: pf = Profile(g.OPT.profileDumpFile) response = pf.runcall(WSGIController.__call__, self, environ, start_response) pf.close() return response else: return WSGIController.__call__(self, environ, start_response) finally: meta.Session.remove()
if options.closure and options.output in RDF_SERIALIZATION_FORMATS: cGraph = network.closureGraph(factGraph) cGraph.namespace_manager = namespace_manager print( cGraph.serialize(destination=None, format=options.output, base=None)) elif options.output and options.output in RDF_SERIALIZATION_FORMATS: print( network.inferredFacts.serialize(destination=None, format=options.output, base=None)) if __name__ == '__main__': from hotshot import Profile, stats # import pycallgraph # pycallgraph.start_trace() # main() # pycallgraph.make_dot_graph('FuXi-timing.png') # sys.exit(1) p = Profile('fuxi.profile') p.runcall(main) p.close() s = stats.load('fuxi.profile') s.strip_dirs() s.sort_stats('time', 'cumulative', 'pcalls') s.print_stats(.05) s.print_callers(.01) s.print_callees(.01)
>>> s.print_stats(20) and a report will be displayed. For more information on generating profiler reports, see http://python.org/doc/current/lib/profile-stats.html or the 'pstats' module chapter of your Python manual. """ import sys from run_tests import ScanningLoader from unittest import main from hotshot import Profile from hotshot.stats import load if __name__ == '__main__': if len(sys.argv) < 2 or sys.argv[1] in ('-h', '--help'): # XXX print __doc__ sys.exit(2) stats_file = "profile.dat" try: Profile(stats_file).run( "main(module=None, testLoader=ScanningLoader())") except SystemExit: # prevent unittest.main() from forcing an early exit pass s = load(stats_file) s.sort_stats("time") s.print_stats(10)
def __init__(self, args, reporter=None): self._rcfile = None self._plugins = [] preprocess_options( args, { # option: (callback, takearg) 'rcfile': (self.cb_set_rcfile, True), 'load-plugins': (self.cb_add_plugins, True), }) self.linter = linter = self.LinterClass( ( ('rcfile', { 'action': 'callback', 'callback': lambda *args: 1, 'type': 'string', 'metavar': '<file>', 'help': 'Specify a configuration file.' }), ('init-hook', { 'action': 'callback', 'type': 'string', 'metavar': '<code>', 'callback': cb_init_hook, 'help': 'Python code to execute, usually for sys.path \ manipulation such as pygtk.require().' }), ('help-msg', { 'action': 'callback', 'type': 'string', 'metavar': '<msg-id>', 'callback': self.cb_help_message, 'group': 'Commands', 'help': '''Display a help message for the given message id and \ exit. The value may be a comma separated list of message ids.''' }), ('list-msgs', { 'action': 'callback', 'metavar': '<msg-id>', 'callback': self.cb_list_messages, 'group': 'Commands', 'help': "Generate pylint's messages." }), ('full-documentation', { 'action': 'callback', 'metavar': '<msg-id>', 'callback': self.cb_full_documentation, 'group': 'Commands', 'help': "Generate pylint's full documentation." }), ('generate-rcfile', { 'action': 'callback', 'callback': self.cb_generate_config, 'group': 'Commands', 'help': '''Generate a sample configuration file according to \ the current configuration. You can put other options before this one to get \ them in the generated configuration.''' }), ('generate-man', { 'action': 'callback', 'callback': self.cb_generate_manpage, 'group': 'Commands', 'help': "Generate pylint's man page.", 'hide': 'True' }), ('errors-only', { 'action': 'callback', 'callback': self.cb_error_mode, 'short': 'e', 'help': '''In error mode, checkers without error messages are \ disabled and for others, only the ERROR messages are displayed, and no reports \ are done by default''' }), ('profile', { 'type': 'yn', 'metavar': '<y_or_n>', 'default': False, 'help': 'Profiled execution.' }), ), option_groups=self.option_groups, reporter=reporter, pylintrc=self._rcfile) # register standard checkers from pylint import checkers checkers.initialize(linter) # load command line plugins linter.load_plugin_modules(self._plugins) # add some help section linter.add_help_section('Environment variables', config.ENV_HELP) linter.add_help_section( 'Output', ''' Using the default text output, the message format is : MESSAGE_TYPE: LINE_NUM:[OBJECT:] MESSAGE There are 5 kind of message types : * (C) convention, for programming standard violation * (R) refactor, for bad code smell * (W) warning, for python specific problems * (E) error, for probable bugs in the code * (F) fatal, if an error occurred which prevented pylint from doing further processing. ''') linter.add_help_section( 'Output status code', ''' Pylint should leave with following status code: * 0 if everything went fine * 1 if a fatal message was issued * 2 if an error message was issued * 4 if a warning message was issued * 8 if a refactor message was issued * 16 if a convention message was issued * 32 on usage error status 1 to 16 will be bit-ORed so you can know which different categories has been issued by analysing pylint output status code ''') # read configuration linter.disable_message('W0704') linter.read_config_file() # is there some additional plugins in the file configuration, in config_parser = linter._config_parser if config_parser.has_option('MASTER', 'load-plugins'): plugins = splitstrip(config_parser.get('MASTER', 'load-plugins')) linter.load_plugin_modules(plugins) # now we can load file config and command line, plugins (which can # provide options) have been registered linter.load_config_file() if reporter: # if a custom reporter is provided as argument, it may be overridden # by file parameters, so re-set it here, but before command line # parsing so it's still overrideable by command line option linter.set_reporter(reporter) args = linter.load_command_line_configuration(args) if not args: print linter.help() sys.exit(32) # insert current working directory to the python path to have a correct # behaviour sys.path.insert(0, os.getcwd()) if self.linter.config.profile: print >> sys.stderr, '** profiled run' from hotshot import Profile, stats prof = Profile('stones.prof') prof.runcall(linter.check, args) prof.close() data = stats.load('stones.prof') data.strip_dirs() data.sort_stats('time', 'calls') data.print_stats(30) else: linter.check(args) sys.path.pop(0) sys.exit(self.linter.msg_status)
def runTests(self): Profile(stats_file).runcall(main.runTests, self)
def main(cmdline): import getopt from netconfpkg import NC_functions NC_functions.setVerboseLevel(2) NC_functions.setDebugLevel(0) hotshot = 0 splash = 0 chroot = None try: opts = getopt.getopt( cmdline, "vh?r:d", ["verbose", "debug", "help", "hotshot", "splash", "root="])[0] for opt, val in opts: if opt == '-v' or opt == '--verbose': NC_functions.setVerboseLevel(NC_functions.getVerboseLevel() + 1) continue if opt == '-d' or opt == '--debug': NC_functions.setDebugLevel(NC_functions.getDebugLevel() + 1) continue if opt == '--hotshot': hotshot += 1 continue if opt == '--splash': splash += 1 continue if opt == '-h' or opt == "?" or opt == '--help': Usage() return 0 if opt == '-r' or opt == '--root': chroot = val continue raise BadUsage except (getopt.error, BadUsage): Usage() return 1 if not NC_functions.getDebugLevel(): log.handler = log.syslog_handler log.open() else: log.handler = log.file_handler log.open(sys.stderr) if chroot: NC_functions.setRoot(chroot) if not os.access(NC_functions.getRoot(), os.W_OK): if os.getuid() != 0: from netconfpkg.gui import GUI_functions NC_functions.generic_error_dialog( _("Please start system-config-network " "with root permissions!\n")) return 10 if chroot: NC_functions.prepareRoot(chroot) if hotshot: import tempfile from hotshot import Profile import hotshot.stats (fd, filename) = tempfile.mkstemp() prof = Profile(filename) prof = prof.runcall(runit) s = hotshot.stats.load(filename) s.strip_dirs().sort_stats('time').print_stats(20) s.strip_dirs().sort_stats('cumulative').print_stats(20) os.close(fd) os.unlink(filename) else: runit(splash) return 0
def begin_profiling(path=''): global profiler, profile_name if profiler is None: profile_name = find_profile(path=path) profiler = Profile(profile_name)
def __init__(self, args, reporter=None): self._rcfile = None self._plugins = [] preprocess_options( args, { # option: (callback, takearg) 'rcfile': (self.cb_set_rcfile, True), 'load-plugins': (self.cb_add_plugins, True), }) self.linter = linter = self.LinterClass(( ('rcfile', { 'action': 'callback', 'callback': lambda *args: 1, 'type': 'string', 'metavar': '<file>', 'help': 'Specify a configuration file.' }), ('init-hook', { 'action': 'callback', 'type': 'string', 'metavar': '<code>', 'callback': cb_init_hook, 'help': 'Python code to execute, usually for sys.path \ manipulation such as pygtk.require().' }), ('help-msg', { 'action': 'callback', 'type': 'string', 'metavar': '<msg-id>', 'callback': self.cb_help_message, 'group': 'Commands', 'help': '''Display a help message for the given message id and \ exit. The value may be a comma separated list of message ids.''' }), ('list-msgs', { 'action': 'callback', 'metavar': '<msg-id>', 'callback': self.cb_list_messages, 'group': 'Commands', 'help': "Generate pylint's full documentation." }), ('generate-rcfile', { 'action': 'callback', 'callback': self.cb_generate_config, 'group': 'Commands', 'help': '''Generate a sample configuration file according to \ the current configuration. You can put other options before this one to get \ them in the generated configuration.''' }), ('generate-man', { 'action': 'callback', 'callback': self.cb_generate_manpage, 'group': 'Commands', 'help': "Generate pylint's man page.", 'hide': 'True' }), ('errors-only', { 'action': 'callback', 'callback': self.cb_error_mode, 'short': 'e', 'help': '''In error mode, checkers without error messages are \ disabled and for others, only the ERROR messages are displayed, and no reports \ are done by default''' }), ('profile', { 'type': 'yn', 'metavar': '<y_or_n>', 'default': False, 'help': 'Profiled execution.' }), ), option_groups=self. option_groups, reporter=reporter, pylintrc=self._rcfile) # register standard checkers checkers.initialize(linter) # load command line plugins linter.load_plugin_modules(self._plugins) # read configuration linter.disable_message('W0704') linter.read_config_file() # is there some additional plugins in the file configuration, in config_parser = linter._config_parser if config_parser.has_option('MASTER', 'load-plugins'): plugins = splitstrip( config_parser.get('MASTER', 'load-plugins')) linter.load_plugin_modules(plugins) # now we can load file config and command line, plugins (which can # provide options) have been registered linter.load_config_file() if reporter: # if a custom reporter is provided as argument, it may be overriden # by file parameters, so re-set it here, but before command line # parsing so it's still overrideable by command line option linter.set_reporter(reporter) args = linter.load_command_line_configuration(args) # insert current working directory to the python path to have a correct # behaviour sys.path.insert(0, os.getcwd()) if self.linter.config.profile: print('** profiled run', file=sys.stderr) from hotshot import Profile, stats prof = Profile('stones.prof') prof.runcall(linter.check, args) prof.close() data = stats.load('stones.prof') data.strip_dirs() data.sort_stats('time', 'calls') data.print_stats(30) sys.path.pop(0)
if operations == None: sys.exit(-1) if output_op: for op, pkg in operations: print op, pkg.source sys.exit(0) if __name__ == '__main__': hotshot = 0 if hotshot: import tempfile from hotshot import Profile import hotshot.stats filename = tempfile.mktemp() prof = Profile(filename) try: prof = prof.runcall(main) except SystemExit: pass prof.close() del prof s = hotshot.stats.load(filename) s.strip_dirs().sort_stats('time').print_stats(20) s.strip_dirs().sort_stats('cumulative').print_stats(20) os.unlink(filename) else: main()