Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 824b1b2

Browse files
committed
Added command line options for profile.py - one for stats output file
and one for sort order when using stdout. Uses optparse.
1 parent 6fca7cc commit 824b1b2

3 files changed

Lines changed: 39 additions & 13 deletions

File tree

Doc/lib/libprofile.tex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,15 @@ \section{Instant Users Manual \label{profile-instant}}
131131
python /usr/local/lib/python1.5/profile.py myscript.py
132132
\end{verbatim}
133133

134+
\file{profile.py} accepts two optional arguments on the command line:
135+
136+
\begin{verbatim}
137+
profile.py [-o output_file] [-s sort_order]
138+
\end{verbatim}
139+
140+
\samp{-s} only applies to stdout (i.e. \samp{-o} is not supplied.
141+
Look in the \class{Stats} documentation for valid sort values.
142+
134143
When you wish to review the profile, you should use the methods in the
135144
\module{pstats} module. Typically you would load the statistics data as
136145
follows:

Lib/profile.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import os
4040
import time
4141
import marshal
42+
from optparse import OptionParser
4243

4344
__all__ = ["run","help","Profile"]
4445

@@ -55,7 +56,7 @@
5556
# Note that an instance of Profile() is *not* needed to call them.
5657
#**************************************************************************
5758

58-
def run(statement, filename=None):
59+
def run(statement, filename=None, sort=-1):
5960
"""Run statement under profiler optionally saving results in filename
6061
6162
This function takes a single argument that can be passed to the
@@ -74,7 +75,7 @@ def run(statement, filename=None):
7475
if filename is not None:
7576
prof.dump_stats(filename)
7677
else:
77-
return prof.print_stats()
78+
return prof.print_stats(sort)
7879

7980
def runctx(statement, globals, locals, filename=None):
8081
"""Run statement under profiler, supplying your own globals and locals,
@@ -384,9 +385,9 @@ def simulate_cmd_complete(self):
384385
self.t = get_time() - t
385386

386387

387-
def print_stats(self):
388+
def print_stats(self, sort=-1):
388389
import pstats
389-
pstats.Stats(self).strip_dirs().sort_stats(-1). \
390+
pstats.Stats(self).strip_dirs().sort_stats(sort). \
390391
print_stats()
391392

392393
def dump_stats(self, file):
@@ -556,15 +557,28 @@ def Stats(*args):
556557

557558
# When invoked as main program, invoke the profiler on a script
558559
if __name__ == '__main__':
560+
usage = "profile.py [-o output_file_path] [-s sort] scriptfile [arg] ..."
559561
if not sys.argv[1:]:
560-
print "usage: profile.py scriptfile [arg] ..."
562+
print "Usage: ", usage
561563
sys.exit(2)
562564

563-
filename = sys.argv[1] # Get script filename
564-
565-
del sys.argv[0] # Hide "profile.py" from argument list
566-
567-
# Insert script directory in front of module search path
568-
sys.path.insert(0, os.path.dirname(filename))
569-
570-
run('execfile(%r)' % (filename,))
565+
class ProfileParser(OptionParser):
566+
def __init__(self, usage):
567+
OptionParser.__init__(self)
568+
self.usage = usage
569+
570+
parser = ProfileParser(usage)
571+
parser.allow_interspersed_args = False
572+
parser.add_option('-o', '--outfile', dest="outfile",
573+
help="Save stats to <outfile>", default=None)
574+
parser.add_option('-s', '--sort', dest="sort",
575+
help="Sort order when printing to stdout, based on pstats.Stats class", default=-1)
576+
577+
(options, args) = parser.parse_args()
578+
sys.argv[:] = args
579+
580+
if (len(sys.argv) > 0):
581+
sys.path.insert(0, os.path.dirname(sys.argv[0]))
582+
run('execfile(%r)' % (sys.argv[0],), options.outfile, options.sort)
583+
else:
584+
print "Usage: ", usage

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@ Extension modules
290290
Library
291291
-------
292292

293+
- Added two new command-line arguments for profile (output file and
294+
default sort).
295+
293296
- Added global runctx function to profile module
294297

295298
- Add hlist missing entryconfigure and entrycget methods.

0 commit comments

Comments
 (0)