コード例 #1
0
ファイル: profiler.py プロジェクト: sulik011/forum
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)
コード例 #2
0
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)
コード例 #3
0
ファイル: profiler.py プロジェクト: JackDandy/SickGear
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)
コード例 #4
0
ファイル: profiler.py プロジェクト: cherry-wb/segvault
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)
コード例 #5
0
ファイル: testOWL.py プロジェクト: baojie/FuXi-1
def runTests(options):
    if options is None:
        options = defaultOptions()
    global REASONING_STRATEGY, GROUND_QUERY, SINGLE_TEST, DEBUG
    SINGLE_TEST = options.singleTest
    DEBUG = options.debug
    GROUND_QUERY = options.groundQuery
    REASONING_STRATEGY = options.strategy

    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)
コード例 #6
0
ファイル: widget.py プロジェクト: facuman/haikuterm
def profile():
    from hotshot import Profile
    prof = Profile('widget.prof')
    prof.runcall(main)
    prof.close()
    import hotshot.stats
    stats = hotshot.stats.load('widget.prof')
    stats.strip_dirs()
    stats.sort_stats('time', 'calls')
    stats.print_stats(30)
コード例 #7
0
ファイル: mysql_profile.py プロジェクト: RDFLib/rdflib-mysql
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)
コード例 #8
0
ファイル: base.py プロジェクト: samyisok/orphereus
 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()
コード例 #9
0
ファイル: testOWL.py プロジェクト: KiranAjayakumar/python-dlp
def runTests(profile=False):
    suite = unittest.makeSuite(OwlTestSuite)
    if profile:
        #from profile import Profile
        from hotshot import Profile, stats
        p = Profile('fuxi.profile')
        #p = Profile()
        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:
        unittest.TextTestRunner(verbosity=5).run(suite)
コード例 #10
0
ファイル: OWLsuite.py プロジェクト: NanduBC/FuXi
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)
コード例 #11
0
ファイル: testOWL.py プロジェクト: slitayem/fuxi
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)
コード例 #12
0
ファイル: mysql.py プロジェクト: ChunHungLiu/watchdog-1
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)
コード例 #13
0
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())
コード例 #14
0
    >>> 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)
コード例 #15
0
ファイル: rpmresolve.py プロジェクト: kholia/pyrpm
    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()
    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)
コード例 #17
0
ファイル: lint.py プロジェクト: zalun/FlightDeck-lib
    def __init__(self, args, reporter=None, exit=True):
        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,
                        "level": 1,
                        "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",
                        "level": 1,
                        "help": "Generate pylint's messages.",
                    },
                ),
                (
                    "full-documentation",
                    {
                        "action": "callback",
                        "metavar": "<msg-id>",
                        "callback": self.cb_full_documentation,
                        "group": "Commands",
                        "level": 1,
                        "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,
                        "hide": True,
                        "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, level=1)
        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.
        """,
            level=1,
        )
        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
        """,
            level=1,
        )
        # read configuration
        linter.disable("W0704")
        linter.read_config_file()
        # is there some additional plugins in the file configuration, in
        config_parser = linter.cfgfile_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)
        if exit:
            sys.exit(self.linter.msg_status)
コード例 #18
0
 def process_request(self, request):
     if (settings.DEBUG or request.user.is_superuser) \
             and request.method == 'GET' and request.GET.get('prof', False):
         self.tmpfile = mktemp()
         self.prof = Profile(self.tmpfile)
コード例 #19
0
ファイル: setup_test.py プロジェクト: lat/WMCore
        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 >> 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)
            sys.path.pop(0)
コード例 #20
0
ファイル: CommandLine.py プロジェクト: baojie/FuXi-1
        network.inferredFacts = network.filteredFacts

    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)
コード例 #21
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()
コード例 #22
0
ファイル: test.py プロジェクト: Bazmundi/fuxi
             print "\tSkipping (%s)"%test,SKIP[test]#>>sys.stderr,SKIP[test],
     elif set(regime).intersection(SUPPORTED_ENTAILMENT):
         test_name = 'test_%s' % test
         test = test_generator(
                     test,
                     name,
                     queryFile,
                     rdfDoc,
                     regime,
                     result,
                     named_graphs,
                     options.debug)
         setattr(TestSequence, test_name, test)
 if options.profile:
     from hotshot import Profile, stats
     p = Profile('fuxi.profile')
     p.runcall(unittest.TextTestRunner(verbosity=5).run,
               unittest.makeSuite(TestSequence))
     p.close()
     s = stats.load('fuxi.profile')
     s.strip_dirs()
     s.sort_stats('time','cumulative','pcalls')
     s.print_stats(.1)
     s.print_callers(.05)
     s.print_callees(.05)
 else:
     unittest.TextTestRunner(verbosity=5).run(
         unittest.makeSuite(TestSequence)
     )
 if not options.debug:
     print test_graph.serialize(format='n3')
