blob: 10559deb6bcb23a91b5cc690e06337910b79cd71 [file] [log] [blame]
Christian Heimese1c98112008-01-21 11:20:28 +00001import unittest
Daniel Olshansky01602ae2020-01-15 17:51:54 -05002
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test import support
Georg Brandl83938432010-10-22 06:28:01 +00004from io import StringIO
mwidjaja863b1e42018-01-25 20:49:56 -08005from pstats import SortKey
Christian Heimese1c98112008-01-21 11:20:28 +00006
Daniel Olshansky01602ae2020-01-15 17:51:54 -05007import pstats
Daniel Olshansky01602ae2020-01-15 17:51:54 -05008import cProfile
Christian Heimese1c98112008-01-21 11:20:28 +00009
10class AddCallersTestCase(unittest.TestCase):
11 """Tests for pstats.add_callers helper."""
12
13 def test_combine_results(self):
Georg Brandl83938432010-10-22 06:28:01 +000014 # pstats.add_callers should combine the call results of both target
15 # and source by adding the call time. See issue1269.
Georg Brandl2d3c4e72010-08-02 17:24:49 +000016 # new format: used by the cProfile module
Christian Heimese1c98112008-01-21 11:20:28 +000017 target = {"a": (1, 2, 3, 4)}
18 source = {"a": (1, 2, 3, 4), "b": (5, 6, 7, 8)}
19 new_callers = pstats.add_callers(target, source)
20 self.assertEqual(new_callers, {'a': (2, 4, 6, 8), 'b': (5, 6, 7, 8)})
Georg Brandl2d3c4e72010-08-02 17:24:49 +000021 # old format: used by the profile module
22 target = {"a": 1}
23 source = {"a": 1, "b": 5}
24 new_callers = pstats.add_callers(target, source)
25 self.assertEqual(new_callers, {'a': 2, 'b': 5})
Christian Heimese1c98112008-01-21 11:20:28 +000026
27
Georg Brandl83938432010-10-22 06:28:01 +000028class StatsTestCase(unittest.TestCase):
29 def setUp(self):
30 stats_file = support.findfile('pstats.pck')
31 self.stats = pstats.Stats(stats_file)
32
33 def test_add(self):
34 stream = StringIO()
35 stats = pstats.Stats(stream=stream)
36 stats.add(self.stats, self.stats)
37
mwidjaja863b1e42018-01-25 20:49:56 -080038 def test_sort_stats_int(self):
39 valid_args = {-1: 'stdname',
40 0: 'calls',
41 1: 'time',
42 2: 'cumulative'}
43 for arg_int, arg_str in valid_args.items():
44 self.stats.sort_stats(arg_int)
45 self.assertEqual(self.stats.sort_type,
46 self.stats.sort_arg_dict_default[arg_str][-1])
47
48 def test_sort_stats_string(self):
49 for sort_name in ['calls', 'ncalls', 'cumtime', 'cumulative',
50 'filename', 'line', 'module', 'name', 'nfl', 'pcalls',
51 'stdname', 'time', 'tottime']:
52 self.stats.sort_stats(sort_name)
53 self.assertEqual(self.stats.sort_type,
54 self.stats.sort_arg_dict_default[sort_name][-1])
55
56 def test_sort_stats_partial(self):
57 sortkey = 'filename'
58 for sort_name in ['f', 'fi', 'fil', 'file', 'filen', 'filena',
59 'filenam', 'filename']:
60 self.stats.sort_stats(sort_name)
61 self.assertEqual(self.stats.sort_type,
62 self.stats.sort_arg_dict_default[sortkey][-1])
63
64 def test_sort_stats_enum(self):
65 for member in SortKey:
66 self.stats.sort_stats(member)
67 self.assertEqual(
68 self.stats.sort_type,
69 self.stats.sort_arg_dict_default[member.value][-1])
70
71 def test_sort_starts_mix(self):
72 self.assertRaises(TypeError, self.stats.sort_stats,
73 'calls',
74 SortKey.TIME)
75 self.assertRaises(TypeError, self.stats.sort_stats,
76 SortKey.TIME,
77 'calls')
78
Daniel Olshansky01602ae2020-01-15 17:51:54 -050079 def test_get_stats_profile(self):
80 def pass1(): pass
81 def pass2(): pass
82 def pass3(): pass
83
84 pr = cProfile.Profile()
85 pr.enable()
86 pass1()
87 pass2()
88 pass3()
89 pr.create_stats()
90 ps = pstats.Stats(pr)
91
92 stats_profile = ps.get_stats_profile()
93 funcs_called = set(stats_profile.func_profiles.keys())
94 self.assertIn('pass1', funcs_called)
95 self.assertIn('pass2', funcs_called)
96 self.assertIn('pass3', funcs_called)
Georg Brandl83938432010-10-22 06:28:01 +000097
Christian Heimese1c98112008-01-21 11:20:28 +000098if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -050099 unittest.main()