Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / lib / python2.4 / test / test_profile.py
CommitLineData
920dae64
AT
1"""Test suite for the profile module."""
2
3import profile
4import os
5from test.test_support import TESTFN, vereq
6
7# In order to have reproducible time, we simulate a timer in the global
8# variable 'ticks', which represents simulated time in milliseconds.
9# (We can't use a helper function increment the timer since it would be
10# included in the profile and would appear to consume all the time.)
11ticks = 0
12
13def test_1():
14 global ticks
15 ticks = 0
16 prof = profile.Profile(timer)
17 prof.runctx("testfunc()", globals(), globals())
18 prof.print_stats()
19
20def timer():
21 return ticks*0.001
22
23def testfunc():
24 # 1 call
25 # 1000 ticks total: 400 ticks local, 600 ticks in subfunctions
26 global ticks
27 ticks += 199
28 helper() # 300
29 helper() # 300
30 ticks += 201
31
32def helper():
33 # 2 calls
34 # 300 ticks total: 40 ticks local, 260 ticks in subfunctions
35 global ticks
36 ticks += 1
37 helper1() # 30
38 ticks += 3
39 helper1() # 30
40 ticks += 6
41 helper2() # 50
42 ticks += 5
43 helper2() # 50
44 ticks += 4
45 helper2() # 50
46 ticks += 7
47 helper2() # 50
48 ticks += 14
49
50def helper1():
51 # 4 calls
52 # 30 ticks total: 29 ticks local, 1 tick in subfunctions
53 global ticks
54 ticks += 10
55 hasattr(C(), "foo")
56 ticks += 19
57
58def helper2():
59 # 8 calls
60 # 50 ticks local: 39 ticks local, 11 ticks in subfunctions
61 global ticks
62 ticks += 11
63 hasattr(C(), "bar") # 1
64 ticks += 13
65 subhelper() # 10
66 ticks += 15
67
68def subhelper():
69 # 8 calls
70 # 10 ticks total: 8 ticks local, 2 ticks in subfunctions
71 global ticks
72 ticks += 2
73 for i in range(2):
74 try:
75 C().foo # 1 x 2
76 except AttributeError:
77 ticks += 3 # 3 x 2
78
79class C:
80 def __getattr__(self, name):
81 # 28 calls
82 # 1 tick, local
83 global ticks
84 ticks += 1
85 raise AttributeError
86
87
88def test_2():
89 d = globals().copy()
90 def testfunc():
91 global x
92 x = 1
93 d['testfunc'] = testfunc
94 profile.runctx("testfunc()", d, d, TESTFN)
95 vereq (x, 1)
96 os.unlink (TESTFN)
97
98def test_3():
99 result = []
100 def testfunc1():
101 try: len(None)
102 except: pass
103 try: len(None)
104 except: pass
105 result.append(True)
106 def testfunc2():
107 testfunc1()
108 testfunc1()
109 profile.runctx("testfunc2()", locals(), locals(), TESTFN)
110 vereq(result, [True, True])
111 os.unlink(TESTFN)
112
113def test_main():
114 test_1()
115 test_2()
116 test_3()
117
118if __name__ == "__main__":
119 test_main()