Python pstats.Stats() Examples

The following are 30 code examples of pstats.Stats(). 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 pstats , or try the search function .
Example #1
Source File: profile.py    From ripozo with GNU General Public License v2.0 8 votes vote down vote up
def profileit(func):
    """
    Decorator straight up stolen from stackoverflow
    """
    def wrapper(*args, **kwargs):
        datafn = func.__name__ + ".profile" # Name the data file sensibly
        prof = cProfile.Profile()
        prof.enable()
        retval = prof.runcall(func, *args, **kwargs)
        prof.disable()
        stats = pstats.Stats(prof)
        stats.sort_stats('tottime').print_stats(20)
        print()
        print()
        stats.sort_stats('cumtime').print_stats(20)
        return retval

    return wrapper 
Example #2
Source File: __init__.py    From pledgeservice with Apache License 2.0 6 votes vote down vote up
def profile(cmd, globals, locals, sort_order, callers): # pragma: no cover
    # runs a command under the profiler and print profiling output at shutdown
    import os
    import profile
    import pstats
    import tempfile
    fd, fn = tempfile.mkstemp()
    try:
        profile.runctx(cmd, globals, locals, fn)
        stats = pstats.Stats(fn)
        stats.strip_dirs()
        # calls,time,cumulative and cumulative,calls,time are useful
        stats.sort_stats(*sort_order or ('cumulative', 'calls', 'time'))
        if callers:
            stats.print_callers(.3)
        else:
            stats.print_stats(.3)
    finally:
        os.remove(fn) 
Example #3
Source File: gw_iter.py    From pyscf with Apache License 2.0 6 votes vote down vote up
def profile(fnc):
    """
    Profiles any function in following class just by adding @profile above function
    """
    import cProfile, pstats, io
    def inner (*args, **kwargs):
        pr = cProfile.Profile()
        pr.enable()
        retval = fnc (*args, **kwargs)
        pr.disable()
        s = io.StringIO()
        sortby = 'cumulative'   #Ordered
        ps = pstats.Stats(pr,stream=s).strip_dirs().sort_stats(sortby)
        n=20                    #reduced the list to be monitored
        ps.print_stats(n)
        #ps.dump_stats("profile.prof")
        print(s.getvalue())
        return retval
    return inner 
Example #4
Source File: test_profile.py    From oss-ftp with MIT License 6 votes vote down vote up
def test_calling_conventions(self):
        # Issue #5330: profile and cProfile wouldn't report C functions called
        # with keyword arguments. We test all calling conventions.
        stmts = [
            "[].sort()",
            "[].sort(reverse=True)",
            "[].sort(*(None, None, True))",
            "[].sort(**dict(reverse=True))",
        ]
        for stmt in stmts:
            s = StringIO()
            prof = self.profilerclass(timer, 0.001)
            prof.runctx(stmt, globals(), locals())
            stats = pstats.Stats(prof, stream=s)
            stats.print_stats()
            res = s.getvalue()
            self.assertIn(self.expected_list_sort_output, res,
                "Profiling {0!r} didn't report list.sort:\n{1}".format(stmt, res)) 
Example #5
Source File: test_profile.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_calling_conventions(self):
        # Issue #5330: profile and cProfile wouldn't report C functions called
        # with keyword arguments. We test all calling conventions.
        stmts = [
            "[].sort()",
            "[].sort(reverse=True)",
            "[].sort(*(None, None, True))",
            "[].sort(**dict(reverse=True))",
        ]
        for stmt in stmts:
            s = StringIO()
            prof = self.profilerclass(timer, 0.001)
            prof.runctx(stmt, globals(), locals())
            stats = pstats.Stats(prof, stream=s)
            stats.print_stats()
            res = s.getvalue()
            self.assertIn(self.expected_list_sort_output, res,
                "Profiling {0!r} didn't report list.sort:\n{1}".format(stmt, res)) 
Example #6
Source File: profiler.py    From cherrypy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def stats(self, filename, sortby='cumulative'):
        """:rtype stats(index): output of print_stats() for the given profile.
        """
        sio = io.StringIO()
        if sys.version_info >= (2, 5):
            s = pstats.Stats(os.path.join(self.path, filename), stream=sio)
            s.strip_dirs()
            s.sort_stats(sortby)
            s.print_stats()
        else:
            # pstats.Stats before Python 2.5 didn't take a 'stream' arg,
            # but just printed to stdout. So re-route stdout.
            s = pstats.Stats(os.path.join(self.path, filename))
            s.strip_dirs()
            s.sort_stats(sortby)
            oldout = sys.stdout
            try:
                sys.stdout = sio
                s.print_stats()
            finally:
                sys.stdout = oldout
        response = sio.getvalue()
        sio.close()
        return response 
