annotate Lib/pstats.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents 1803de5ae0dc
children 61ad81a774c5 442e170b56c3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10581
diff changeset
1 """Class for printing reports on profiled python code."""
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10581
diff changeset
2
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # Class for printing reports on profiled python code. rev 1.0 4/1/94
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 #
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # Based on prior profile module by Sjoerd Mullender...
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 # which was hacked somewhat by: Guido van Rossum
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 #
10581
bbb5af78a259 Fix mysterious references to jprofile that were in the source since
Guido van Rossum <guido@python.org>
parents: 7732
diff changeset
8 # see profile.doc and profile.py for more info.
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 # Copyright 1994, by InfoSeek Corporation, all rights reserved.
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 # Written by James Roskind
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
12 #
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 # Permission to use, copy, modify, and distribute this Python software
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 # and its associated documentation for any purpose (subject to the
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 # restriction in the following sentence) without fee is hereby granted,
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 # provided that the above copyright notice appears in all copies, and
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 # that both that copyright notice and this permission notice appear in
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 # supporting documentation, and that the name of InfoSeek not be used in
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 # advertising or publicity pertaining to distribution of the software
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # without specific, written prior permission. This permission is
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 # explicitly restricted to the copying and modification of the software
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 # to remain in Python, compiled Python, or other languages (such as C)
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 # wherein the modified or derived code is exclusively imported into a
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 # Python module.
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
25 #
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 # INFOSEEK CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 # SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # FITNESS. IN NO EVENT SHALL INFOSEEK CORPORATION BE LIABLE FOR ANY
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 # SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 # RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 # CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
35 import sys
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 import os
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 import time
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 import marshal
6519
24da035ceb6e Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents: 6452
diff changeset
39 import re
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
16562
623456576d6a __all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents: 16492
diff changeset
41 __all__ = ["Stats"]
623456576d6a __all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents: 16492
diff changeset
42
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 class Stats:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
44 """This class is used for creating reports from data generated by the
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
45 Profile class. It is a "friend" of that class, and imports data either
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
46 by direct access to members of Profile class, or by reading in a dictionary
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
47 that was emitted (via marshal) from the Profile class.
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10581
diff changeset
48
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
49 The big change from the previous Profiler (in terms of raw functionality)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
50 is that an "add()" method has been provided to combine Stats from
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
51 several distinct profile runs. Both the constructor and the add()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
52 method now take arbitrarily many file names as arguments.
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10581
diff changeset
53
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
54 All the print methods now take an argument that indicates how many lines
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
55 to print. If the arg is a floating point number between 0 and 1.0, then
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
56 it is taken as a decimal percentage of the available lines to be printed
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
57 (e.g., .1 means print 10% of all available lines). If it is an integer,
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
58 it is taken to mean the number of lines of data that you wish to have
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
59 printed.
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10581
diff changeset
60
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
61 The sort_stats() method now processes some additional options (i.e., in
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
62 addition to the old -1, 0, 1, or 2). It takes an arbitrary number of
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
63 quoted strings to select the sort order. For example sort_stats('time',
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
64 'name') sorts on the major key of 'internal function time', and on the
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
65 minor key of 'the name of the function'. Look at the two tables in
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
66 sort_stats() and get_sort_arg_defs(self) for more examples.
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
67
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
68 All methods return self, so you can string together commands like:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
69 Stats('foo', 'goo').strip_dirs().sort_stats('calls').\
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
70 print_stats(5).print_callers(5)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
71 """
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10581
diff changeset
72
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
73 def __init__(self, *args, **kwds):
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
74 # I can't figure out how to explictly specify a stream keyword arg
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
75 # with *args:
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
76 # def __init__(self, *args, stream=sys.stdout): ...
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
77 # so I use **kwds and sqauwk if something unexpected is passed in.
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
78 self.stream = sys.stdout
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
79 if "stream" in kwds:
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
80 self.stream = kwds["stream"]
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
81 del kwds["stream"]
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
82 if kwds:
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
83 keys = kwds.keys()
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
84 keys.sort()
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
85 extras = ", ".join(["%s=%s" % (k, kwds[k]) for k in keys])
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
86 raise ValueError, "unrecognized keyword args: %s" % extras
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
87 if not len(args):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
88 arg = None
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
89 else:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
90 arg = args[0]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
91 args = args[1:]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
92 self.init(arg)
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 23634
diff changeset
93 self.add(*args)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
94
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
95 def init(self, arg):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
96 self.all_callees = None # calc only if needed
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
97 self.files = []
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
98 self.fcn_list = None
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
99 self.total_tt = 0
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
100 self.total_calls = 0
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
101 self.prim_calls = 0
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
102 self.max_name_len = 0
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
103 self.top_level = {}
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
104 self.stats = {}
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
105 self.sort_arg_dict = {}
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
106 self.load_stats(arg)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
107 trouble = 1
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
108 try:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
109 self.get_top_level_stats()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
110 trouble = 0
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
111 finally:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
112 if trouble:
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
113 print >> self.stream, "Invalid timing data",
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
114 if self.files: print >> self.stream, self.files[-1],
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
115 print >> self.stream
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
116
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
117 def load_stats(self, arg):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
118 if not arg: self.stats = {}
40397
454eff2706d3 Bug #1603321: make pstats.Stats accept Unicode file paths.
Georg Brandl <georg@python.org>
parents: 38742
diff changeset
119 elif isinstance(arg, basestring):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
120 f = open(arg, 'rb')
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
121 self.stats = marshal.load(f)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
122 f.close()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
123 try:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
124 file_stats = os.stat(arg)
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23622
diff changeset
125 arg = time.ctime(file_stats.st_mtime) + " " + arg
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
126 except: # in case this is not unix
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
127 pass
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
128 self.files = [ arg ]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
129 elif hasattr(arg, 'create_stats'):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
130 arg.create_stats()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
131 self.stats = arg.stats
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
132 arg.stats = {}
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
133 if not self.stats:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29111
diff changeset
134 raise TypeError, "Cannot create or construct a %r object from '%r''" % (
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29111
diff changeset
135 self.__class__, arg)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
136 return
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
137
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
138 def get_top_level_stats(self):
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
139 for func, (cc, nc, tt, ct, callers) in self.stats.items():
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
140 self.total_calls += nc
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
141 self.prim_calls += cc
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
142 self.total_tt += tt
48146
1803de5ae0dc Silence -3 warnings in pstats: a dict.has_key() usage and backport solution to
Brett Cannon <bcannon@gmail.com>
parents: 44833
diff changeset
143 if ("jprofile", 0, "profiler") in callers:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
144 self.top_level[func] = None
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
145 if len(func_std_string(func)) > self.max_name_len:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
146 self.max_name_len = len(func_std_string(func))
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
147
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
148 def add(self, *arg_list):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
149 if not arg_list: return self
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 23634
diff changeset
150 if len(arg_list) > 1: self.add(*arg_list[1:])
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
151 other = arg_list[0]
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
152 if type(self) != type(other) or self.__class__ != other.__class__:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
153 other = Stats(other)
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
154 self.files += other.files
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
155 self.total_calls += other.total_calls
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
156 self.prim_calls += other.prim_calls
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
157 self.total_tt += other.total_tt
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
158 for func in other.top_level:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
159 self.top_level[func] = None
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
160
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
161 if self.max_name_len < other.max_name_len:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
162 self.max_name_len = other.max_name_len
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
163
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
164 self.fcn_list = None
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
165
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
166 for func, stat in other.stats.iteritems():
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20309
diff changeset
167 if func in self.stats:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
168 old_func_stat = self.stats[func]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
169 else:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
170 old_func_stat = (0, 0, 0, 0, {},)
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
171 self.stats[func] = add_func_stats(old_func_stat, stat)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
172 return self
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
173
29111
f4d406acf1ca - add a dump_stats() method similar to that of the profile.Profile class
Fred Drake <fdrake@acm.org>
parents: 28089
diff changeset
174 def dump_stats(self, filename):
f4d406acf1ca - add a dump_stats() method similar to that of the profile.Profile class
Fred Drake <fdrake@acm.org>
parents: 28089
diff changeset
175 """Write the profile data to a file we know how to load back."""
f4d406acf1ca - add a dump_stats() method similar to that of the profile.Profile class
Fred Drake <fdrake@acm.org>
parents: 28089
diff changeset
176 f = file(filename, 'wb')
f4d406acf1ca - add a dump_stats() method similar to that of the profile.Profile class
Fred Drake <fdrake@acm.org>
parents: 28089
diff changeset
177 try:
f4d406acf1ca - add a dump_stats() method similar to that of the profile.Profile class
Fred Drake <fdrake@acm.org>
parents: 28089
diff changeset
178 marshal.dump(self.stats, f)
f4d406acf1ca - add a dump_stats() method similar to that of the profile.Profile class
Fred Drake <fdrake@acm.org>
parents: 28089
diff changeset
179 finally:
f4d406acf1ca - add a dump_stats() method similar to that of the profile.Profile class
Fred Drake <fdrake@acm.org>
parents: 28089
diff changeset
180 f.close()
f4d406acf1ca - add a dump_stats() method similar to that of the profile.Profile class
Fred Drake <fdrake@acm.org>
parents: 28089
diff changeset
181
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
182 # list the tuple indices and directions for sorting,
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
183 # along with some printable description
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
184 sort_arg_dict_default = {
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
185 "calls" : (((1,-1), ), "call count"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
186 "cumulative": (((3,-1), ), "cumulative time"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
187 "file" : (((4, 1), ), "file name"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
188 "line" : (((5, 1), ), "line number"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
189 "module" : (((4, 1), ), "file name"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
190 "name" : (((6, 1), ), "function name"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
191 "nfl" : (((6, 1),(4, 1),(5, 1),), "name/file/line"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
192 "pcalls" : (((0,-1), ), "call count"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
193 "stdname" : (((7, 1), ), "standard name"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
194 "time" : (((2,-1), ), "internal time"),
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
195 }
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
196
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
197 def get_sort_arg_defs(self):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
198 """Expand all abbreviations that are unique."""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
199 if not self.sort_arg_dict:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
200 self.sort_arg_dict = dict = {}
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
201 bad_list = {}
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
202 for word, tup in self.sort_arg_dict_default.iteritems():
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
203 fragment = word
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
204 while fragment:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
205 if not fragment:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
206 break
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20309
diff changeset
207 if fragment in dict:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
208 bad_list[fragment] = 0
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
209 break
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
210 dict[fragment] = tup
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
211 fragment = fragment[:-1]
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
212 for word in bad_list:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
213 del dict[word]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
214 return self.sort_arg_dict
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
215
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
216 def sort_stats(self, *field):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
217 if not field:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
218 self.fcn_list = 0
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
219 return self
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
220 if len(field) == 1 and type(field[0]) == type(1):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
221 # Be compatible with old profiler
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
222 field = [ {-1: "stdname",
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
223 0:"calls",
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
224 1:"time",
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
225 2: "cumulative" } [ field[0] ] ]
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
226
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
227 sort_arg_defs = self.get_sort_arg_defs()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
228 sort_tuple = ()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
229 self.sort_type = ""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
230 connector = ""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
231 for word in field:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
232 sort_tuple = sort_tuple + sort_arg_defs[word][0]
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
233 self.sort_type += connector + sort_arg_defs[word][1]
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
234 connector = ", "
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
235
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
236 stats_list = []
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
237 for func, (cc, nc, tt, ct, callers) in self.stats.iteritems():
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
238 stats_list.append((cc, nc, tt, ct) + func +
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
239 (func_std_string(func), func))
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
240
48146
1803de5ae0dc Silence -3 warnings in pstats: a dict.has_key() usage and backport solution to
Brett Cannon <bcannon@gmail.com>
parents: 44833
diff changeset
241 stats_list.sort(key=CmpToKey(TupleComp(sort_tuple).compare))
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
242
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
243 self.fcn_list = fcn_list = []
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
244 for tuple in stats_list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
245 fcn_list.append(tuple[-1])
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
246 return self
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
247
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
248 def reverse_order(self):
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
249 if self.fcn_list:
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
250 self.fcn_list.reverse()
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
251 return self
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
252
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
253 def strip_dirs(self):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
254 oldstats = self.stats
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
255 self.stats = newstats = {}
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
256 max_name_len = 0
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
257 for func, (cc, nc, tt, ct, callers) in oldstats.iteritems():
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
258 newfunc = func_strip_path(func)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
259 if len(func_std_string(newfunc)) > max_name_len:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
260 max_name_len = len(func_std_string(newfunc))
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
261 newcallers = {}
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
262 for func2, caller in callers.iteritems():
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
263 newcallers[func_strip_path(func2)] = caller
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
264
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20309
diff changeset
265 if newfunc in newstats:
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
266 newstats[newfunc] = add_func_stats(
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
267 newstats[newfunc],
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
268 (cc, nc, tt, ct, newcallers))
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
269 else:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
270 newstats[newfunc] = (cc, nc, tt, ct, newcallers)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
271 old_top = self.top_level
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
272 self.top_level = new_top = {}
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
273 for func in old_top:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
274 new_top[func_strip_path(func)] = None
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
275
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
276 self.max_name_len = max_name_len
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
277
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
278 self.fcn_list = None
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
279 self.all_callees = None
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
280 return self
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
281
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
282 def calc_callees(self):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
283 if self.all_callees: return
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
284 self.all_callees = all_callees = {}
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
285 for func, (cc, nc, tt, ct, callers) in self.stats.iteritems():
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20309
diff changeset
286 if not func in all_callees:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
287 all_callees[func] = {}
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
288 for func2, caller in callers.iteritems():
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20309
diff changeset
289 if not func2 in all_callees:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
290 all_callees[func2] = {}
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
291 all_callees[func2][func] = caller
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
292 return
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
293
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
294 #******************************************************************
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
295 # The following functions support actual printing of reports
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
296 #******************************************************************
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
297
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
298 # Optional "amount" is either a line count, or a percentage of lines.
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
299
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
300 def eval_print_amount(self, sel, list, msg):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
301 new_list = list
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
302 if type(sel) == type(""):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
303 new_list = []
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
304 for func in list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
305 if re.search(sel, func_std_string(func)):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
306 new_list.append(func)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
307 else:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
308 count = len(list)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
309 if type(sel) == type(1.0) and 0.0 <= sel < 1.0:
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
310 count = int(count * sel + .5)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
311 new_list = list[:count]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
312 elif type(sel) == type(1) and 0 <= sel < count:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
313 count = sel
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
314 new_list = list[:count]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
315 if len(list) != len(new_list):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29111
diff changeset
316 msg = msg + " List reduced from %r to %r due to restriction <%r>\n" % (
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29111
diff changeset
317 len(list), len(new_list), sel)
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
318
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
319 return new_list, msg
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
320
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
321 def get_print_list(self, sel_list):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
322 width = self.max_name_len
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
323 if self.fcn_list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
324 list = self.fcn_list[:]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
325 msg = " Ordered by: " + self.sort_type + '\n'
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
326 else:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
327 list = self.stats.keys()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
328 msg = " Random listing order was used\n"
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
329
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
330 for selection in sel_list:
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
331 list, msg = self.eval_print_amount(selection, list, msg)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
332
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
333 count = len(list)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
334
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
335 if not list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
336 return 0, list
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
337 print >> self.stream, msg
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
338 if count < len(self.stats):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
339 width = 0
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
340 for func in list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
341 if len(func_std_string(func)) > width:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
342 width = len(func_std_string(func))
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
343 return width+2, list
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
344
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
345 def print_stats(self, *amount):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
346 for filename in self.files:
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
347 print >> self.stream, filename
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
348 if self.files: print >> self.stream
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
349 indent = ' ' * 8
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
350 for func in self.top_level:
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
351 print >> self.stream, indent, func_get_function_name(func)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
352
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
353 print >> self.stream, indent, self.total_calls, "function calls",
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
354 if self.total_calls != self.prim_calls:
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
355 print >> self.stream, "(%d primitive calls)" % self.prim_calls,
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
356 print >> self.stream, "in %.3f CPU seconds" % self.total_tt
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
357 print >> self.stream
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
358 width, list = self.get_print_list(amount)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
359 if list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
360 self.print_title()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
361 for func in list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
362 self.print_line(func)
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
363 print >> self.stream
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
364 print >> self.stream
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
365 return self
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
366
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
367 def print_callees(self, *amount):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
368 width, list = self.get_print_list(amount)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
369 if list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
370 self.calc_callees()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
371
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
372 self.print_call_heading(width, "called...")
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
373 for func in list:
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20309
diff changeset
374 if func in self.all_callees:
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
375 self.print_call_line(width, func, self.all_callees[func])
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
376 else:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
377 self.print_call_line(width, func, {})
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
378 print >> self.stream
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
379 print >> self.stream
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
380 return self
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
381
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
382 def print_callers(self, *amount):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
383 width, list = self.get_print_list(amount)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
384 if list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
385 self.print_call_heading(width, "was called by...")
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
386 for func in list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
387 cc, nc, tt, ct, callers = self.stats[func]
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
388 self.print_call_line(width, func, callers, "<-")
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
389 print >> self.stream
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
390 print >> self.stream
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
391 return self
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
392
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
393 def print_call_heading(self, name_size, column_title):
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
394 print >> self.stream, "Function ".ljust(name_size) + column_title
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
395 # print sub-header only if we have new-style callers
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
396 subheader = False
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
397 for cc, nc, tt, ct, callers in self.stats.itervalues():
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
398 if callers:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
399 value = callers.itervalues().next()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
400 subheader = isinstance(value, tuple)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
401 break
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
402 if subheader:
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
403 print >> self.stream, " "*name_size + " ncalls tottime cumtime"
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
404
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
405 def print_call_line(self, name_size, source, call_dict, arrow="->"):
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
406 print >> self.stream, func_std_string(source).ljust(name_size) + arrow,
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
407 if not call_dict:
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
408 print >> self.stream
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
409 return
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
410 clist = call_dict.keys()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
411 clist.sort()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
412 indent = ""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
413 for func in clist:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
414 name = func_std_string(func)
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
415 value = call_dict[func]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
416 if isinstance(value, tuple):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
417 nc, cc, tt, ct = value
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
418 if nc != cc:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
419 substats = '%d/%d' % (nc, cc)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
420 else:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
421 substats = '%d' % (nc,)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
422 substats = '%s %s %s %s' % (substats.rjust(7+2*len(indent)),
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
423 f8(tt), f8(ct), name)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
424 left_width = name_size + 1
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
425 else:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
426 substats = '%s(%r) %s' % (name, value, f8(self.stats[func][3]))
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
427 left_width = name_size + 3
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
428 print >> self.stream, indent*left_width + substats
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
429 indent = " "
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
430
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
431 def print_title(self):
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
432 print >> self.stream, ' ncalls tottime percall cumtime percall',
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
433 print >> self.stream, 'filename:lineno(function)'
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
434
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
435 def print_line(self, func): # hack : should print percentages
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
436 cc, nc, tt, ct, callers = self.stats[func]
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
437 c = str(nc)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
438 if nc != cc:
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
439 c = c + '/' + str(cc)
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
440 print >> self.stream, c.rjust(9),
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
441 print >> self.stream, f8(tt),
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
442 if nc == 0:
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
443 print >> self.stream, ' '*8,
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
444 else:
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
445 print >> self.stream, f8(tt/nc),
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
446 print >> self.stream, f8(ct),
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
447 if cc == 0:
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
448 print >> self.stream, ' '*8,
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
449 else:
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
450 print >> self.stream, f8(ct/cc),
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
451 print >> self.stream, func_std_string(func)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
452
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 class TupleComp:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
454 """This class provides a generic function for comparing any two tuples.
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
455 Each instance records a list of tuple-indices (from most significant
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
456 to least significant), and sort direction (ascending or decending) for
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
457 each tuple-index. The compare functions can then be used as the function
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
458 argument to the system sort() function when a list of tuples need to be
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
459 sorted in the instances order."""
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10581
diff changeset
460
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
461 def __init__(self, comp_select_list):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
462 self.comp_select_list = comp_select_list
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
463
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
464 def compare (self, left, right):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
465 for index, direction in self.comp_select_list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
466 l = left[index]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
467 r = right[index]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
468 if l < r:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
469 return -direction
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
470 if l > r:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
471 return direction
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
472 return 0
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
473
48146
1803de5ae0dc Silence -3 warnings in pstats: a dict.has_key() usage and backport solution to
Brett Cannon <bcannon@gmail.com>
parents: 44833
diff changeset
474 def CmpToKey(mycmp):
1803de5ae0dc Silence -3 warnings in pstats: a dict.has_key() usage and backport solution to
Brett Cannon <bcannon@gmail.com>
parents: 44833
diff changeset
475 """Convert a cmp= function into a key= function"""
1803de5ae0dc Silence -3 warnings in pstats: a dict.has_key() usage and backport solution to
Brett Cannon <bcannon@gmail.com>
parents: 44833
diff changeset
476 class K(object):
1803de5ae0dc Silence -3 warnings in pstats: a dict.has_key() usage and backport solution to
Brett Cannon <bcannon@gmail.com>
parents: 44833
diff changeset
477 def __init__(self, obj):
1803de5ae0dc Silence -3 warnings in pstats: a dict.has_key() usage and backport solution to
Brett Cannon <bcannon@gmail.com>
parents: 44833
diff changeset
478 self.obj = obj
1803de5ae0dc Silence -3 warnings in pstats: a dict.has_key() usage and backport solution to
Brett Cannon <bcannon@gmail.com>
parents: 44833
diff changeset
479 def __lt__(self, other):
1803de5ae0dc Silence -3 warnings in pstats: a dict.has_key() usage and backport solution to
Brett Cannon <bcannon@gmail.com>
parents: 44833
diff changeset
480 return mycmp(self.obj, other.obj) == -1
1803de5ae0dc Silence -3 warnings in pstats: a dict.has_key() usage and backport solution to
Brett Cannon <bcannon@gmail.com>
parents: 44833
diff changeset
481 return K
1803de5ae0dc Silence -3 warnings in pstats: a dict.has_key() usage and backport solution to
Brett Cannon <bcannon@gmail.com>
parents: 44833
diff changeset
482
1803de5ae0dc Silence -3 warnings in pstats: a dict.has_key() usage and backport solution to
Brett Cannon <bcannon@gmail.com>
parents: 44833
diff changeset
483
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
484 #**************************************************************************
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
485 # func_name is a triple (file:string, line:int, name:string)
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
486
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
487 def func_strip_path(func_name):
29111
f4d406acf1ca - add a dump_stats() method similar to that of the profile.Profile class
Fred Drake <fdrake@acm.org>
parents: 28089
diff changeset
488 filename, line, name = func_name
f4d406acf1ca - add a dump_stats() method similar to that of the profile.Profile class
Fred Drake <fdrake@acm.org>
parents: 28089
diff changeset
489 return os.path.basename(filename), line, name
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
490
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
491 def func_get_function_name(func):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
492 return func[2]
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
493
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
494 def func_std_string(func_name): # match what old profile produced
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
495 if func_name[:2] == ('~', 0):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
496 # special case for built-in functions
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
497 name = func_name[2]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
498 if name.startswith('<') and name.endswith('>'):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
499 return '{%s}' % name[1:-1]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
500 else:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
501 return name
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
502 else:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
503 return "%s:%d(%s)" % func_name
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
504
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
505 #**************************************************************************
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
506 # The following functions combine statists for pairs functions.
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
507 # The bulk of the processing involves correctly handling "call" lists,
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
508 # such as callers and callees.
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
509 #**************************************************************************
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
510
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10581
diff changeset
511 def add_func_stats(target, source):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
512 """Add together all the stats for two profile entries."""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
513 cc, nc, tt, ct, callers = source
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
514 t_cc, t_nc, t_tt, t_ct, t_callers = target
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
515 return (cc+t_cc, nc+t_nc, tt+t_tt, ct+t_ct,
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
516 add_callers(t_callers, callers))
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
517
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
518 def add_callers(target, source):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
519 """Combine two caller lists in a single list."""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
520 new_callers = {}
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
521 for func, caller in target.iteritems():
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
522 new_callers[func] = caller
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
523 for func, caller in source.iteritems():
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20309
diff changeset
524 if func in new_callers:
44833
89b13b693864 #1269: fix a bug in pstats.add_callers() and add a unit test file for pstats.
Georg Brandl <georg@python.org>
parents: 40397
diff changeset
525 new_callers[func] = tuple([i[0] + i[1] for i in
89b13b693864 #1269: fix a bug in pstats.add_callers() and add a unit test file for pstats.
Georg Brandl <georg@python.org>
parents: 40397
diff changeset
526 zip(caller, new_callers[func])])
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
527 else:
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
528 new_callers[func] = caller
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
529 return new_callers
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
530
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
531 def count_calls(callers):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
532 """Sum the caller statistics to get total number of calls received."""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
533 nc = 0
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
534 for calls in callers.itervalues():
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
535 nc += calls
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
536 return nc
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
537
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
538 #**************************************************************************
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
539 # The following functions support printing of reports
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
540 #**************************************************************************
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
541
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
542 def f8(x):
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
543 return "%8.3f" % x
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
544
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
545 #**************************************************************************
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
546 # Statistics browser added by ESR, April 2001
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
547 #**************************************************************************
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
548
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
549 if __name__ == '__main__':
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
550 import cmd
17510
fd0683cb7301 Import readline when possible to make the commaninterpreter UI nicer.
Eric S. Raymond <esr@thyrsus.com>
parents: 17467
diff changeset
551 try:
fd0683cb7301 Import readline when possible to make the commaninterpreter UI nicer.
Eric S. Raymond <esr@thyrsus.com>
parents: 17467
diff changeset
552 import readline
17800
c19980c03e2d When guarding an import, only catch ImportError.
Fred Drake <fdrake@acm.org>
parents: 17634
diff changeset
553 except ImportError:
17510
fd0683cb7301 Import readline when possible to make the commaninterpreter UI nicer.
Eric S. Raymond <esr@thyrsus.com>
parents: 17467
diff changeset
554 pass
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
555
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
556 class ProfileBrowser(cmd.Cmd):
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
557 def __init__(self, profile=None):
18855
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18048
diff changeset
558 cmd.Cmd.__init__(self)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
559 self.prompt = "% "
23622
5edd80726d96 Replace boolean test with is None.
Raymond Hettinger <python@rcn.com>
parents: 23620
diff changeset
560 if profile is not None:
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
561 self.stats = Stats(profile)
38742
49026af9d1d7 Fix errors found by pychecker.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37873
diff changeset
562 self.stream = self.stats.stream
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
563 else:
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
564 self.stats = None
38742
49026af9d1d7 Fix errors found by pychecker.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37873
diff changeset
565 self.stream = sys.stdout
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
566
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
567 def generic(self, fn, line):
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
568 args = line.split()
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
569 processed = []
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
570 for term in args:
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
571 try:
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
572 processed.append(int(term))
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
573 continue
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
574 except ValueError:
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
575 pass
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
576 try:
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
577 frac = float(term)
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
578 if frac > 1 or frac < 0:
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
579 print >> self.stream, "Fraction argument must be in [0, 1]"
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
580 continue
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
581 processed.append(frac)
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
582 continue
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
583 except ValueError:
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
584 pass
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
585 processed.append(term)
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
586 if self.stats:
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 23634
diff changeset
587 getattr(self.stats, fn)(*processed)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
588 else:
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
589 print >> self.stream, "No statistics object is loaded."
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
590 return 0
17634
0bc3377d0d72 Added more help, and recovery from misspelled sort key arguments.
Eric S. Raymond <esr@thyrsus.com>
parents: 17514
diff changeset
591 def generic_help(self):
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
592 print >> self.stream, "Arguments may be:"
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
593 print >> self.stream, "* An integer maximum number of entries to print."
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
594 print >> self.stream, "* A decimal fractional number between 0 and 1, controlling"
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
595 print >> self.stream, " what fraction of selected entries to print."
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
596 print >> self.stream, "* A regular expression; only entries with function names"
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
597 print >> self.stream, " that match it are printed."
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
598
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
599 def do_add(self, line):
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
600 self.stats.add(line)
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
601 return 0
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
602 def help_add(self):
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
603 print >> self.stream, "Add profile info from given file to current statistics object."
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
604
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
605 def do_callees(self, line):
17514
773c64d38ac6 Should resolve [ #416039 ] pstats browser crashes.
Eric S. Raymond <esr@thyrsus.com>
parents: 17510
diff changeset
606 return self.generic('print_callees', line)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
607 def help_callees(self):
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
608 print >> self.stream, "Print callees statistics from the current stat object."
17634
0bc3377d0d72 Added more help, and recovery from misspelled sort key arguments.
Eric S. Raymond <esr@thyrsus.com>
parents: 17514
diff changeset
609 self.generic_help()
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
610
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
611 def do_callers(self, line):
17514
773c64d38ac6 Should resolve [ #416039 ] pstats browser crashes.
Eric S. Raymond <esr@thyrsus.com>
parents: 17510
diff changeset
612 return self.generic('print_callers', line)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
613 def help_callers(self):
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
614 print >> self.stream, "Print callers statistics from the current stat object."
17634
0bc3377d0d72 Added more help, and recovery from misspelled sort key arguments.
Eric S. Raymond <esr@thyrsus.com>
parents: 17514
diff changeset
615 self.generic_help()
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
616
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
617 def do_EOF(self, line):
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
618 print >> self.stream, ""
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
619 return 1
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
620 def help_EOF(self):
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
621 print >> self.stream, "Leave the profile brower."
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
622
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
623 def do_quit(self, line):
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
624 return 1
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
625 def help_quit(self):
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
626 print >> self.stream, "Leave the profile brower."
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
627
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
628 def do_read(self, line):
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
629 if line:
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
630 try:
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
631 self.stats = Stats(line)
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
632 except IOError, args:
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
633 print >> self.stream, args[1]
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
634 return
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
635 self.prompt = line + "% "
18048
14dc75a5600a Patch #416220: Fix misplaced paren.
Martin v. Löwis <martin@v.loewis.de>
parents: 17800
diff changeset
636 elif len(self.prompt) > 2:
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
637 line = self.prompt[-2:]
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
638 else:
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
639 print >> self.stream, "No statistics object is current -- cannot reload."
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
640 return 0
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
641 def help_read(self):
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
642 print >> self.stream, "Read in profile data from a specified file."
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
643
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
644 def do_reverse(self, line):
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
645 self.stats.reverse_order()
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
646 return 0
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
647 def help_reverse(self):
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
648 print >> self.stream, "Reverse the sort order of the profiling report."
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
649
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
650 def do_sort(self, line):
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
651 abbrevs = self.stats.get_sort_arg_defs()
17634
0bc3377d0d72 Added more help, and recovery from misspelled sort key arguments.
Eric S. Raymond <esr@thyrsus.com>
parents: 17514
diff changeset
652 if line and not filter(lambda x,a=abbrevs: x not in a,line.split()):
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 23634
diff changeset
653 self.stats.sort_stats(*line.split())
17634
0bc3377d0d72 Added more help, and recovery from misspelled sort key arguments.
Eric S. Raymond <esr@thyrsus.com>
parents: 17514
diff changeset
654 else:
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
655 print >> self.stream, "Valid sort keys (unique prefixes are accepted):"
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
656 for (key, value) in Stats.sort_arg_dict_default.iteritems():
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
657 print >> self.stream, "%s -- %s" % (key, value[1])
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
658 return 0
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
659 def help_sort(self):
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
660 print >> self.stream, "Sort profile data according to specified keys."
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
661 print >> self.stream, "(Typing `sort' without arguments lists valid keys.)"
18877
0e212e88d33d Patch #445538: add completion for pstats.py sort cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18855
diff changeset
662 def complete_sort(self, text, *args):
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
663 return [a for a in Stats.sort_arg_dict_default if a.startswith(text)]
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
664
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
665 def do_stats(self, line):
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
666 return self.generic('print_stats', line)
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
667 def help_stats(self):
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
668 print >> self.stream, "Print statistics from the current stat object."
17634
0bc3377d0d72 Added more help, and recovery from misspelled sort key arguments.
Eric S. Raymond <esr@thyrsus.com>
parents: 17514
diff changeset
669 self.generic_help()
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
670
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
671 def do_strip(self, line):
17514
773c64d38ac6 Should resolve [ #416039 ] pstats browser crashes.
Eric S. Raymond <esr@thyrsus.com>
parents: 17510
diff changeset
672 self.stats.strip_dirs()
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
673 return 0
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
674 def help_strip(self):
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
675 print >> self.stream, "Strip leading path information from filenames in the report."
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
676
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
677 def postcmd(self, stop, line):
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
678 if stop:
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
679 return stop
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
680 return None
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
681
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
682 import sys
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
683 if len(sys.argv) > 1:
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
684 initprofile = sys.argv[1]
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
685 else:
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
686 initprofile = None
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
687 try:
38742
49026af9d1d7 Fix errors found by pychecker.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37873
diff changeset
688 browser = ProfileBrowser(initprofile)
49026af9d1d7 Fix errors found by pychecker.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37873
diff changeset
689 print >> browser.stream, "Welcome to the profile statistics browser."
49026af9d1d7 Fix errors found by pychecker.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37873
diff changeset
690 browser.cmdloop()
49026af9d1d7 Fix errors found by pychecker.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37873
diff changeset
691 print >> browser.stream, "Goodbye."
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
692 except KeyboardInterrupt:
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
693 pass
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
694
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
695 # That's all, folks.