test_pstats.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import unittest
  2. from test import support
  3. from io import StringIO
  4. from pstats import SortKey
  5. from enum import StrEnum, _test_simple_enum
  6. import pstats
  7. import cProfile
  8. class AddCallersTestCase(unittest.TestCase):
  9. """Tests for pstats.add_callers helper."""
  10. def test_combine_results(self):
  11. # pstats.add_callers should combine the call results of both target
  12. # and source by adding the call time. See issue1269.
  13. # new format: used by the cProfile module
  14. target = {"a": (1, 2, 3, 4)}
  15. source = {"a": (1, 2, 3, 4), "b": (5, 6, 7, 8)}
  16. new_callers = pstats.add_callers(target, source)
  17. self.assertEqual(new_callers, {'a': (2, 4, 6, 8), 'b': (5, 6, 7, 8)})
  18. # old format: used by the profile module
  19. target = {"a": 1}
  20. source = {"a": 1, "b": 5}
  21. new_callers = pstats.add_callers(target, source)
  22. self.assertEqual(new_callers, {'a': 2, 'b': 5})
  23. class StatsTestCase(unittest.TestCase):
  24. def setUp(self):
  25. stats_file = support.findfile('pstats.pck')
  26. self.stats = pstats.Stats(stats_file)
  27. def test_add(self):
  28. stream = StringIO()
  29. stats = pstats.Stats(stream=stream)
  30. stats.add(self.stats, self.stats)
  31. def test_sort_stats_int(self):
  32. valid_args = {-1: 'stdname',
  33. 0: 'calls',
  34. 1: 'time',
  35. 2: 'cumulative'}
  36. for arg_int, arg_str in valid_args.items():
  37. self.stats.sort_stats(arg_int)
  38. self.assertEqual(self.stats.sort_type,
  39. self.stats.sort_arg_dict_default[arg_str][-1])
  40. def test_sort_stats_string(self):
  41. for sort_name in ['calls', 'ncalls', 'cumtime', 'cumulative',
  42. 'filename', 'line', 'module', 'name', 'nfl', 'pcalls',
  43. 'stdname', 'time', 'tottime']:
  44. self.stats.sort_stats(sort_name)
  45. self.assertEqual(self.stats.sort_type,
  46. self.stats.sort_arg_dict_default[sort_name][-1])
  47. def test_sort_stats_partial(self):
  48. sortkey = 'filename'
  49. for sort_name in ['f', 'fi', 'fil', 'file', 'filen', 'filena',
  50. 'filenam', 'filename']:
  51. self.stats.sort_stats(sort_name)
  52. self.assertEqual(self.stats.sort_type,
  53. self.stats.sort_arg_dict_default[sortkey][-1])
  54. def test_sort_stats_enum(self):
  55. for member in SortKey:
  56. self.stats.sort_stats(member)
  57. self.assertEqual(
  58. self.stats.sort_type,
  59. self.stats.sort_arg_dict_default[member.value][-1])
  60. class CheckedSortKey(StrEnum):
  61. CALLS = 'calls', 'ncalls'
  62. CUMULATIVE = 'cumulative', 'cumtime'
  63. FILENAME = 'filename', 'module'
  64. LINE = 'line'
  65. NAME = 'name'
  66. NFL = 'nfl'
  67. PCALLS = 'pcalls'
  68. STDNAME = 'stdname'
  69. TIME = 'time', 'tottime'
  70. def __new__(cls, *values):
  71. value = values[0]
  72. obj = str.__new__(cls, value)
  73. obj._value_ = value
  74. for other_value in values[1:]:
  75. cls._value2member_map_[other_value] = obj
  76. obj._all_values = values
  77. return obj
  78. _test_simple_enum(CheckedSortKey, SortKey)
  79. def test_sort_starts_mix(self):
  80. self.assertRaises(TypeError, self.stats.sort_stats,
  81. 'calls',
  82. SortKey.TIME)
  83. self.assertRaises(TypeError, self.stats.sort_stats,
  84. SortKey.TIME,
  85. 'calls')
  86. def test_get_stats_profile(self):
  87. def pass1(): pass
  88. def pass2(): pass
  89. def pass3(): pass
  90. pr = cProfile.Profile()
  91. pr.enable()
  92. pass1()
  93. pass2()
  94. pass3()
  95. pr.create_stats()
  96. ps = pstats.Stats(pr)
  97. stats_profile = ps.get_stats_profile()
  98. funcs_called = set(stats_profile.func_profiles.keys())
  99. self.assertIn('pass1', funcs_called)
  100. self.assertIn('pass2', funcs_called)
  101. self.assertIn('pass3', funcs_called)
  102. def test_SortKey_enum(self):
  103. self.assertEqual(SortKey.FILENAME, 'filename')
  104. self.assertNotEqual(SortKey.FILENAME, SortKey.CALLS)
  105. if __name__ == "__main__":
  106. unittest.main()