test_cprofile.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. """Test suite for the cProfile module."""
  2. import sys
  3. from test.test_support import run_unittest, TESTFN, unlink
  4. # rip off all interesting stuff from test_profile
  5. import cProfile
  6. from test.test_profile import ProfileTest, regenerate_expected_output
  7. class CProfileTest(ProfileTest):
  8. profilerclass = cProfile.Profile
  9. expected_list_sort_output = "{method 'sort' of 'list' objects}"
  10. # Issue 3895.
  11. def test_bad_counter_during_dealloc(self):
  12. import _lsprof
  13. # Must use a file as StringIO doesn't trigger the bug.
  14. sys.stderr = open(TESTFN, 'w')
  15. try:
  16. obj = _lsprof.Profiler(lambda: int)
  17. obj.enable()
  18. obj = _lsprof.Profiler(1)
  19. obj.disable()
  20. finally:
  21. sys.stderr = sys.__stderr__
  22. unlink(TESTFN)
  23. def test_main():
  24. run_unittest(CProfileTest)
  25. def main():
  26. if '-r' not in sys.argv:
  27. test_main()
  28. else:
  29. regenerate_expected_output(__file__, CProfileTest)
  30. # Don't remove this comment. Everything below it is auto-generated.
  31. #--cut--------------------------------------------------------------------------
  32. CProfileTest.expected_output['print_stats'] = """\
  33. 126 function calls (106 primitive calls) in 1.000 seconds
  34. Ordered by: standard name
  35. ncalls tottime percall cumtime percall filename:lineno(function)
  36. 1 0.000 0.000 1.000 1.000 <string>:1(<module>)
  37. 28 0.028 0.001 0.028 0.001 profilee.py:110(__getattr__)
  38. 1 0.270 0.270 1.000 1.000 profilee.py:25(testfunc)
  39. 23/3 0.150 0.007 0.170 0.057 profilee.py:35(factorial)
  40. 20 0.020 0.001 0.020 0.001 profilee.py:48(mul)
  41. 2 0.040 0.020 0.600 0.300 profilee.py:55(helper)
  42. 4 0.116 0.029 0.120 0.030 profilee.py:73(helper1)
  43. 2 0.000 0.000 0.140 0.070 profilee.py:84(helper2_indirect)
  44. 8 0.312 0.039 0.400 0.050 profilee.py:88(helper2)
  45. 8 0.064 0.008 0.080 0.010 profilee.py:98(subhelper)
  46. 12 0.000 0.000 0.012 0.001 {hasattr}
  47. 4 0.000 0.000 0.000 0.000 {method 'append' of 'list' objects}
  48. 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
  49. 8 0.000 0.000 0.000 0.000 {range}
  50. 4 0.000 0.000 0.000 0.000 {sys.exc_info}
  51. """
  52. CProfileTest.expected_output['print_callers'] = """\
  53. Ordered by: standard name
  54. Function was called by...
  55. ncalls tottime cumtime
  56. <string>:1(<module>) <-
  57. profilee.py:110(__getattr__) <- 16 0.016 0.016 profilee.py:98(subhelper)
  58. 12 0.012 0.012 {hasattr}
  59. profilee.py:25(testfunc) <- 1 0.270 1.000 <string>:1(<module>)
  60. profilee.py:35(factorial) <- 1 0.014 0.130 profilee.py:25(testfunc)
  61. 20/3 0.130 0.147 profilee.py:35(factorial)
  62. 2 0.006 0.040 profilee.py:84(helper2_indirect)
  63. profilee.py:48(mul) <- 20 0.020 0.020 profilee.py:35(factorial)
  64. profilee.py:55(helper) <- 2 0.040 0.600 profilee.py:25(testfunc)
  65. profilee.py:73(helper1) <- 4 0.116 0.120 profilee.py:55(helper)
  66. profilee.py:84(helper2_indirect) <- 2 0.000 0.140 profilee.py:55(helper)
  67. profilee.py:88(helper2) <- 6 0.234 0.300 profilee.py:55(helper)
  68. 2 0.078 0.100 profilee.py:84(helper2_indirect)
  69. profilee.py:98(subhelper) <- 8 0.064 0.080 profilee.py:88(helper2)
  70. {hasattr} <- 4 0.000 0.004 profilee.py:73(helper1)
  71. 8 0.000 0.008 profilee.py:88(helper2)
  72. {method 'append' of 'list' objects} <- 4 0.000 0.000 profilee.py:73(helper1)
  73. {method 'disable' of '_lsprof.Profiler' objects} <-
  74. {range} <- 8 0.000 0.000 profilee.py:98(subhelper)
  75. {sys.exc_info} <- 4 0.000 0.000 profilee.py:73(helper1)
  76. """
  77. CProfileTest.expected_output['print_callees'] = """\
  78. Ordered by: standard name
  79. Function called...
  80. ncalls tottime cumtime
  81. <string>:1(<module>) -> 1 0.270 1.000 profilee.py:25(testfunc)
  82. profilee.py:110(__getattr__) ->
  83. profilee.py:25(testfunc) -> 1 0.014 0.130 profilee.py:35(factorial)
  84. 2 0.040 0.600 profilee.py:55(helper)
  85. profilee.py:35(factorial) -> 20/3 0.130 0.147 profilee.py:35(factorial)
  86. 20 0.020 0.020 profilee.py:48(mul)
  87. profilee.py:48(mul) ->
  88. profilee.py:55(helper) -> 4 0.116 0.120 profilee.py:73(helper1)
  89. 2 0.000 0.140 profilee.py:84(helper2_indirect)
  90. 6 0.234 0.300 profilee.py:88(helper2)
  91. profilee.py:73(helper1) -> 4 0.000 0.004 {hasattr}
  92. 4 0.000 0.000 {method 'append' of 'list' objects}
  93. 4 0.000 0.000 {sys.exc_info}
  94. profilee.py:84(helper2_indirect) -> 2 0.006 0.040 profilee.py:35(factorial)
  95. 2 0.078 0.100 profilee.py:88(helper2)
  96. profilee.py:88(helper2) -> 8 0.064 0.080 profilee.py:98(subhelper)
  97. 8 0.000 0.008 {hasattr}
  98. profilee.py:98(subhelper) -> 16 0.016 0.016 profilee.py:110(__getattr__)
  99. 8 0.000 0.000 {range}
  100. {hasattr} -> 12 0.012 0.012 profilee.py:110(__getattr__)
  101. {method 'append' of 'list' objects} ->
  102. {method 'disable' of '_lsprof.Profiler' objects} ->
  103. {range} ->
  104. {sys.exc_info} ->
  105. """
  106. if __name__ == "__main__":
  107. main()