Python cProfile.run() Examples

The following are 30 code examples of cProfile.run(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cProfile , or try the search function .
Example #1
Source File: profiling.py    From NoobSec-Toolkit with GNU General Public License v2.0 6 votes vote down vote up
def profile(profileOutputFile=None, dotOutputFile=None, imageOutputFile=None):
    """
    This will run the program and present profiling data in a nice looking graph
    """

    try:
        from thirdparty.gprof2dot import gprof2dot
        from thirdparty.xdot import xdot
        import gobject
        import gtk
        import pydot
    except ImportError, e:
        errMsg = "profiling requires third-party libraries (%s). " % getUnicode(e, UNICODE_ENCODING)
        errMsg += "Quick steps:%s" % os.linesep
        errMsg += "1) sudo apt-get install python-pydot python-pyparsing python-profiler graphviz"
        logger.error(errMsg)

        return 
Example #2
Source File: foomodulegen-auto.py    From royal-chaos with MIT License 6 votes vote down vote up
def main():
    if sys.argv[1] == '-d':
        del sys.argv[1]
        import pdb
        pdb.set_trace()
        my_module_gen()
    else:
        import os
        if "PYBINDGEN_ENABLE_PROFILING" in os.environ:
            try:
                import cProfile as profile
            except ImportError:
                my_module_gen()
            else:
                print("** running under profiler", file=sys.stderr)
                profile.run('my_module_gen()', 'foomodulegen-auto.pstat')
        else:
            my_module_gen() 
Example #3
Source File: script.py    From allura with Apache License 2.0 6 votes vote down vote up
def command(self):
        with warnings.catch_warnings():
            try:
                from sqlalchemy import exc
            except ImportError:
                pass
            else:
                warnings.simplefilter("ignore", category=exc.SAWarning)
            self.basic_setup()
            request = webob.Request.blank('--script--', environ={
                'paste.registry': self.registry})
            tg.request_local.context.request = request
            if self.options.pdb:
                base.log.info('Installing exception hook')
                sys.excepthook = utils.postmortem_hook
            with open(self.args[1]) as fp:
                ns = dict(__name__='__main__')
                sys.argv = self.args[1:]
                if self.options.profile:
                    cProfile.run(fp, '%s.profile' %
                                 os.path.basename(self.args[1]))
                else:
                    exec(fp.read(), ns) 
Example #4
Source File: profiling.py    From NoobSec-Toolkit with GNU General Public License v2.0 6 votes vote down vote up
def profile(profileOutputFile=None, dotOutputFile=None, imageOutputFile=None):
    """
    This will run the program and present profiling data in a nice looking graph
    """

    try:
        from thirdparty.gprof2dot import gprof2dot
        from thirdparty.xdot import xdot
        import gobject
        import gtk
        import pydot
    except ImportError, e:
        errMsg = "profiling requires third-party libraries (%s). " % getUnicode(e, UNICODE_ENCODING)
        errMsg += "Quick steps:%s" % os.linesep
        errMsg += "1) sudo apt-get install python-pydot python-pyparsing python-profiler graphviz"
        logger.error(errMsg)

        return 
Example #5
Source File: chimay_red.py    From Chimay-Red with MIT License 6 votes vote down vote up
def main():
    """ DocstringNotImplemented """

    # set pwntools context for binary file
    if TARGET.binary:
        context.binary = TARGET.binary
    if TARGET.debug:
        # Setup pwnlib context for tmux debug automation
        context.terminal = ['tmux', '-L', 'chimay-red', 'splitw', '-v', '-p', '50']
        # run remote gdbserver attached to `www` PID on TARGET
        run_new_remote_gdbserver(TARGET.rhost, TARGET.gdbport)
        # attach and connect to remote gdbserver on TARGET
        attach_gdb_server(TARGET.rhost, TARGET.gdbport, TARGET.binary, TARGET.breakpoints.split(","))

    if TARGET.shellcommand:
        Command(TARGET.vector, TARGET.rhost, TARGET.lhost, TARGET.shellcommand, command="custom_shell_command")
    else:
        Command(TARGET.vector, TARGET.rhost, TARGET.lhost, command=TARGET.command)


# Run the script 
Example #6
Source File: chimay_red.py    From Chimay-Red with MIT License 6 votes vote down vote up
def profile_main():
    """

    :return:
    """
    log.info("Profiling: ENABLED")
    # Enable memory usage profiling at the line level
    tracemalloc.start()
    # Enable CPU usage/function call timing/rate at the function level
    # Automatigically dumps profile to `filename` for further analysis
    cProfile.run("main()", filename=(CWD + "/chimay-red.cprof"))
    # Take snapshot of traced malloc profile
    snapshot = tracemalloc.take_snapshot()
    # Print snapshot statistics filtering for only `tracefiles`
    display_top(snapshot, limit=20, modpaths=TRACEFILES)

    return 0 
Example #7
Source File: profiling.py    From POC-EXP with GNU General Public License v3.0 6 votes vote down vote up
def profile(profileOutputFile=None, dotOutputFile=None, imageOutputFile=None):
    """
    This will run the program and present profiling data in a nice looking graph
    """

    try:
        from thirdparty.gprof2dot import gprof2dot
        from thirdparty.xdot import xdot
        import gobject
        import gtk
        import pydot
    except ImportError, e:
        errMsg = "profiling requires third-party libraries (%s). " % getUnicode(e, UNICODE_ENCODING)
        errMsg += "Quick steps:%s" % os.linesep
        errMsg += "1) sudo apt-get install python-pydot python-pyparsing python-profiler graphviz"
        logger.error(errMsg)

        return 
Example #8
Source File: profiler.py    From panflute with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def run():
    print('\nLoading JSON...')
    input_fn = 'benchmark.json'
    output_fn = 'panflute.json'

    with open(input_fn, encoding='utf-8') as f:
        doc = pf.load(f)

    print('\nApplying trivial filter...')
    doc = doc.walk(action=empty_test, doc=doc)

    print('Dumping JSON...')
    with open(output_fn, mode='w', encoding='utf-8') as f:
        pf.dump(doc, f)
        f.write('\n')

    print(' - Done!') 
Example #9
Source File: anonymizer.py    From Clustering_based_K_Anon with MIT License 6 votes vote down vote up
def get_result_n(att_trees, data, type_alg, k=DEFAULT_K, n=10):
    """
    run clustering_based_k_anon for n time, with k=10
    """
    print "K=%d" % k
    data_back = copy.deepcopy(data)
    n_ncp = 0.0
    n_time = 0.0
    for i in range(n):
        _, eval_result = clustering_based_k_anon(att_trees, data, type_alg, k)
        data = copy.deepcopy(data_back)
        n_ncp += eval_result[0]
        n_time += eval_result[1]
    n_ncp = n_ncp / n
    n_time = n_ncp / n
    print "Run %d times" % n
    print "NCP %0.2f" % n_ncp + "%"
    print "Running time %0.2f" % n_time + " seconds" 
Example #10
Source File: profiling.py    From EasY_HaCk with Apache License 2.0 6 votes vote down vote up
def profile(profileOutputFile=None, dotOutputFile=None, imageOutputFile=None):
    """
    This will run the program and present profiling data in a nice looking graph
    """

    try:
        __import__("gobject")
        from thirdparty.gprof2dot import gprof2dot
        from thirdparty.xdot import xdot
        import gtk
        import pydot
    except ImportError, e:
        errMsg = "profiling requires third-party libraries ('%s') " % getUnicode(e, UNICODE_ENCODING)
        errMsg += "(Hint: 'sudo apt-get install python-pydot python-pyparsing python-profiler graphviz')"
        logger.error(errMsg)

        return 
Example #11
Source File: processor.py    From Greynir with GNU General Public License v3.0 6 votes vote down vote up
def main():
    """ Main function to invoke for profiling """
    import cProfile as profile
    import pstats

    global _PROFILING

    _PROFILING = True
    filename = "Processor.profile"
    profile.run("_main()", filename)
    stats = pstats.Stats(filename)
    # Clean up filenames for the report
    stats.strip_dirs()
    # Sort the statistics by the total time spent in the function itself
    stats.sort_stats("tottime")
    stats.print_stats(100)  # Print 100 most significant lines 
Example #12
Source File: logging.py    From scvelo with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def timeout(func, args=(), timeout_duration=2, default=None, **kwargs):
    """This will spwan a thread and run the given function using the args, kwargs and
    return the given default value if the timeout_duration is exceeded
    """
    import threading

    class InterruptableThread(threading.Thread):
        def __init__(self):
            threading.Thread.__init__(self)
            self.result = default

        def run(self):
            try:
                self.result = func(*args, **kwargs)
            except:
                pass

    it = InterruptableThread()
    it.start()
    it.join(timeout_duration)
    return it.result 
Example #13
Source File: logging.py    From scvelo with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def profiler(command, filename="profile.stats", n_stats=10):
    """Profiler for a python program

    Runs cProfile and outputs ordered statistics that describe
    how often and for how long various parts of the program are executed.

    Stats can be visualized with `!snakeviz profile.stats`.

    Parameters
    ----------
    command: str
        Command string to be executed.
    filename: str
        Name under which to store the stats.
    n_stats: int or None
        Number of top stats to show.
    """
    import cProfile, pstats

    cProfile.run(command, filename)
    stats = pstats.Stats(filename).strip_dirs().sort_stats("time")
    return stats.print_stats(n_stats or {}) 
Example #14
Source File: pdbparseFile.py    From biskit with GNU General Public License v3.0 5 votes vote down vote up
def __xplorAtomIndicesTest( self, source ):
        """
        In some cases the setup with parallell xplor trajectories
        run out of atom indices when writing the pdb files to disc.
        When this happens (usualy for the TIP3 waters in the later
        of the 10 parallell trajectories) the atom indices get
        replaced with ***** which will cause the parsing to fail.
        The error message recieved is quite cryptic - this function
        is here to give a more comprehensible message.
        
        :param source: file that failed to be parsed
        :type  source: str
        """
        import re
        f = open( source, 'r' )
        lines = f.readlines()
        f.close()

        for i in range( len(lines) ):
            if re.match( '^ATOM\s{2}\*{5}', lines[i]):
                msg = """
Line %i to %i of the file %s contains invalid atom indices!

In some cases the setup with parallell xplor trajectories run out of atom indices when writing the pdb files to disc. When this happens (usualy for the TIP3 waters in the later of the 10 parallell trajectories) the atom indices get replaced with ***** which will cause the parsing to fail.

REMEDY: run the script fixAtomIndices.py
""" % (i, len(lines), source)

                return msg 
Example #15
Source File: pdbModel.py    From biskit with GNU General Public License v3.0 5 votes vote down vote up
def structureFit( self, refModel, mask=None ):
        """
        Structure-align this model onto a reference model using the external
        TM-Align program (which needs to be installed).
        
        structureFit( refModel [, mask] ) -> PDBModel (or subclass)

        The result model has additional TM-Align statistics in its info record:
        r = m.structureFit( ref )
        r.info['tm_score'] -> TM-Align score
        the other keys are: 'tm_rmsd', 'tm_len', 'tm_id'
        
        .. seealso:: `biskit.TMAlign`

        :param refModel: reference PDBModel
        :type  refModel: PDBModel
        :param mask: atom mask to use for the fit
        :type  mask: list of int (1||0)

        :return: fitted PDBModel or sub-class
        :rtype: PDBModel
        """
        from biskit.exe import tmalign
        
        if mask is not None:
            m_this = self.compress( mask )
        else:
            m_this = self

        tm = tmalign.TMAlign( m_this, refModel )
        r = tm.run()

        return tm.applyTransformation( self ) 
Example #16
Source File: debug.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def run(self):
        while True:
            with self.lock:
                if self._stop is True:
                    return
                    
            print("\n=============  THREAD FRAMES:  ================")
            for id, frame in sys._current_frames().items():
                if id == threading.current_thread().ident:
                    continue
                print("<< thread %d >>" % id)
                traceback.print_stack(frame)
            print("===============================================\n")
            
            time.sleep(self.interval) 
Example #17
Source File: Editor_x64dbg.py    From X64dbg_script_editor with The Unlicense 5 votes vote down vote up
def runtoprob(self):
        try:
            self.path = QtCore.QFileInfo(self.filename).path()
            self.path = QtCore.QFileInfo(self.filename).path()
            g = globals()
            os.chdir(str(self.path))
            script = str(self.codebox.text())
            import cProfile
            cProfile.run(script)
        except Exception as e:
            print e.__doc__
            print e.message
        else:
            import cProfile
            cProfile.run(script) 
Example #18
Source File: tools.py    From biskit with GNU General Public License v3.0 5 votes vote down vote up
def profile( s ):
    """
    Profile the given code fragment and report time-consuming method calls.
    :param s: python code fragment, example: 'm = PDBModel("3tgi")'
    :type  s: str
    """    
    try:
        import cProfile as profile
        fout = tempDir() + '/profiling.out'

        profile.run( s, fout )

        ## Analyzing
        import pstats
        p = pstats.Stats(fout)

        ## long steps and methods calling them
        p.sort_stats('cumulative').print_stats(20)
        p.print_callers(0.0)

        tryRemove( fout )

    except ImportError as why:
        raise ToolsError('Python profiling modules are not installed.')


#############
##  TESTING        
############# 
Example #19
Source File: debug.py    From soapy with GNU General Public License v3.0 5 votes vote down vote up
def findPersistent(self, regex):
        """Return all objects matching regex that were considered 'persistent' when the last diff() was run."""
        return self.findTypes(self.persistentRefs, regex) 
Example #20
Source File: test.py    From emot with GNU General Public License v3.0 5 votes vote down vote up
def main():
    cProfile.run('test_emo()',sort = 'time')
    return None 
Example #21
Source File: debug.py    From soapy with GNU General Public License v3.0 5 votes vote down vote up
def findNew(self, regex):
        """Return all objects matching regex that were considered 'new' when the last diff() was run."""
        return self.findTypes(self.newRefs, regex) 
Example #22
Source File: debug.py    From soapy with GNU General Public License v3.0 5 votes vote down vote up
def profile(code, name='profile_run', sort='cumulative', num=30):
    """Common-use for cProfile"""
    cProfile.run(code, name)
    stats = pstats.Stats(name)
    stats.sort_stats(sort)
    stats.print_stats(num)
    return stats
        
        
  
#### Code for listing (nearly) all objects in the known universe
#### http://utcc.utoronto.ca/~cks/space/blog/python/GetAllObjects
# Recursively expand slist's objects
# into olist, using seen to track
# already processed objects. 
Example #23
Source File: profile_numdifftools.py    From numdifftools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def profile_main():
    import cProfile
    import pstats
    cProfile.run("main()", "{}.profile".format(__file__))
    s = pstats.Stats("{}.profile".format(__file__))
    # s.strip_dirs()
    s.sort_stats("time").print_stats(20) 
Example #24
Source File: pyeditor.py    From Python_editor with The Unlicense 5 votes vote down vote up
def runtoprob(self):
        try:
            self.path = QtCore.QFileInfo(self.filename).path()
        except AttributeError:
            pass
        self.path = QtCore.QFileInfo(self.filename).path()
        g = globals()
        os.chdir(str(self.path))
        script = str(self.codebox.text())
        import cProfile
        cProfile.run(script) 
Example #25
Source File: pyeditor.py    From Python_editor with The Unlicense 5 votes vote down vote up
def runtoprob(self):
        try:
            self.path = QtCore.QFileInfo(self.filename).path()
        except AttributeError:
            pass
        self.path = QtCore.QFileInfo(self.filename).path()
        g = globals()
        os.chdir(str(self.path))
        script = str(self.codebox.text())
        import cProfile
        cProfile.run(script) 
Example #26
Source File: pyeditor.py    From Python_editor with The Unlicense 5 votes vote down vote up
def runtoprob(self):
        try:
            self.path = QtCore.QFileInfo(self.filename).path()
            self.path = QtCore.QFileInfo(self.filename).path()
            g = globals()
            os.chdir(str(self.path))
            script = str(self.codebox.text())
            import cProfile
            cProfile.run(script)
        except Exception as e:
            print e.__doc__
            print e.message
        else:
            import cProfile
            cProfile.run(script) 
Example #27
Source File: pyeditor.py    From Python_editor with The Unlicense 5 votes vote down vote up
def runtoprob(self):
        try:
            self.path = QtCore.QFileInfo(self.filename).path()
            self.path = QtCore.QFileInfo(self.filename).path()
            g = globals()
            os.chdir(str(self.path))
            script = str(self.codebox.text())
            import cProfile
            cProfile.run(script)
        except Exception as e:
            print e.__doc__
            print e.message
        else:
            import cProfile
            cProfile.run(script) 
Example #28
Source File: pyeditor.py    From Python_editor with The Unlicense 5 votes vote down vote up
def runtoprob(self):
        try:
            self.path = QtCore.QFileInfo(self.filename).path()
            self.path = QtCore.QFileInfo(self.filename).path()
            g = globals()
            os.chdir(str(self.path))
            script = str(self.codebox.text())
            import cProfile
            cProfile.run(script)
        except Exception as e:
            print e.__doc__
            print e.message
        else:
            import cProfile
            cProfile.run(script) 
Example #29
Source File: test.py    From emot with GNU General Public License v3.0 5 votes vote down vote up
def main():
    cProfile.run('test_emo()',sort = 'time')
    return None 
Example #30
Source File: debug.py    From tf-pose with Apache License 2.0 5 votes vote down vote up
def findNew(self, regex):
        """Return all objects matching regex that were considered 'new' when the last diff() was run."""
        return self.findTypes(self.newRefs, regex)