コード例 #23
0
ファイル: test.py プロジェクト: slitayem/fuxi
               help='Run the test in verbose mode')
 (options, facts) = op.parse_args()
 for test, name, queryFile, rdfDoc, regime, result, named_graphs in GetTests(
 ):
     if test in SKIP or options.singleTest is not None and options.singleTest != test:
         if test in SKIP and options.debug:
             print "\tSkipping (%s)" % test, SKIP[
                 test]  #>>sys.stderr,SKIP[test],
     elif set(regime).intersection(SUPPORTED_ENTAILMENT):
         test_name = 'test_%s' % test
         test = test_generator(test, name, queryFile, rdfDoc, regime,
                               result, named_graphs, options.debug)
         setattr(TestSequence, test_name, test)
 if options.profile:
     from hotshot import Profile, stats
     p = Profile('fuxi.profile')
     p.runcall(
         unittest.TextTestRunner(verbosity=5).run,
         unittest.makeSuite(TestSequence))
     p.close()
     s = stats.load('fuxi.profile')
     s.strip_dirs()
     s.sort_stats('time', 'cumulative', 'pcalls')
     s.print_stats(.1)
     s.print_callers(.05)
     s.print_callees(.05)
 else:
     unittest.TextTestRunner(verbosity=5).run(
         unittest.makeSuite(TestSequence))
 if not options.debug:
     print test_graph.serialize(format='n3')
コード例 #24
0
     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)
コード例 #25
0
 def runTests(self):
     Profile(stats_file).runcall(main.runTests, self)
コード例 #26
0
    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)
コード例 #27
0
def begin_profiling(path=''):
    global profiler, profile_name

    if profiler is None:
        profile_name = find_profile(path=path)
        profiler = Profile(profile_name)
コード例 #28
0
ファイル: lint.py プロジェクト: facuman/environment
    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
        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 occured 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 some fatal message issued                                           
    * 2 if some error message issued                                           
    * 4 if some warning message issued                                         
    * 8 if some refactor message issued                                        
    * 16 if some convention message 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 = get_csv(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)
        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)
コード例 #29
0
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
コード例 #30
0
ファイル: setup_test.py プロジェクト: zhiwenuil/WMCore
        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 >> 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)
            sys.path.pop(0)
コード例 #31
0
class ProfileMiddleware(object):
    """
    Displays hotshot profiling for any view.
    http://yoursite.com/yourview/?prof

    Add the "prof" key to query string by appending ?prof (or &prof=)
    and you'll see the profiling results in your browser.
    It's set up to only be available in django's debug mode,
    is available for superuser otherwise, but you really shouldn't add this
    middleware to any production configuration.

    WARNING: It uses hotshot profiler which is not thread safe.
    """
    def __init__(self):
        self.tmpfile = None
        self.prof = None

    def process_request(self, request):
        if (settings.DEBUG or request.user.is_superuser) \
                and request.method == 'GET' and request.GET.get('prof', False):
            self.tmpfile = mktemp()
            self.prof = Profile(self.tmpfile)

    def process_view(self, request, callback, callback_args, callback_kwargs):
        if (settings.DEBUG or request.user.is_superuser) \
                and request.method == 'GET' and request.GET.get('prof', False):
            return self.prof.runcall(callback, request, *callback_args,
                                     **callback_kwargs)

    @staticmethod
    def get_group(f):
        for g in group_prefix_re:
            name = g.findall(f)
            if name:
                return name[0]

    @staticmethod
    def get_summary(results_dict, sum_obj):
        group = [(item[1], item[0]) for item in results_dict.items()]
        group.sort(reverse=True)
        group = list[:40]

        res = "      tottime\n"
        for item in group:
            res += "%4.1f%% %7.3f %s\n" % \
                   (100 * item[0] / sum_obj if sum_obj else 0, item[0], item[1])
        return res

    def summary_for_files(self, stats_str):
        stats_str = stats_str.split("\n")[5:]

        mystats = {}
        mygroups = {}

        sum_obj = 0

        for s in stats_str:
            fields = words_re.split(s)
            if len(fields) == 7:
                time = float(fields[2])
                sum_obj += time
                file_name = fields[6].split(":")[0]

                if file_name not in mystats:
                    mystats[file_name] = 0
                mystats[file_name] += time

                group = self.get_group(file_name)
                if group not in mygroups:
                    mygroups[group] = 0
                mygroups[group] += time

        return '<pre> ---- By file ----\n\n{}\n ' \
               '---- By group ---\n\n{}</pre>'.\
            format(self.get_summary(mystats, sum_obj),
                   self.get_summary(mygroups, sum_obj))

    def process_response(self, request, response):
        if (settings.DEBUG or request.user.is_superuser) \
                and request.method == 'GET' and request.GET.get('prof', False):
            self.prof.close()

            out = StringIO.StringIO()
            old_stdout = sys.stdout
            sys.stdout = out

            obj_stats = stats.load(self.tmpfile)
            obj_stats.sort_stats('time', 'calls')
            obj_stats.print_stats()

            sys.stdout = old_stdout
            stats_str = out.getvalue()

            if response and response.content and stats_str:
                response.content = "<pre>" + stats_str + "</pre>"

            response.content = "\n".join(response.content.split("\n")[:40])

            response.content += self.summary_for_files(stats_str)

            unlink(self.tmpfile)

        return response