Example #7
Source File: profwidget.py    From codimension with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, scriptName, params, reportTime, dataFile, parent=None):
        MainWindowTabWidgetBase.__init__(self)
        QWidget.__init__(self, parent)

        # The same stats object is needed for both - a table and a graph
        # So, parse profile output once and then pass the object further
        stats = pstats.Stats(dataFile)
        stats.calc_callees()

        self.__profTable = ProfileTableViewer(scriptName, params, reportTime,
                                              dataFile, stats, self)
        self.__profGraph = ProfileGraphViewer(scriptName, params, reportTime,
                                              dataFile, stats, self)
        self.__profTable.hide()

        self.__profTable.sigEscapePressed.connect(self.__onEsc)
        self.__profGraph.sigEscapePressed.connect(self.__onEsc)

        self.__createLayout() 
Example #8
Source File: profiler.py    From vprof with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _profile_package(self):
        """Runs cProfile on a package."""
        prof = cProfile.Profile()
        prof.enable()
        try:
            runpy.run_path(self._run_object, run_name='__main__')
        except SystemExit:
            pass
        prof.disable()
        prof_stats = pstats.Stats(prof)
        prof_stats.calc_callees()
        return {
            'objectName': self._object_name,
            'callStats': self._transform_stats(prof_stats),
            'totalTime': prof_stats.total_tt,
            'primitiveCalls': prof_stats.prim_calls,
            'totalCalls': prof_stats.total_calls,
            'timestamp': int(time.time())
        } 
Example #9
Source File: test_profile.py    From BinderFilter with MIT License 6 votes vote down vote up
def test_calling_conventions(self):
        # Issue #5330: profile and cProfile wouldn't report C functions called
        # with keyword arguments. We test all calling conventions.
        stmts = [
            "[].sort()",
            "[].sort(reverse=True)",
            "[].sort(*(None, None, True))",
            "[].sort(**dict(reverse=True))",
        ]
        for stmt in stmts:
            s = StringIO()
            prof = self.profilerclass(timer, 0.001)
            prof.runctx(stmt, globals(), locals())
            stats = pstats.Stats(prof, stream=s)
            stats.print_stats()
            res = s.getvalue()
            self.assertIn(self.expected_list_sort_output, res,
                "Profiling {0!r} didn't report list.sort:\n{1}".format(stmt, res)) 
Example #10
Source File: profiler.py    From vprof with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _profile_module(self):
        """Runs cProfile on a module."""
        prof = cProfile.Profile()
        try:
            with open(self._run_object, 'rb') as srcfile:
                code = compile(srcfile.read(), self._run_object, 'exec')
            prof.runctx(code, self._globs, None)
        except SystemExit:
            pass
        prof_stats = pstats.Stats(prof)
        prof_stats.calc_callees()
        return {
            'objectName': self._object_name,
            'callStats': self._transform_stats(prof_stats),
            'totalTime': prof_stats.total_tt,
            'primitiveCalls': prof_stats.prim_calls,
            'totalCalls': prof_stats.total_calls,
            'timestamp': int(time.time())
        } 
Example #11
Source File: profiler.py    From vprof with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def profile_function(self):
        """Runs cProfile on a function."""
        prof = cProfile.Profile()
        prof.enable()
        result = self._run_object(*self._run_args, **self._run_kwargs)
        prof.disable()
        prof_stats = pstats.Stats(prof)
        prof_stats.calc_callees()
        return {
            'objectName': self._object_name,
            'callStats': self._transform_stats(prof_stats),
            'totalTime': prof_stats.total_tt,
            'primitiveCalls': prof_stats.prim_calls,
            'totalCalls': prof_stats.total_calls,
            'result': result,
            'timestamp': int(time.time())
        } 
Example #12
Source File: main.py    From eyeD3 with GNU General Public License v3.0 6 votes vote down vote up
def profileMain(args, config):  # pragma: no cover
    """This is the main function for profiling
    http://code.google.com/appengine/kb/commontasks.html#profiling
    """
    import cProfile
    import pstats

    eyed3.log.debug("driver profileMain")
    prof = cProfile.Profile()
    prof = prof.runctx("main(args)", globals(), locals())

    stream = StringIO()
    stats = pstats.Stats(prof, stream=stream)
    stats.sort_stats("time")  # Or cumulative
    stats.print_stats(100)  # 80 = how many to print

    # The rest is optional.
    stats.print_callees()
    stats.print_callers()
    sys.stderr.write("Profile data:\n%s\n" % stream.getvalue())

    return 0 
