annotate Lib/pstats.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 69b310f7acfa
children ad1cc596bedb
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
71039
02150e60636b update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 63551
diff changeset
3 # Written by James Roskind
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 # Based on prior profile module by Sjoerd Mullender...
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # which was hacked somewhat by: Guido van Rossum
71039
02150e60636b update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 63551
diff changeset
6
02150e60636b update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 63551
diff changeset
7 # Copyright Disney Enterprises, Inc. All Rights Reserved.
02150e60636b update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 63551
diff changeset
8 # Licensed to PSF under a Contributor Agreement
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 #
71039
02150e60636b update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 63551
diff changeset
10 # Licensed under the Apache License, Version 2.0 (the "License");
02150e60636b update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 63551
diff changeset
11 # you may not use this file except in compliance with the License.
02150e60636b update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 63551
diff changeset
12 # You may obtain a copy of the License at
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
13 #
71039
02150e60636b update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 63551
diff changeset
14 # http://www.apache.org/licenses/LICENSE-2.0
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
15 #
71039
02150e60636b update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 63551
diff changeset
16 # Unless required by applicable law or agreed to in writing, software
02150e60636b update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 63551
diff changeset
17 # distributed under the License is distributed on an "AS IS" BASIS,
02150e60636b update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 63551
diff changeset
18 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
02150e60636b update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 63551
diff changeset
19 # either express or implied. See the License for the specific language
02150e60636b update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 63551
diff changeset
20 # governing permissions and limitations under the License.
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
23 import sys
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 import os
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 import time
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 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
27 import re
60315
5dda3f32a2ce Add tests for cmp_to_key.
Raymond Hettinger <python@rcn.com>
parents: 60167
diff changeset
28 from functools import cmp_to_key
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
16562
623456576d6a __all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents: 16492
diff changeset
30 __all__ = ["Stats"]
623456576d6a __all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents: 16492
diff changeset
31
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 class Stats:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
33 """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
34 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
35 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
36 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
37
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
38 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
39 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
40 several distinct profile runs. Both the constructor and the add()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
41 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
42
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
43 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
44 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
45 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
46 (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
47 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
48 printed.
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10581
diff changeset
49
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
50 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
51 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
52 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
53 '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
54 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
55 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
56
63602
e971c104a5c2 Merged revisions 83523 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63549
diff changeset
57 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
58 Stats('foo', 'goo').strip_dirs().sort_stats('calls').\
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
59 print_stats(5).print_callers(5)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
60 """
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10581
diff changeset
61
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
62 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
63 # 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
64 # with *args:
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
65 # 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
66 # 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
67 self.stream = sys.stdout
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
68 if "stream" in kwds:
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
69 self.stream = kwds["stream"]
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
70 del kwds["stream"]
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
71 if kwds:
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
72 keys = kwds.keys()
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
73 keys.sort()
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
74 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
75 raise ValueError, "unrecognized keyword args: %s" % extras
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
76 if not len(args):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
77 arg = None
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
78 else:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
79 arg = args[0]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
80 args = args[1:]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
81 self.init(arg)
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 23634
diff changeset
82 self.add(*args)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
83
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
84 def init(self, arg):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
85 self.all_callees = None # calc only if needed
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
86 self.files = []
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
87 self.fcn_list = None
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
88 self.total_tt = 0
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
89 self.total_calls = 0
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
90 self.prim_calls = 0
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
91 self.max_name_len = 0
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
92 self.top_level = {}
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
93 self.stats = {}
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
94 self.sort_arg_dict = {}
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
95 self.load_stats(arg)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
96 trouble = 1
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
97 try:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
98 self.get_top_level_stats()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
99 trouble = 0
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
100 finally:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
101 if trouble:
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
102 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
103 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
104 print >> self.stream
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
105
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
106 def load_stats(self, arg):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
107 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
108 elif isinstance(arg, basestring):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
109 f = open(arg, 'rb')
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
110 self.stats = marshal.load(f)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
111 f.close()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
112 try:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
113 file_stats = os.stat(arg)
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23622
diff changeset
114 arg = time.ctime(file_stats.st_mtime) + " " + arg
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
115 except: # in case this is not unix
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
116 pass
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
117 self.files = [ arg ]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
118 elif hasattr(arg, 'create_stats'):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
119 arg.create_stats()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
120 self.stats = arg.stats
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
121 arg.stats = {}
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
122 if not self.stats:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29111
diff changeset
123 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
124 self.__class__, arg)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
125 return
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
126
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
127 def get_top_level_stats(self):
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
128 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
129 self.total_calls += nc
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
130 self.prim_calls += cc
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
131 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
132 if ("jprofile", 0, "profiler") in callers:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
133 self.top_level[func] = None
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
134 if len(func_std_string(func)) > self.max_name_len:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
135 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
136
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
137 def add(self, *arg_list):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
138 if not arg_list: return self
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 23634
diff changeset
139 if len(arg_list) > 1: self.add(*arg_list[1:])
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
140 other = arg_list[0]
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
141 if type(self) != type(other) or self.__class__ != other.__class__:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
142 other = Stats(other)
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
143 self.files += other.files
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
144 self.total_calls += other.total_calls
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
145 self.prim_calls += other.prim_calls
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
146 self.total_tt += other.total_tt
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
147 for func in other.top_level:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
148 self.top_level[func] = None
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
150 if self.max_name_len < other.max_name_len:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
151 self.max_name_len = other.max_name_len
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
152
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
153 self.fcn_list = None
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
154
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
155 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
156 if func in self.stats:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
157 old_func_stat = self.stats[func]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
158 else:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
159 old_func_stat = (0, 0, 0, 0, {},)
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
160 self.stats[func] = add_func_stats(old_func_stat, stat)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
161 return self
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
162
29111
f4d406acf1ca - add a dump_stats() method similar to that of the profile.Profile class
Fred Drake <fdrake@acm.org>
parents: 28089
diff changeset
163 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
164 """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
165 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
166 try:
f4d406acf1ca - add a dump_stats() method similar to that of the profile.Profile class
Fred Drake <fdrake@acm.org>
parents: 28089
diff changeset
167 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
168 finally:
f4d406acf1ca - add a dump_stats() method similar to that of the profile.Profile class
Fred Drake <fdrake@acm.org>
parents: 28089
diff changeset
169 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
170
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
171 # list the tuple indices and directions for sorting,
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
172 # along with some printable description
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
173 sort_arg_dict_default = {
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
174 "calls" : (((1,-1), ), "call count"),
79576
69b310f7acfa Issue #14900: Add aliases for sorting params for pstat to follow column names from pstat output.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 79570
diff changeset
175 "ncalls" : (((1,-1), ), "call count"),
69b310f7acfa Issue #14900: Add aliases for sorting params for pstat to follow column names from pstat output.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 79570
diff changeset
176 "cumtime" : (((3,-1), ), "cumulative time"),
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
177 "cumulative": (((3,-1), ), "cumulative time"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
178 "file" : (((4, 1), ), "file name"),
79576
69b310f7acfa Issue #14900: Add aliases for sorting params for pstat to follow column names from pstat output.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 79570
diff changeset
179 "filename" : (((4, 1), ), "file name"),
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
180 "line" : (((5, 1), ), "line number"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
181 "module" : (((4, 1), ), "file name"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
182 "name" : (((6, 1), ), "function name"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
183 "nfl" : (((6, 1),(4, 1),(5, 1),), "name/file/line"),
79570
cc3e27b7da41 Issue #14900: Distuguish call count and primitive call count in pstat output.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 71580
diff changeset
184 "pcalls" : (((0,-1), ), "primitive call count"),
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
185 "stdname" : (((7, 1), ), "standard name"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
186 "time" : (((2,-1), ), "internal time"),
79576
69b310f7acfa Issue #14900: Add aliases for sorting params for pstat to follow column names from pstat output.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 79570
diff changeset
187 "tottime" : (((2,-1), ), "internal time"),
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
188 }
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
189
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
190 def get_sort_arg_defs(self):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
191 """Expand all abbreviations that are unique."""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
192 if not self.sort_arg_dict:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
193 self.sort_arg_dict = dict = {}
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
194 bad_list = {}
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
195 for word, tup in self.sort_arg_dict_default.iteritems():
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
196 fragment = word
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
197 while fragment:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
198 if not fragment:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
199 break
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20309
diff changeset
200 if fragment in dict:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
201 bad_list[fragment] = 0
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
202 break
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
203 dict[fragment] = tup
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
204 fragment = fragment[:-1]
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
205 for word in bad_list:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
206 del dict[word]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
207 return self.sort_arg_dict
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
208
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
209 def sort_stats(self, *field):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
210 if not field:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
211 self.fcn_list = 0
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
212 return self
63604
6f81d6f9a6d1 Fix merging glitches.
Georg Brandl <georg@python.org>
parents: 63602
diff changeset
213 if len(field) == 1 and isinstance(field[0], (int, long)):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
214 # Be compatible with old profiler
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
215 field = [ {-1: "stdname",
63602
e971c104a5c2 Merged revisions 83523 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63549
diff changeset
216 0: "calls",
e971c104a5c2 Merged revisions 83523 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63549
diff changeset
217 1: "time",
e971c104a5c2 Merged revisions 83523 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63549
diff changeset
218 2: "cumulative"}[field[0]] ]
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
219
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
220 sort_arg_defs = self.get_sort_arg_defs()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
221 sort_tuple = ()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
222 self.sort_type = ""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
223 connector = ""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
224 for word in field:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
225 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
226 self.sort_type += connector + sort_arg_defs[word][1]
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
227 connector = ", "
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
228
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
229 stats_list = []
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
230 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
231 stats_list.append((cc, nc, tt, ct) + func +
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
232 (func_std_string(func), func))
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
233
60315
5dda3f32a2ce Add tests for cmp_to_key.
Raymond Hettinger <python@rcn.com>
parents: 60167
diff changeset
234 stats_list.sort(key=cmp_to_key(TupleComp(sort_tuple).compare))
15870
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 self.fcn_list = fcn_list = []
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
237 for tuple in stats_list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
238 fcn_list.append(tuple[-1])
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
239 return self
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
240
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
241 def reverse_order(self):
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
242 if self.fcn_list:
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
243 self.fcn_list.reverse()
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
244 return self
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
245
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
246 def strip_dirs(self):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
247 oldstats = self.stats
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
248 self.stats = newstats = {}
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
249 max_name_len = 0
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
250 for func, (cc, nc, tt, ct, callers) in oldstats.iteritems():
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
251 newfunc = func_strip_path(func)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
252 if len(func_std_string(newfunc)) > max_name_len:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
253 max_name_len = len(func_std_string(newfunc))
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
254 newcallers = {}
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
255 for func2, caller in callers.iteritems():
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
256 newcallers[func_strip_path(func2)] = caller
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
257
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20309
diff changeset
258 if newfunc in newstats:
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
259 newstats[newfunc] = add_func_stats(
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
260 newstats[newfunc],
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
261 (cc, nc, tt, ct, newcallers))
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
262 else:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
263 newstats[newfunc] = (cc, nc, tt, ct, newcallers)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
264 old_top = self.top_level
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
265 self.top_level = new_top = {}
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
266 for func in old_top:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
267 new_top[func_strip_path(func)] = None
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
268
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
269 self.max_name_len = max_name_len
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
270
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
271 self.fcn_list = None
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
272 self.all_callees = None
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
273 return self
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
274
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
275 def calc_callees(self):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
276 if self.all_callees: return
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
277 self.all_callees = all_callees = {}
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
278 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
279 if not func in all_callees:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
280 all_callees[func] = {}
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
281 for func2, caller in callers.iteritems():
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20309
diff changeset
282 if not func2 in all_callees:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
283 all_callees[func2] = {}
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
284 all_callees[func2][func] = caller
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
285 return
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
286
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
287 #******************************************************************
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
288 # The following functions support actual printing of reports
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
289 #******************************************************************
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
290
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
291 # 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
292
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
293 def eval_print_amount(self, sel, list, msg):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
294 new_list = list
63604
6f81d6f9a6d1 Fix merging glitches.
Georg Brandl <georg@python.org>
parents: 63602
diff changeset
295 if isinstance(sel, basestring):
63602
e971c104a5c2 Merged revisions 83523 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63549
diff changeset
296 try:
e971c104a5c2 Merged revisions 83523 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63549
diff changeset
297 rex = re.compile(sel)
e971c104a5c2 Merged revisions 83523 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63549
diff changeset
298 except re.error:
e971c104a5c2 Merged revisions 83523 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63549
diff changeset
299 msg += " <Invalid regular expression %r>\n" % sel
e971c104a5c2 Merged revisions 83523 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63549
diff changeset
300 return new_list, msg
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
301 new_list = []
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
302 for func in list:
63602
e971c104a5c2 Merged revisions 83523 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63549
diff changeset
303 if rex.search(func_std_string(func)):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
304 new_list.append(func)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
305 else:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
306 count = len(list)
63602
e971c104a5c2 Merged revisions 83523 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63549
diff changeset
307 if isinstance(sel, float) and 0.0 <= sel < 1.0:
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
308 count = int(count * sel + .5)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
309 new_list = list[:count]
63604
6f81d6f9a6d1 Fix merging glitches.
Georg Brandl <georg@python.org>
parents: 63602
diff changeset
310 elif isinstance(sel, (int, long)) and 0 <= sel < count:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
311 count = sel
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
312 new_list = list[:count]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
313 if len(list) != len(new_list):
63602
e971c104a5c2 Merged revisions 83523 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63549
diff changeset
314 msg += " List reduced from %r to %r due to restriction <%r>\n" % (
e971c104a5c2 Merged revisions 83523 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63549
diff changeset
315 len(list), len(new_list), sel)
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
316
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
317 return new_list, msg
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 def get_print_list(self, sel_list):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
320 width = self.max_name_len
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
321 if self.fcn_list:
63602
e971c104a5c2 Merged revisions 83523 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63549
diff changeset
322 stat_list = self.fcn_list[:]
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
323 msg = " Ordered by: " + self.sort_type + '\n'
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
324 else:
63604
6f81d6f9a6d1 Fix merging glitches.
Georg Brandl <georg@python.org>
parents: 63602
diff changeset
325 stat_list = self.stats.keys()
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
326 msg = " Random listing order was used\n"
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
327
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
328 for selection in sel_list:
63602
e971c104a5c2 Merged revisions 83523 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63549
diff changeset
329 stat_list, msg = self.eval_print_amount(selection, stat_list, msg)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
330
63602
e971c104a5c2 Merged revisions 83523 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63549
diff changeset
331 count = len(stat_list)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
332
63602
e971c104a5c2 Merged revisions 83523 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63549
diff changeset
333 if not stat_list:
e971c104a5c2 Merged revisions 83523 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63549
diff changeset
334 return 0, stat_list
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
335 print >> self.stream, msg
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
336 if count < len(self.stats):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
337 width = 0
63602
e971c104a5c2 Merged revisions 83523 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63549
diff changeset
338 for func in stat_list:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
339 if len(func_std_string(func)) > width:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
340 width = len(func_std_string(func))
63602
e971c104a5c2 Merged revisions 83523 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63549
diff changeset
341 return width+2, stat_list
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
342
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
343 def print_stats(self, *amount):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
344 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
345 print >> self.stream, filename
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
346 if self.files: print >> self.stream
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
347 indent = ' ' * 8
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
348 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
349 print >> self.stream, indent, func_get_function_name(func)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
350
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, self.total_calls, "function calls",
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
352 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
353 print >> self.stream, "(%d primitive calls)" % self.prim_calls,
66296
a7c22107aecd Merged revisions 86580 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 63604
diff changeset
354 print >> self.stream, "in %.3f seconds" % self.total_tt
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
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
356 width, list = self.get_print_list(amount)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
357 if list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
358 self.print_title()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
359 for func in list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
360 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
361 print >> self.stream
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
362 print >> self.stream
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
363 return self
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
364
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
365 def print_callees(self, *amount):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
366 width, list = self.get_print_list(amount)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
367 if list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
368 self.calc_callees()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
369
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
370 self.print_call_heading(width, "called...")
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
371 for func in list:
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20309
diff changeset
372 if func in self.all_callees:
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
373 self.print_call_line(width, func, self.all_callees[func])
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
374 else:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
375 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
376 print >> self.stream
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
377 print >> self.stream
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
378 return self
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
379
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
380 def print_callers(self, *amount):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
381 width, list = self.get_print_list(amount)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
382 if list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
383 self.print_call_heading(width, "was called by...")
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
384 for func in list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
385 cc, nc, tt, ct, callers = self.stats[func]
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
386 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
387 print >> self.stream
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
388 print >> self.stream
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
389 return self
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
390
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
391 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
392 print >> self.stream, "Function ".ljust(name_size) + column_title
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
393 # print sub-header only if we have new-style callers
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
394 subheader = False
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
395 for cc, nc, tt, ct, callers in self.stats.itervalues():
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
396 if callers:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
397 value = callers.itervalues().next()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
398 subheader = isinstance(value, tuple)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
399 break
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
400 if subheader:
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
401 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
402
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
403 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
404 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
405 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
406 print >> self.stream
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
407 return
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
408 clist = call_dict.keys()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
409 clist.sort()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
410 indent = ""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
411 for func in clist:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
412 name = func_std_string(func)
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
413 value = call_dict[func]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
414 if isinstance(value, tuple):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
415 nc, cc, tt, ct = value
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
416 if nc != cc:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
417 substats = '%d/%d' % (nc, cc)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
418 else:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
419 substats = '%d' % (nc,)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
420 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
421 f8(tt), f8(ct), name)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
422 left_width = name_size + 1
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
423 else:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
424 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
425 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
426 print >> self.stream, indent*left_width + substats
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
427 indent = " "
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
428
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
429 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
430 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
431 print >> self.stream, 'filename:lineno(function)'
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
432
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
433 def print_line(self, func): # hack : should print percentages
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
434 cc, nc, tt, ct, callers = self.stats[func]
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
435 c = str(nc)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
436 if nc != cc:
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
437 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
438 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
439 print >> self.stream, f8(tt),
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
440 if nc == 0:
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
441 print >> self.stream, ' '*8,
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
442 else:
63528
751c6dd71b5e Merged revisions 77310-77311 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63511
diff changeset
443 print >> self.stream, f8(float(tt)/nc),
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
444 print >> self.stream, f8(ct),
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
445 if cc == 0:
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
446 print >> self.stream, ' '*8,
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
447 else:
63528
751c6dd71b5e Merged revisions 77310-77311 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63511
diff changeset
448 print >> self.stream, f8(float(ct)/cc),
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
449 print >> self.stream, func_std_string(func)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
450
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 class TupleComp:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
452 """This class provides a generic function for comparing any two tuples.
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
453 Each instance records a list of tuple-indices (from most significant
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
454 to least significant), and sort direction (ascending or decending) for
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
455 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
456 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
457 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
458
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
459 def __init__(self, comp_select_list):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
460 self.comp_select_list = comp_select_list
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
461
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
462 def compare (self, left, right):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
463 for index, direction in self.comp_select_list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
464 l = left[index]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
465 r = right[index]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
466 if l < r:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
467 return -direction
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 return 0
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
471
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
472 #**************************************************************************
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
473 # 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
474
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
475 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
476 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
477 return os.path.basename(filename), line, name
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
478
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
479 def func_get_function_name(func):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
480 return func[2]
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
481
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
482 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
483 if func_name[:2] == ('~', 0):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
484 # special case for built-in functions
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
485 name = func_name[2]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
486 if name.startswith('<') and name.endswith('>'):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
487 return '{%s}' % name[1:-1]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
488 else:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
489 return name
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
490 else:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
491 return "%s:%d(%s)" % func_name
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
492
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 # The following functions combine statists for pairs functions.
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
495 # The bulk of the processing involves correctly handling "call" lists,
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
496 # such as callers and callees.
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
497 #**************************************************************************
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
498
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10581
diff changeset
499 def add_func_stats(target, source):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
500 """Add together all the stats for two profile entries."""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
501 cc, nc, tt, ct, callers = source
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
502 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
503 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
504 add_callers(t_callers, callers))
1679
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 def add_callers(target, source):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
507 """Combine two caller lists in a single list."""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
508 new_callers = {}
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
509 for func, caller in target.iteritems():
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
510 new_callers[func] = caller
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
511 for func, caller in source.iteritems():
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20309
diff changeset
512 if func in new_callers:
63551
3be2464c0c39 Merged revisions 83533 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63528
diff changeset
513 if isinstance(caller, tuple):
3be2464c0c39 Merged revisions 83533 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63528
diff changeset
514 # format used by cProfile
3be2464c0c39 Merged revisions 83533 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63528
diff changeset
515 new_callers[func] = tuple([i[0] + i[1] for i in
3be2464c0c39 Merged revisions 83533 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63528
diff changeset
516 zip(caller, new_callers[func])])
3be2464c0c39 Merged revisions 83533 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63528
diff changeset
517 else:
3be2464c0c39 Merged revisions 83533 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63528
diff changeset
518 # format used by profile
3be2464c0c39 Merged revisions 83533 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63528
diff changeset
519 new_callers[func] += caller
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
520 else:
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
521 new_callers[func] = caller
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
522 return new_callers
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
523
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
524 def count_calls(callers):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
525 """Sum the caller statistics to get total number of calls received."""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
526 nc = 0
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
527 for calls in callers.itervalues():
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
528 nc += calls
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
529 return nc
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 #**************************************************************************
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
532 # The following functions support printing of reports
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
533 #**************************************************************************
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
534
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
535 def f8(x):
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
536 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
537
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
538 #**************************************************************************
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
539 # 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
540 #**************************************************************************
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
541
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
542 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
543 import cmd
17510
fd0683cb7301 Import readline when possible to make the commaninterpreter UI nicer.
Eric S. Raymond <esr@thyrsus.com>
parents: 17467
diff changeset
544 try:
fd0683cb7301 Import readline when possible to make the commaninterpreter UI nicer.
Eric S. Raymond <esr@thyrsus.com>
parents: 17467
diff changeset
545 import readline
17800
c19980c03e2d When guarding an import, only catch ImportError.
Fred Drake <fdrake@acm.org>
parents: 17634
diff changeset
546 except ImportError:
17510
fd0683cb7301 Import readline when possible to make the commaninterpreter UI nicer.
Eric S. Raymond <esr@thyrsus.com>
parents: 17467
diff changeset
547 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
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 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
550 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
551 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
552 self.prompt = "% "
63602
e971c104a5c2 Merged revisions 83523 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63549
diff changeset
553 self.stats = None
e971c104a5c2 Merged revisions 83523 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63549
diff changeset
554 self.stream = sys.stdout
23622
5edd80726d96 Replace boolean test with is None.
Raymond Hettinger <python@rcn.com>
parents: 23620
diff changeset
555 if profile is not None:
63602
e971c104a5c2 Merged revisions 83523 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63549
diff changeset
556 self.do_read(profile)
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
557
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
558 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
559 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
560 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
561 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
562 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
563 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
564 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
565 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
566 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
567 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
568 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
569 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
570 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
571 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
572 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
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 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
577 if self.stats:
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 23634
diff changeset
578 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
579 else:
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
580 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
581 return 0
17634
0bc3377d0d72 Added more help, and recovery from misspelled sort key arguments.
Eric S. Raymond <esr@thyrsus.com>
parents: 17514
diff changeset
582 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
583 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
584 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
585 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
586 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
587 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
588 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
589
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 def do_add(self, line):
63511
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48146
diff changeset
591 if self.stats:
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48146
diff changeset
592 self.stats.add(line)
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48146
diff changeset
593 else:
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48146
diff changeset
594 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
595 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
596 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
597 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
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_callees(self, line):
17514
773c64d38ac6 Should resolve [ #416039 ] pstats browser crashes.
Eric S. Raymond <esr@thyrsus.com>
parents: 17510
diff changeset
600 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
601 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
602 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
603 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
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_callers(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_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
607 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
608 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
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_EOF(self, line):
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
612 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
613 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
614 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
615 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
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_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
618 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
619 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
620 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
621
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 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
623 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
624 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
625 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
626 except IOError, args:
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
627 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
628 return
63511
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48146
diff changeset
629 except Exception as err:
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48146
diff changeset
630 print >> self.stream, err.__class__.__name__ + ':', err
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48146
diff changeset
631 return
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
632 self.prompt = line + "% "
18048
14dc75a5600a Patch #416220: Fix misplaced paren.
Martin v. Löwis <martin@v.loewis.de>
parents: 17800
diff changeset
633 elif len(self.prompt) > 2:
63511
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48146
diff changeset
634 line = self.prompt[:-2]
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48146
diff changeset
635 self.do_read(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
636 else:
37873
d35db84ca072 Allow pstats.Stats creator to specify an alternate to stdout.
Skip Montanaro <skip@pobox.com>
parents: 36459
diff changeset
637 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
638 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
639 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
640 print >> self.stream, "Read in profile data from a specified file."
63511
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48146
diff changeset
641 print >> self.stream, "Without argument, reload the current 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
642
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 def do_reverse(self, line):
63511
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48146
diff changeset
644 if self.stats:
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48146
diff changeset
645 self.stats.reverse_order()
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48146
diff changeset
646 else:
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48146
diff changeset
647 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
648 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
649 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
650 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
651
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
652 def do_sort(self, line):
63511
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48146
diff changeset
653 if not self.stats:
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48146
diff changeset
654 print >> self.stream, "No statistics object is loaded."
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48146
diff changeset
655 return
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
656 abbrevs = self.stats.get_sort_arg_defs()
60167
4ac05d1f60c1 #4440: modernize a use of filter(), making it compatible with 3.x
Andrew M. Kuchling <amk@amk.ca>
parents: 58159
diff changeset
657 if line and all((x in abbrevs) for x in line.split()):
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 23634
diff changeset
658 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
659 else:
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, "Valid sort keys (unique prefixes are accepted):"
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
661 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
662 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
663 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
664 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
665 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
666 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
667 def complete_sort(self, text, *args):
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
668 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
669
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 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
671 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
672 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
673 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
674 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
675
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 def do_strip(self, line):
63511
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48146
diff changeset
677 if self.stats:
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48146
diff changeset
678 self.stats.strip_dirs()
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48146
diff changeset
679 else:
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48146
diff changeset
680 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
681 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
682 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
683
63511
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48146
diff changeset
684 def help_help(self):
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48146
diff changeset
685 print >> self.stream, "Show help for a given command."
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48146
diff changeset
686
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
687 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
688 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
689 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
690 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
691
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 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
693 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
694 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
695 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
696 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
697 try:
38742
49026af9d1d7 Fix errors found by pychecker.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37873
diff changeset
698 browser = ProfileBrowser(initprofile)
49026af9d1d7 Fix errors found by pychecker.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37873
diff changeset
699 print >> browser.stream, "Welcome to the profile statistics browser."
49026af9d1d7 Fix errors found by pychecker.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37873
diff changeset
700 browser.cmdloop()
49026af9d1d7 Fix errors found by pychecker.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37873
diff changeset
701 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
702 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
703 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
704
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
705 # That's all, folks.