Debian Codegraph @

Lib/test/test_pstats.py

raw
1 import unittest
2
3 from test import support
4 from test.support.import_helper import ensure_lazy_imports
5 from io import StringIO
6 from pstats import SortKey
7 from enum import StrEnum, _test_simple_enum
8
9 import os
10 import pstats
11 import tempfile
12 import cProfile
13
14 class LazyImportTest(unittest.TestCase):
15 @support.cpython_only
16 def test_lazy_import(self):
17 ensure_lazy_imports("pstats", {"typing"})
18
19
20 class AddCallersTestCase(unittest.TestCase):
21 """Tests for pstats.add_callers helper."""
22
23 def test_combine_results(self):
24 # pstats.add_callers should combine the call results of both target
25 # and source by adding the call time. See issue1269.
26 # new format: used by the cProfile module
27 target = {"a": (1, 2, 3, 4)}
28 source = {"a": (1, 2, 3, 4), "b": (5, 6, 7, 8)}
29 new_callers = pstats.add_callers(target, source)
30 self.assertEqual(new_callers, {'a': (2, 4, 6, 8), 'b': (5, 6, 7, 8)})
31 # old format: used by the profile module
32 target = {"a": 1}
33 source = {"a": 1, "b": 5}
34 new_callers = pstats.add_callers(target, source)
35 self.assertEqual(new_callers, {'a': 2, 'b': 5})
36
37
38 class StatsTestCase(unittest.TestCase):
39 def setUp(self):
40 stats_file = support.findfile('pstats.pck')
41 self.stats = pstats.Stats(stats_file)
42
43 def test_add(self):
44 stream = StringIO()
45 stats = pstats.Stats(stream=stream)
46 stats.add(self.stats, self.stats)
47
48 def test_dump_and_load_works_correctly(self):
49 temp_storage_new = tempfile.NamedTemporaryFile(delete=False)
50 try:
51 self.stats.dump_stats(filename=temp_storage_new.name)
52 tmp_stats = pstats.Stats(temp_storage_new.name)
53 self.assertEqual(self.stats.stats, tmp_stats.stats)
54 finally:
55 temp_storage_new.close()
56 os.remove(temp_storage_new.name)
57
58 def test_load_equivalent_to_init(self):
59 stats = pstats.Stats()
60 self.temp_storage = tempfile.NamedTemporaryFile(delete=False)
61 try:
62 cProfile.run('import os', filename=self.temp_storage.name)
63 stats.load_stats(self.temp_storage.name)
64 created = pstats.Stats(self.temp_storage.name)
65 self.assertEqual(stats.stats, created.stats)
66 finally:
67 self.temp_storage.close()
68 os.remove(self.temp_storage.name)
69
70 def test_loading_wrong_types(self):
71 stats = pstats.Stats()
72 with self.assertRaises(TypeError):
73 stats.load_stats(42)
74
75 def test_sort_stats_int(self):
76 valid_args = {-1: 'stdname',
77 0: 'calls',
78 1: 'time',
79 2: 'cumulative'}
80 for arg_int, arg_str in valid_args.items():
81 self.stats.sort_stats(arg_int)
82 self.assertEqual(self.stats.sort_type,
83 self.stats.sort_arg_dict_default[arg_str][-1])
84
85 def test_sort_stats_string(self):
86 for sort_name in ['calls', 'ncalls', 'cumtime', 'cumulative',
87 'filename', 'line', 'module', 'name', 'nfl', 'pcalls',
88 'stdname', 'time', 'tottime']:
89 self.stats.sort_stats(sort_name)
90 self.assertEqual(self.stats.sort_type,
91 self.stats.sort_arg_dict_default[sort_name][-1])
92
93 def test_sort_stats_partial(self):
94 sortkey = 'filename'
95 for sort_name in ['f', 'fi', 'fil', 'file', 'filen', 'filena',
96 'filenam', 'filename']:
97 self.stats.sort_stats(sort_name)
98 self.assertEqual(self.stats.sort_type,
99 self.stats.sort_arg_dict_default[sortkey][-1])
100
101 def test_sort_stats_enum(self):
102 for member in SortKey:
103 self.stats.sort_stats(member)
104 self.assertEqual(
105 self.stats.sort_type,
106 self.stats.sort_arg_dict_default[member.value][-1])
107 class CheckedSortKey(StrEnum):
108 CALLS = 'calls', 'ncalls'
109 CUMULATIVE = 'cumulative', 'cumtime'
110 FILENAME = 'filename', 'module'
111 LINE = 'line'
112 NAME = 'name'
113 NFL = 'nfl'
114 PCALLS = 'pcalls'
115 STDNAME = 'stdname'
116 TIME = 'time', 'tottime'
117 def __new__(cls, *values):
118 value = values[0]
119 obj = str.__new__(cls, value)
120 obj._value_ = value
121 for other_value in values[1:]:
122 cls._value2member_map_[other_value] = obj
123 obj._all_values = values
124 return obj
125 _test_simple_enum(CheckedSortKey, SortKey)
126
127 def test_sort_starts_mix(self):
128 self.assertRaises(TypeError, self.stats.sort_stats,
129 'calls',
130 SortKey.TIME)
131 self.assertRaises(TypeError, self.stats.sort_stats,
132 SortKey.TIME,
133 'calls')
134
135 def test_get_stats_profile(self):
136 def pass1(): pass
137 def pass2(): pass
138 def pass3(): pass
139
140 pr = cProfile.Profile()
141 pr.enable()
142 pass1()
143 pass2()
144 pass3()
145 pr.create_stats()
146 ps = pstats.Stats(pr)
147
148 stats_profile = ps.get_stats_profile()
149 funcs_called = set(stats_profile.func_profiles.keys())
150 self.assertIn('pass1', funcs_called)
151 self.assertIn('pass2', funcs_called)
152 self.assertIn('pass3', funcs_called)
153
154 def test_SortKey_enum(self):
155 self.assertEqual(SortKey.FILENAME, 'filename')
156 self.assertNotEqual(SortKey.FILENAME, SortKey.CALLS)
157
158 if __name__ == "__main__":
159 unittest.main()