Example #13
Source File: test_profile.py    From BinderFilter with MIT License 5 votes vote down vote up
def do_profiling(cls):
        results = []
        prof = cls.profilerclass(timer, 0.001)
        start_timer = timer()
        prof.runctx("testfunc()", globals(), locals())
        results.append(timer() - start_timer)
        for methodname in cls.methodnames:
            s = StringIO()
            stats = pstats.Stats(prof, stream=s)
            stats.strip_dirs().sort_stats("stdname")
            getattr(stats, methodname)()
            results.append(s.getvalue())
        return results 
Example #14
Source File: stats.py    From BinderFilter with MIT License 5 votes vote down vote up
def load(self):
        # The timer selected by the profiler should never be used, so make
        # sure it doesn't work:
        p = Profile()
        p.get_time = _brokentimer
        log = hotshot.log.LogReader(self._logfn)
        taccum = 0
        for event in log:
            what, (filename, lineno, funcname), tdelta = event
            if tdelta > 0:
                taccum += tdelta

            # We multiply taccum to convert from the microseconds we
            # have to the seconds that the profile/pstats module work
            # with; this allows the numbers to have some basis in
            # reality (ignoring calibration issues for now).

            if what == ENTER:
                frame = self.new_frame(filename, lineno, funcname)
                p.trace_dispatch_call(frame, taccum * .000001)
                taccum = 0

            elif what == EXIT:
                frame = self.pop_frame()
                p.trace_dispatch_return(frame, taccum * .000001)
                taccum = 0

            # no further work for line events

        assert not self._stack
        return pstats.Stats(p) 
Example #15
Source File: cProfile.py    From BinderFilter with MIT License 5 votes vote down vote up
def print_stats(self, sort=-1):
        import pstats
        pstats.Stats(self).strip_dirs().sort_stats(sort).print_stats() 
Example #16
Source File: profile.py    From BinderFilter with MIT License 5 votes vote down vote up
def print_stats(self, sort=-1):
        import pstats
        pstats.Stats(self).strip_dirs().sort_stats(sort). \
                  print_stats() 
Example #17
Source File: profile.py    From oss-ftp with MIT License 5 votes vote down vote up
def main():
    usage = "profile.py [-o output_file_path] [-s sort] scriptfile [arg] ..."
    parser = OptionParser(usage=usage)
    parser.allow_interspersed_args = False
    parser.add_option('-o', '--outfile', dest="outfile",
        help="Save stats to <outfile>", default=None)
    parser.add_option('-s', '--sort', dest="sort",
        help="Sort order when printing to stdout, based on pstats.Stats class",
        default=-1)

    if not sys.argv[1:]:
        parser.print_usage()
        sys.exit(2)

    (options, args) = parser.parse_args()
    sys.argv[:] = args

    if len(args) > 0:
        progname = args[0]
        sys.path.insert(0, os.path.dirname(progname))
        with open(progname, 'rb') as fp:
            code = compile(fp.read(), progname, 'exec')
        globs = {
            '__file__': progname,
            '__name__': '__main__',
            '__package__': None,
        }
        runctx(code, globs, None, options.outfile, options.sort)
    else:
        parser.print_usage()
    return parser

# When invoked as main program, invoke the profiler on a script 
Example #18
Source File: cProfile.py    From oss-ftp with MIT License 5 votes vote down vote up
def print_stats(self, sort=-1):
        import pstats
        pstats.Stats(self).strip_dirs().sort_stats(sort).print_stats() 
Example #19
Source File: stats.py    From oss-ftp with MIT License 5 votes vote down vote up
def load(self):
        # The timer selected by the profiler should never be used, so make
        # sure it doesn't work:
        p = Profile()
        p.get_time = _brokentimer
        log = hotshot.log.LogReader(self._logfn)
        taccum = 0
        for event in log:
            what, (filename, lineno, funcname), tdelta = event
            if tdelta > 0:
                taccum += tdelta

            # We multiply taccum to convert from the microseconds we
            # have to the seconds that the profile/pstats module work
            # with; this allows the numbers to have some basis in
            # reality (ignoring calibration issues for now).

            if what == ENTER:
                frame = self.new_frame(filename, lineno, funcname)
                p.trace_dispatch_call(frame, taccum * .000001)
                taccum = 0

            elif what == EXIT:
                frame = self.pop_frame()
                p.trace_dispatch_return(frame, taccum * .000001)
                taccum = 0

            # no further work for line events

        assert not self._stack
        return pstats.Stats(p) 
