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

Skip to content

Commit 4f3980d

Browse files
Added a test main to the pstats library that can help you browse profile dumps.
1 parent f4e5bd9 commit 4f3980d

2 files changed

Lines changed: 131 additions & 0 deletions

File tree

Doc/lib/libprofile.tex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ \section{Instant Users Manual \label{profile-instant}}
220220
p.add('fooprof')
221221
\end{verbatim}
222222

223+
Invoked as a script, the \module{pstats} module is a statistics
224+
browser for reading and examining profile dumps. It has a simple
225+
line-oriented interface (implemented using \module{cmd}) and
226+
interactive help.
227+
223228
\section{What Is Deterministic Profiling?}
224229
\nodename{Deterministic Profiling}
225230

Lib/pstats.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,3 +524,129 @@ def count_calls(callers):
524524

525525
def f8(x):
526526
return fpformat.fix(x, 3).rjust(8)
527+
528+
#**************************************************************************
529+
# Statistics browser added by ESR, April 2001
530+
#**************************************************************************
531+
532+
if __name__ == '__main__':
533+
import cmd
534+
535+
class ProfileBrowser(cmd.Cmd):
536+
def __init__(self, profile=None):
537+
self.prompt = "% "
538+
if profile:
539+
self.stats = Stats(profile)
540+
else:
541+
self.stats = None
542+
543+
def generic(self, fn, line):
544+
args = line.split()
545+
processed = []
546+
for term in args:
547+
try:
548+
processed.append(int(term))
549+
continue
550+
except ValueError:
551+
pass
552+
try:
553+
frac = float(term)
554+
if frac > 1 or frac < 0:
555+
print "Fraction argument mus be in [0, 1]"
556+
continue
557+
processed.append(frac)
558+
continue
559+
except ValueError:
560+
pass
561+
processed.append(term)
562+
if self.stats:
563+
apply(getattr(self.stats, fn), processed)
564+
else:
565+
print "No statistics object is loaded."
566+
return 0
567+
568+
def do_add(self, line):
569+
self.stats.add(line)
570+
return 0
571+
def help_add(self):
572+
print "Add profile info from given file to current stastics object."
573+
574+
def do_callees(self, line):
575+
return self.generic('callees', line)
576+
def help_callees(self):
577+
print "Print callees statistics from the current stat object."
578+
579+
def do_callers(self, line):
580+
return self.generic('callers', line)
581+
def help_callers(self):
582+
print "Print callers statistics from the current stat object."
583+
584+
def do_EOF(self, line):
585+
print ""
586+
return 1
587+
def help_EOF(self):
588+
print "Leave the profile brower."
589+
590+
def do_quit(self, line):
591+
return 1
592+
def help_quit(self):
593+
print "Leave the profile brower."
594+
595+
def do_read(self, line):
596+
if line:
597+
try:
598+
self.stats = Stats(line)
599+
except IOError, args:
600+
print args[1]
601+
return
602+
self.prompt = line + "% "
603+
elif len(self.prompt > 2):
604+
line = self.prompt[-2:]
605+
else:
606+
print "No statistics object is current -- cannot reload."
607+
return 0
608+
def help_read(self):
609+
print "Read in profile data from a specified file."
610+
611+
def do_reverse(self, line):
612+
self.stats.reverse_order()
613+
return 0
614+
def help_reverse(self):
615+
print "Reverse the sort order of the profiling report."
616+
617+
def do_sort(self, line):
618+
apply(self.stats.sort_stats, line.split())
619+
return 0
620+
def help_sort(self):
621+
print "Sort profile data according to specified keys."
622+
623+
def do_stats(self, line):
624+
return self.generic('print_stats', line)
625+
def help_stats(self):
626+
print "Print statistics from the current stat object."
627+
628+
def do_strip(self, line):
629+
self.stats.strip_order()
630+
return 0
631+
def help_strip(self):
632+
print "Strip leading path information from filenames in the report."
633+
634+
def postcmd(self, stop, line):
635+
if stop:
636+
return stop
637+
return None
638+
639+
import sys
640+
print "Welcome to the profile statistics browser."
641+
if len(sys.argv) > 1:
642+
initprofile = sys.argv[1]
643+
else:
644+
initprofile = None
645+
try:
646+
ProfileBrowser(initprofile).cmdloop()
647+
print "Goodbye."
648+
except KeyboardInterrupt:
649+
pass
650+
651+
# That's all, folks.
652+

0 commit comments

Comments
 (0)