test_cprofile.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. """Test suite for the cProfile module."""
  2. import sys
  3. import unittest
  4. # rip off all interesting stuff from test_profile
  5. import cProfile
  6. from test.test_profile import ProfileTest, regenerate_expected_output
  7. from test.support.script_helper import assert_python_failure
  8. from test import support
  9. class CProfileTest(ProfileTest):
  10. profilerclass = cProfile.Profile
  11. profilermodule = cProfile
  12. expected_max_output = "{built-in method builtins.max}"
  13. def get_expected_output(self):
  14. return _ProfileOutput
  15. def test_bad_counter_during_dealloc(self):
  16. # bpo-3895
  17. import _lsprof
  18. with support.catch_unraisable_exception() as cm:
  19. obj = _lsprof.Profiler(lambda: int)
  20. obj.enable()
  21. obj = _lsprof.Profiler(1)
  22. obj.disable()
  23. obj.clear()
  24. self.assertEqual(cm.unraisable.exc_type, TypeError)
  25. def test_profile_enable_disable(self):
  26. prof = self.profilerclass()
  27. # Make sure we clean ourselves up if the test fails for some reason.
  28. self.addCleanup(prof.disable)
  29. prof.enable()
  30. self.assertIs(sys.getprofile(), prof)
  31. prof.disable()
  32. self.assertIs(sys.getprofile(), None)
  33. def test_profile_as_context_manager(self):
  34. prof = self.profilerclass()
  35. # Make sure we clean ourselves up if the test fails for some reason.
  36. self.addCleanup(prof.disable)
  37. with prof as __enter__return_value:
  38. # profile.__enter__ should return itself.
  39. self.assertIs(prof, __enter__return_value)
  40. # profile should be set as the global profiler inside the
  41. # with-block
  42. self.assertIs(sys.getprofile(), prof)
  43. # profile shouldn't be set once we leave the with-block.
  44. self.assertIs(sys.getprofile(), None)
  45. class TestCommandLine(unittest.TestCase):
  46. def test_sort(self):
  47. rc, out, err = assert_python_failure('-m', 'cProfile', '-s', 'demo')
  48. self.assertGreater(rc, 0)
  49. self.assertIn(b"option -s: invalid choice: 'demo'", err)
  50. def main():
  51. if '-r' not in sys.argv:
  52. unittest.main()
  53. else:
  54. regenerate_expected_output(__file__, CProfileTest)
  55. # Don't remove this comment. Everything below it is auto-generated.
  56. #--cut--------------------------------------------------------------------------
  57. _ProfileOutput = {}
  58. _ProfileOutput['print_stats'] = """\
  59. 28 0.028 0.001 0.028 0.001 profilee.py:110(__getattr__)
  60. 1 0.270 0.270 1.000 1.000 profilee.py:25(testfunc)
  61. 23/3 0.150 0.007 0.170 0.057 profilee.py:35(factorial)
  62. 20 0.020 0.001 0.020 0.001 profilee.py:48(mul)
  63. 2 0.040 0.020 0.600 0.300 profilee.py:55(helper)
  64. 4 0.116 0.029 0.120 0.030 profilee.py:73(helper1)
  65. 2 0.000 0.000 0.140 0.070 profilee.py:84(helper2_indirect)
  66. 8 0.312 0.039 0.400 0.050 profilee.py:88(helper2)
  67. 8 0.064 0.008 0.080 0.010 profilee.py:98(subhelper)"""
  68. _ProfileOutput['print_callers'] = """\
  69. profilee.py:110(__getattr__) <- 16 0.016 0.016 profilee.py:98(subhelper)
  70. profilee.py:25(testfunc) <- 1 0.270 1.000 <string>:1(<module>)
  71. profilee.py:35(factorial) <- 1 0.014 0.130 profilee.py:25(testfunc)
  72. 20/3 0.130 0.147 profilee.py:35(factorial)
  73. 2 0.006 0.040 profilee.py:84(helper2_indirect)
  74. profilee.py:48(mul) <- 20 0.020 0.020 profilee.py:35(factorial)
  75. profilee.py:55(helper) <- 2 0.040 0.600 profilee.py:25(testfunc)
  76. profilee.py:73(helper1) <- 4 0.116 0.120 profilee.py:55(helper)
  77. profilee.py:84(helper2_indirect) <- 2 0.000 0.140 profilee.py:55(helper)
  78. profilee.py:88(helper2) <- 6 0.234 0.300 profilee.py:55(helper)
  79. 2 0.078 0.100 profilee.py:84(helper2_indirect)
  80. profilee.py:98(subhelper) <- 8 0.064 0.080 profilee.py:88(helper2)
  81. {built-in method builtins.hasattr} <- 4 0.000 0.004 profilee.py:73(helper1)
  82. 8 0.000 0.008 profilee.py:88(helper2)
  83. {built-in method sys.exc_info} <- 4 0.000 0.000 profilee.py:73(helper1)
  84. {method 'append' of 'list' objects} <- 4 0.000 0.000 profilee.py:73(helper1)"""
  85. _ProfileOutput['print_callees'] = """\
  86. <string>:1(<module>) -> 1 0.270 1.000 profilee.py:25(testfunc)
  87. profilee.py:110(__getattr__) ->
  88. profilee.py:25(testfunc) -> 1 0.014 0.130 profilee.py:35(factorial)
  89. 2 0.040 0.600 profilee.py:55(helper)
  90. profilee.py:35(factorial) -> 20/3 0.130 0.147 profilee.py:35(factorial)
  91. 20 0.020 0.020 profilee.py:48(mul)
  92. profilee.py:48(mul) ->
  93. profilee.py:55(helper) -> 4 0.116 0.120 profilee.py:73(helper1)
  94. 2 0.000 0.140 profilee.py:84(helper2_indirect)
  95. 6 0.234 0.300 profilee.py:88(helper2)
  96. profilee.py:73(helper1) -> 4 0.000 0.004 {built-in method builtins.hasattr}
  97. profilee.py:84(helper2_indirect) -> 2 0.006 0.040 profilee.py:35(factorial)
  98. 2 0.078 0.100 profilee.py:88(helper2)
  99. profilee.py:88(helper2) -> 8 0.064 0.080 profilee.py:98(subhelper)
  100. profilee.py:98(subhelper) -> 16 0.016 0.016 profilee.py:110(__getattr__)
  101. {built-in method builtins.hasattr} -> 12 0.012 0.012 profilee.py:110(__getattr__)"""
  102. if __name__ == "__main__":
  103. main()