Skip to content

bpo-34861: better cProfile CLI defaults: sort by time, restrict to top 20 #13083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions Lib/cProfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
# ____________________________________________________________
# Simple interface

def run(statement, filename=None, sort=-1):
return _pyprofile._Utils(Profile).run(statement, filename, sort)
def run(statement, filename=None, sort=-1, numresults=None):
return _pyprofile._Utils(Profile).run(statement, filename, sort,
numresults)

def runctx(statement, globals, locals, filename=None, sort=-1):
def runctx(statement, globals, locals, filename=None, sort=-1,
numresults=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, indent numresults under statement.

return _pyprofile._Utils(Profile).runctx(statement, globals, locals,
filename, sort)
filename, sort, numresults)

run.__doc__ = _pyprofile.run.__doc__
runctx.__doc__ = _pyprofile.runctx.__doc__
Expand All @@ -37,9 +39,10 @@ class Profile(_lsprof.Profiler):
# Most of the functionality is in the base class.
# This subclass only adds convenient and backward-compatible methods.

def print_stats(self, sort=-1):
def print_stats(self, sort=-1, numresults=None):
import pstats
pstats.Stats(self).strip_dirs().sort_stats(sort).print_stats()
pstats.Stats(self).strip_dirs().sort_stats(sort).print_stats(
numresults)

def dump_stats(self, file):
import marshal
Expand Down Expand Up @@ -155,8 +158,10 @@ def main():
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,
default='time',
choices=sorted(pstats.Stats.sort_arg_dict_default))
parser.add_option('-n', '--numresults', dest="numresults", type="int",
help="Number of results to show", default=20)
parser.add_option('-m', dest="module", action="store_true",
help="Profile a library module", default=False)

Expand Down Expand Up @@ -185,7 +190,8 @@ def main():
'__package__': None,
'__cached__': None,
}
runctx(code, globs, None, options.outfile, options.sort)
runctx(code, globs, None, options.outfile, options.sort,
options.numresults)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, indent options.numresults under code.

else:
parser.print_usage()
return parser
Expand Down
38 changes: 22 additions & 16 deletions Lib/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,37 +47,37 @@ class _Utils:
def __init__(self, profiler):
self.profiler = profiler

def run(self, statement, filename, sort):
def run(self, statement, filename, sort, numresults):
prof = self.profiler()
try:
prof.run(statement)
except SystemExit:
pass
finally:
self._show(prof, filename, sort)
self._show(prof, filename, sort, numresults)

def runctx(self, statement, globals, locals, filename, sort):
def runctx(self, statement, globals, locals, filename, sort, numresults):
prof = self.profiler()
try:
prof.runctx(statement, globals, locals)
except SystemExit:
pass
finally:
self._show(prof, filename, sort)
self._show(prof, filename, sort, numresults)

def _show(self, prof, filename, sort):
def _show(self, prof, filename, sort, numresults):
if filename is not None:
prof.dump_stats(filename)
else:
prof.print_stats(sort)
prof.print_stats(sort, numresults)


#**************************************************************************
# The following are the static member functions for the profiler class
# Note that an instance of Profile() is *not* needed to call them.
#**************************************************************************

def run(statement, filename=None, sort=-1):
def run(statement, filename=None, sort=-1, numresults=None):
"""Run statement under profiler optionally saving results in filename

This function takes a single argument that can be passed to the
Expand All @@ -88,15 +88,17 @@ def run(statement, filename=None, sort=-1):
standard name string (file/line/function-name) that is presented in
each line.
"""
return _Utils(Profile).run(statement, filename, sort)
return _Utils(Profile).run(statement, filename, sort, numresults)

def runctx(statement, globals, locals, filename=None, sort=-1):
def runctx(statement, globals, locals, filename=None, sort=-1,
numresults=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same for this row. :)

"""Run statement under profiler, supplying your own globals and locals,
optionally saving results in filename.

statement and filename have the same semantics as profile.run
"""
return _Utils(Profile).runctx(statement, globals, locals, filename, sort)
return _Utils(Profile).runctx(statement, globals, locals, filename, sort,
numresults)


class Profile:
Expand Down Expand Up @@ -383,10 +385,10 @@ def simulate_cmd_complete(self):
self.t = get_time() - t


def print_stats(self, sort=-1):
def print_stats(self, sort=-1, numresults=None):
import pstats
pstats.Stats(self).strip_dirs().sort_stats(sort). \
print_stats()
pstats.Stats(self).strip_dirs().sort_stats(sort).print_stats(
numresults)

def dump_stats(self, file):
with open(file, 'wb') as f:
Expand Down Expand Up @@ -566,18 +568,21 @@ def f(m, f1=f1):

def main():
import os
import pstats
from optparse import OptionParser

usage = "profile.py [-o output_file_path] [-s sort] [-m module | scriptfile] [arg] ..."
usage = "profile.py [-o output_file_path] [-s sort] [-n limit] [-m module | 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('-m', dest="module", action="store_true",
help="Profile a library module.", default=False)
parser.add_option('-n', '--numresults', dest="numresults", type="int",
help="Number of results to show", default=20)
parser.add_option('-s', '--sort', dest="sort",
help="Sort order when printing to stdout, based on pstats.Stats class",
default=-1)
default='time', choices=sorted(pstats.Stats.sort_arg_dict_default))

if not sys.argv[1:]:
parser.print_usage()
Expand Down Expand Up @@ -605,7 +610,8 @@ def main():
'__package__': None,
'__cached__': None,
}
runctx(code, globs, None, options.outfile, options.sort)
runctx(code, globs, None, options.outfile, options.sort,
options.numresults)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent options.numresults under code.

else:
parser.print_usage()
return parser
Expand Down
7 changes: 5 additions & 2 deletions Lib/pstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,10 @@ def compare (self, left, right):

def func_strip_path(func_name):
filename, line, name = func_name
return os.path.basename(filename), line, name
path, base_name = os.path.split(filename)
if base_name.startswith('__'):
base_name = os.path.join(os.path.basename(path), base_name)
return base_name, line, name

def func_get_function_name(func):
return func[2]
Expand All @@ -509,7 +512,7 @@ def func_std_string(func_name): # match what old profile produced
return "%s:%d(%s)" % func_name

#**************************************************************************
# The following functions combine statists for pairs functions.
# The following functions combine statistics for pairs functions.
# The bulk of the processing involves correctly handling "call" lists,
# such as callers and callees.
#**************************************************************************
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Better cProfile CLI defaults. Add option -n to restrict to top n lines (defaults to 20 for CLI); set default of -s to sort by time (instead of unsorted).