summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNicholas Bastin <nick.bastin@gmail.com>2004-03-23 18:44:39 (GMT)
committerNicholas Bastin <nick.bastin@gmail.com>2004-03-23 18:44:39 (GMT)
commit824b1b2da8238b7b71a6135b727ea68d7bc8c5d2 (patch)
tree6c4f481bc7a3dc1cd2ca91dfcb3bf82ee3ad8363
parent6fca7cc783bfd5696ca3b47022a62fa10b9ab89b (diff)
downloadcpython-824b1b2da8238b7b71a6135b727ea68d7bc8c5d2.zip
cpython-824b1b2da8238b7b71a6135b727ea68d7bc8c5d2.tar.gz
cpython-824b1b2da8238b7b71a6135b727ea68d7bc8c5d2.tar.bz2
Added command line options for profile.py - one for stats output file
and one for sort order when using stdout. Uses optparse.
-rw-r--r--Doc/lib/libprofile.tex9
-rwxr-xr-xLib/profile.py40
-rw-r--r--Misc/NEWS3
3 files changed, 39 insertions, 13 deletions
diff --git a/Doc/lib/libprofile.tex b/Doc/lib/libprofile.tex
index 608e1cd..fa0f6b3 100644
--- a/Doc/lib/libprofile.tex
+++ b/Doc/lib/libprofile.tex
@@ -131,6 +131,15 @@ a script to profile another script. For example:
python /usr/local/lib/python1.5/profile.py myscript.py
\end{verbatim}
+\file{profile.py} accepts two optional arguments on the command line:
+
+\begin{verbatim}
+profile.py [-o output_file] [-s sort_order]
+\end{verbatim}
+
+\samp{-s} only applies to stdout (i.e. \samp{-o} is not supplied.
+Look in the \class{Stats} documentation for valid sort values.
+
When you wish to review the profile, you should use the methods in the
\module{pstats} module. Typically you would load the statistics data as
follows:
diff --git a/Lib/profile.py b/Lib/profile.py
index 1a4ff67..2db70b7 100755
--- a/Lib/profile.py
+++ b/Lib/profile.py
@@ -39,6 +39,7 @@ import sys
import os
import time
import marshal
+from optparse import OptionParser
__all__ = ["run","help","Profile"]
@@ -55,7 +56,7 @@ __all__ = ["run","help","Profile"]
# Note that an instance of Profile() is *not* needed to call them.
#**************************************************************************
-def run(statement, filename=None):
+def run(statement, filename=None, sort=-1):
"""Run statement under profiler optionally saving results in filename
This function takes a single argument that can be passed to the
@@ -74,7 +75,7 @@ def run(statement, filename=None):
if filename is not None:
prof.dump_stats(filename)
else:
- return prof.print_stats()
+ return prof.print_stats(sort)
def runctx(statement, globals, locals, filename=None):
"""Run statement under profiler, supplying your own globals and locals,
@@ -384,9 +385,9 @@ class Profile:
self.t = get_time() - t
- def print_stats(self):
+ def print_stats(self, sort=-1):
import pstats
- pstats.Stats(self).strip_dirs().sort_stats(-1). \
+ pstats.Stats(self).strip_dirs().sort_stats(sort). \
print_stats()
def dump_stats(self, file):
@@ -556,15 +557,28 @@ def Stats(*args):
# When invoked as main program, invoke the profiler on a script
if __name__ == '__main__':
+ usage = "profile.py [-o output_file_path] [-s sort] scriptfile [arg] ..."
if not sys.argv[1:]:
- print "usage: profile.py scriptfile [arg] ..."
+ print "Usage: ", usage
sys.exit(2)
- filename = sys.argv[1] # Get script filename
-
- del sys.argv[0] # Hide "profile.py" from argument list
-
- # Insert script directory in front of module search path
- sys.path.insert(0, os.path.dirname(filename))
-
- run('execfile(%r)' % (filename,))
+ class ProfileParser(OptionParser):
+ def __init__(self, usage):
+ OptionParser.__init__(self)
+ self.usage = usage
+
+ parser = ProfileParser(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)
+
+ (options, args) = parser.parse_args()
+ sys.argv[:] = args
+
+ if (len(sys.argv) > 0):
+ sys.path.insert(0, os.path.dirname(sys.argv[0]))
+ run('execfile(%r)' % (sys.argv[0],), options.outfile, options.sort)
+ else:
+ print "Usage: ", usage
diff --git a/Misc/NEWS b/Misc/NEWS
index 50c5249..be6984b 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -290,6 +290,9 @@ Extension modules
Library
-------
+- Added two new command-line arguments for profile (output file and
+ default sort).
+
- Added global runctx function to profile module
- Add hlist missing entryconfigure and entrycget methods.