Example #20
Source File: profile.py    From BinderFilter with MIT License 5 votes vote down vote up
def main():
    usage = "profile.py [-o output_file_path] [-s sort] scriptfile [arg] ..."
    parser = OptionParser(usage=usage)
    parser.allow_interspersed_args = False
    parser.add_option('-o', '--outfile', dest="outfile",
        help="Save stats to <outfile>", default=None)
    parser.add_option('-s', '--sort', dest="sort",
        help="Sort order when printing to stdout, based on pstats.Stats class",
        default=-1)

    if not sys.argv[1:]:
        parser.print_usage()
        sys.exit(2)

    (options, args) = parser.parse_args()
    sys.argv[:] = args

    if len(args) > 0:
        progname = args[0]
        sys.path.insert(0, os.path.dirname(progname))
        with open(progname, 'rb') as fp:
            code = compile(fp.read(), progname, 'exec')
        globs = {
            '__file__': progname,
            '__name__': '__main__',
            '__package__': None,
        }
        runctx(code, globs, None, options.outfile, options.sort)
    else:
        parser.print_usage()
    return parser

# When invoked as main program, invoke the profiler on a script 
Example #21
Source File: profile.py    From BinderFilter with MIT License 5 votes vote down vote up
def Stats(*args):
    print 'Report generating functions are in the "pstats" module\a' 
Example #22
Source File: profiler.py    From indy-plenum with Apache License 2.0 5 votes vote down vote up
def profile_this(func, *args, **kwargs):
    pr = cProfile.Profile()
    # calculated on Jason's machine using 'calibrate' function; YMMV
    pr.bias = 7.328898416011422e-07
    pr.enable()

    r = func(*args, **kwargs)
    pr.disable()
    p = pstats.Stats(pr)
    print("Cumulative top 50")
    p.sort_stats('cumulative').print_stats(500)
    print("Time top 50")
    p.sort_stats('time').print_stats(500)
    return r 
Example #23
Source File: cli.py    From daf-recipes with GNU General Public License v3.0 5 votes vote down vote up
def command(self):
        self._load_config_into_test_app()

        import paste.fixture
        import cProfile
        import re

        url = self.args[0]
        if self.args[1:]:
            user = self.args[1]
        else:
            user = 'visitor'

        def profile_url(url):
            try:
                res = self.app.get(url, status=[200],
                                   extra_environ={'REMOTE_USER': user})
            except paste.fixture.AppError:
                print 'App error: ', url.strip()
            except KeyboardInterrupt:
                raise
            except:
                import traceback
                traceback.print_exc()
                print 'Unknown error: ', url.strip()

        output_filename = 'ckan%s.profile' % re.sub('[/?]', '.', url.replace('/', '.'))
        profile_command = "profile_url('%s')" % url
        cProfile.runctx(profile_command, globals(), locals(), filename=output_filename)
        import pstats
        stats = pstats.Stats(output_filename)
        stats.sort_stats('cumulative')
        stats.print_stats(0.1)  # show only top 10% of lines
        print 'Only top 10% of lines shown'
        print 'Written profile to: %s' % output_filename 
Example #24
Source File: prof.py    From SISideBar with MIT License 5 votes vote down vote up
def profileFunction(sortKey="time", rows=30):
	def _(f):
		@functools.wraps(_)
		def __(*fargs, **fkwargs):
			prof = profile.Profile()
			ret = prof.runcall(f, *fargs, **fkwargs)
			pstats.Stats(prof).strip_dirs().sort_stats(sortKey).print_stats(rows)

			return ret

		return __
	return _
    
