annotate Lib/test/profilee.py @ 79675:c0a2294de787 2.7

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