profilee.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """
  2. Input for test_profile.py and test_cprofile.py.
  3. IMPORTANT: This stuff is touchy. If you modify anything above the
  4. test class you'll have to regenerate the stats by running the two
  5. test files.
  6. *ALL* NUMBERS in the expected output are relevant. If you change
  7. the formatting of pstats, please don't just regenerate the expected
  8. output without checking very carefully that not a single number has
  9. changed.
  10. """
  11. import sys
  12. # In order to have reproducible time, we simulate a timer in the global
  13. # variable 'TICKS', which represents simulated time in milliseconds.
  14. # (We can't use a helper function increment the timer since it would be
  15. # included in the profile and would appear to consume all the time.)
  16. TICKS = 42000
  17. def timer():
  18. return TICKS
  19. def testfunc():
  20. # 1 call
  21. # 1000 ticks total: 270 ticks local, 730 ticks in subfunctions
  22. global TICKS
  23. TICKS += 99
  24. helper() # 300
  25. helper() # 300
  26. TICKS += 171
  27. factorial(14) # 130
  28. def factorial(n):
  29. # 23 calls total
  30. # 170 ticks total, 150 ticks local
  31. # 3 primitive calls, 130, 20 and 20 ticks total
  32. # including 116, 17, 17 ticks local
  33. global TICKS
  34. if n > 0:
  35. TICKS += n
  36. return mul(n, factorial(n-1))
  37. else:
  38. TICKS += 11
  39. return 1
  40. def mul(a, b):
  41. # 20 calls
  42. # 1 tick, local
  43. global TICKS
  44. TICKS += 1
  45. return a * b
  46. def helper():
  47. # 2 calls
  48. # 300 ticks total: 20 ticks local, 260 ticks in subfunctions
  49. global TICKS
  50. TICKS += 1
  51. helper1() # 30
  52. TICKS += 2
  53. helper1() # 30
  54. TICKS += 6
  55. helper2() # 50
  56. TICKS += 3
  57. helper2() # 50
  58. TICKS += 2
  59. helper2() # 50
  60. TICKS += 5
  61. helper2_indirect() # 70
  62. TICKS += 1
  63. def helper1():
  64. # 4 calls
  65. # 30 ticks total: 29 ticks local, 1 tick in subfunctions
  66. global TICKS
  67. TICKS += 10
  68. hasattr(C(), "foo") # 1
  69. TICKS += 19
  70. lst = []
  71. lst.append(42) # 0
  72. sys.exc_info() # 0
  73. def helper2_indirect():
  74. helper2() # 50
  75. factorial(3) # 20
  76. def helper2():
  77. # 8 calls
  78. # 50 ticks local: 39 ticks local, 11 ticks in subfunctions
  79. global TICKS
  80. TICKS += 11
  81. hasattr(C(), "bar") # 1
  82. TICKS += 13
  83. subhelper() # 10
  84. TICKS += 15
  85. def subhelper():
  86. # 8 calls
  87. # 10 ticks total: 8 ticks local, 2 ticks in subfunctions
  88. global TICKS
  89. TICKS += 2
  90. for i in range(2): # 0
  91. try:
  92. C().foo # 1 x 2
  93. except AttributeError:
  94. TICKS += 3 # 3 x 2
  95. class C:
  96. def __getattr__(self, name):
  97. # 28 calls
  98. # 1 tick, local
  99. global TICKS
  100. TICKS += 1
  101. raise AttributeError