#周回カウンター 
Example #25
Source File: profiler.py    From edgedb with Apache License 2.0 5 votes vote down vote up
def find_singledispatch_wrapper(
    stats: Stats, *, regular_location: bool = False
) -> FunctionID:
    """Returns the singledispatch wrapper function ID tuple.

    Raises LookupError if not found.
    """
    if regular_location:
        functools_path = re.compile(r"python3.\d+/functools.py$")
        dispatch_name = "dispatch"
        wrapper_name = "wrapper"
    else:
        functools_path = re.compile(r"profiling/tracing_singledispatch.py$")
        dispatch_name = "dispatch"
        wrapper_name = "sd_wrapper"

    for (modpath, _lineno, funcname), (_, _, _, _, callers) in stats.items():
        if funcname != dispatch_name:
            continue

        m = functools_path.search(modpath)
        if not m:
            continue

        # Using this opportunity, we're figuring out which `wrapper` from
        # functools in the trace is the singledispatch `wrapper` (there
        # are three more others in functools.py).
        for caller_modpath, caller_lineno, caller_funcname in callers:
            if caller_funcname == wrapper_name:
                m = functools_path.search(modpath)
                if not m:
                    continue

                return (caller_modpath, caller_lineno, caller_funcname)

        raise LookupError("singledispatch.dispatch without wrapper?")

    raise LookupError("No singledispatch use in provided stats") 
Example #26
Source File: ui_bridge.py    From python-gui with Apache License 2.0 5 votes vote down vote up
def _ui_event_loop(self):
        self._sem.acquire()
        if self._profile:
            import StringIO
            import cProfile
            import pstats
            pr = cProfile.Profile()
            pr.enable()
        self._ui.start(self)
        if self._profile:
            pr.disable()
            s = StringIO.StringIO()
            ps = pstats.Stats(pr, stream=s)
            ps.strip_dirs().sort_stats(self._profile).print_stats(30)
            self._profile = s.getvalue() 
Example #27
Source File: profiling.py    From marvin-python-toolbox with Apache License 2.0 5 votes vote down vote up
def __exit__(self, type, value, traceback):
        if self.enable_profiling:
            pr = self.pr
            pr.disable()
            # args accept functions
            output_path = self.output_path
            uid = self.uid
            info = self.info
            if callable(uid):
                uid = uid()

            # make sure the output path exists
            if not os.path.exists(output_path):  # pragma: no cover
                os.makedirs(output_path, mode=0o774)

            # collect profiling info
            stats = pstats.Stats(pr)
            stats.sort_stats(self.sortby)
            info_path = os.path.join(output_path, '{}.json'.format(uid))
            stats_path = os.path.join(output_path, '{}.pstats'.format(uid))
            dot_path = os.path.join(output_path, '{}.dot'.format(uid))
            png_path = os.path.join(output_path, '{}.png'.format(uid))
            if info:
                try:
                    with open(info_path, 'w') as fp:
                        json.dump(info, fp, indent=2, encoding='utf-8')
                except Exception as e:
                    logger.error('An error occurred while saving %s: %s.', info_path, e)
            stats.dump_stats(stats_path)
            # create profiling graph
            try:
                subprocess.call(['gprof2dot', '-f', 'pstats', '-o', dot_path, stats_path])
                subprocess.call(['dot', '-Tpng', '-o', png_path, dot_path])
                pr.image_path = png_path
            except Exception:
                logger.error('An error occurred while creating profiling image! '
                             'Please make sure you have installed GraphViz.')
            logger.info('Saving profiling data (%s)', stats_path[:-7]) 
Example #28
Source File: profiling.py    From marvin-python-toolbox with Apache License 2.0 5 votes vote down vote up
def _repr_html_(self):
        s = StringIO()
        stats = pstats.Stats(self, stream=s).sort_stats(self.sortby)
        stats.print_stats(10)
        stats_value = s.getvalue()
        html = '<pre>{}</pre>'.format(stats_value)
        if self.image_path:
            html += '<img src="{}" style="margin: 0 auto;">'.format(self.image_path)
        return html 
Example #29
Source File: diagnose.py    From fuzzdb-collect with GNU General Public License v3.0 5 votes vote down vote up
def profile(num_elements=100000, parser="lxml"):

    filehandle = tempfile.NamedTemporaryFile()
    filename = filehandle.name

    data = rdoc(num_elements)
    vars = dict(bs4=bs4, data=data, parser=parser)
    cProfile.runctx('bs4.BeautifulSoup(data, parser)' , vars, vars, filename)

    stats = pstats.Stats(filename)
    # stats.strip_dirs()
    stats.sort_stats("cumulative")
    stats.print_stats('_html5lib|bs4', 50) 
Example #30
Source File: test_profile.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_cprofile(self):
        results = self.do_profiling()
        self.assertEqual(results[0], 1000)
        for i, method in enumerate(self.methodnames):
            self.assertEqual(results[i+1], self.expected_output[method],
                             "Stats.%s output for %s doesn't fit expectation!" %
                             (method, self.profilerclass.__name__))