blob: 95ad8d2dbe023c2efcb8f770d58ccb392ea12171 [file] [log] [blame]
Guido van Rossumf137f752001-10-04 00:58:24 +00001"""Test suite for the profile module."""
2
Armin Rigoa871ef22006-02-08 12:53:56 +00003import profile, pstats, sys
Guido van Rossumf137f752001-10-04 00:58:24 +00004
5# In order to have reproducible time, we simulate a timer in the global
6# variable 'ticks', which represents simulated time in milliseconds.
7# (We can't use a helper function increment the timer since it would be
8# included in the profile and would appear to consume all the time.)
9ticks = 0
Tim Peters527e64f2001-10-04 05:36:56 +000010
Armin Rigoa871ef22006-02-08 12:53:56 +000011# IMPORTANT: this is an output test. *ALL* NUMBERS in the expected
12# output are relevant. If you change the formatting of pstats,
13# please don't just regenerate output/test_profile without checking
14# very carefully that not a single number has changed.
15
16def test_main():
Guido van Rossumf137f752001-10-04 00:58:24 +000017 global ticks
Armin Rigoa871ef22006-02-08 12:53:56 +000018 ticks = 42000
Guido van Rossumf137f752001-10-04 00:58:24 +000019 prof = profile.Profile(timer)
Armin Rigoa871ef22006-02-08 12:53:56 +000020 prof.runctx("testfunc()", globals(), locals())
21 assert ticks == 43000, ticks
22 st = pstats.Stats(prof)
23 st.strip_dirs().sort_stats('stdname').print_stats()
24 st.print_callees()
25 st.print_callers()
Guido van Rossumf137f752001-10-04 00:58:24 +000026
27def timer():
28 return ticks*0.001
29
30def testfunc():
31 # 1 call
Armin Rigoa871ef22006-02-08 12:53:56 +000032 # 1000 ticks total: 270 ticks local, 730 ticks in subfunctions
Guido van Rossumf137f752001-10-04 00:58:24 +000033 global ticks
Armin Rigoa871ef22006-02-08 12:53:56 +000034 ticks += 99
Guido van Rossumf137f752001-10-04 00:58:24 +000035 helper() # 300
36 helper() # 300
Armin Rigoa871ef22006-02-08 12:53:56 +000037 ticks += 171
38 factorial(14) # 130
39
40def factorial(n):
41 # 23 calls total
42 # 170 ticks total, 150 ticks local
43 # 3 primitive calls, 130, 20 and 20 ticks total
44 # including 116, 17, 17 ticks local
45 global ticks
46 if n > 0:
47 ticks += n
48 return mul(n, factorial(n-1))
49 else:
50 ticks += 11
51 return 1
52
53def mul(a, b):
54 # 20 calls
55 # 1 tick, local
56 global ticks
57 ticks += 1
58 return a * b
Guido van Rossumf137f752001-10-04 00:58:24 +000059
60def helper():
61 # 2 calls
Armin Rigoa871ef22006-02-08 12:53:56 +000062 # 300 ticks total: 20 ticks local, 260 ticks in subfunctions
Guido van Rossumf137f752001-10-04 00:58:24 +000063 global ticks
64 ticks += 1
65 helper1() # 30
Armin Rigoa871ef22006-02-08 12:53:56 +000066 ticks += 2
Guido van Rossumf137f752001-10-04 00:58:24 +000067 helper1() # 30
68 ticks += 6
69 helper2() # 50
Armin Rigoa871ef22006-02-08 12:53:56 +000070 ticks += 3
71 helper2() # 50
72 ticks += 2
73 helper2() # 50
Guido van Rossumf137f752001-10-04 00:58:24 +000074 ticks += 5
Armin Rigoa871ef22006-02-08 12:53:56 +000075 helper2_indirect() # 70
76 ticks += 1
Guido van Rossumf137f752001-10-04 00:58:24 +000077
78def helper1():
79 # 4 calls
80 # 30 ticks total: 29 ticks local, 1 tick in subfunctions
81 global ticks
82 ticks += 10
Armin Rigoa871ef22006-02-08 12:53:56 +000083 hasattr(C(), "foo") # 1
Guido van Rossumf137f752001-10-04 00:58:24 +000084 ticks += 19
Armin Rigoa871ef22006-02-08 12:53:56 +000085 lst = []
86 lst.append(42) # 0
87 sys.exc_info() # 0
88
89def helper2_indirect():
90 helper2() # 50
91 factorial(3) # 20
Guido van Rossumf137f752001-10-04 00:58:24 +000092
93def helper2():
94 # 8 calls
95 # 50 ticks local: 39 ticks local, 11 ticks in subfunctions
96 global ticks
97 ticks += 11
98 hasattr(C(), "bar") # 1
99 ticks += 13
100 subhelper() # 10
101 ticks += 15
102
103def subhelper():
104 # 8 calls
105 # 10 ticks total: 8 ticks local, 2 ticks in subfunctions
106 global ticks
107 ticks += 2
Armin Rigoa871ef22006-02-08 12:53:56 +0000108 for i in range(2): # 0
Guido van Rossumf137f752001-10-04 00:58:24 +0000109 try:
110 C().foo # 1 x 2
111 except AttributeError:
112 ticks += 3 # 3 x 2
113
114class C:
115 def __getattr__(self, name):
116 # 28 calls
117 # 1 tick, local
118 global ticks
119 ticks += 1
120 raise AttributeError
121
122if __name__ == "__main__":
123 test_main()