PyXR

c:\python24\lib \ test \ test_profile.py



0001 """Test suite for the profile module."""
0002 
0003 import profile
0004 import os
0005 from test.test_support import TESTFN, vereq
0006 
0007 # In order to have reproducible time, we simulate a timer in the global
0008 # variable 'ticks', which represents simulated time in milliseconds.
0009 # (We can't use a helper function increment the timer since it would be
0010 # included in the profile and would appear to consume all the time.)
0011 ticks = 0
0012 
0013 def test_main():
0014     global ticks
0015     ticks = 0
0016     prof = profile.Profile(timer)
0017     prof.runctx("testfunc()", globals(), globals())
0018     prof.print_stats()
0019 
0020 def timer():
0021     return ticks*0.001
0022 
0023 def testfunc():
0024     # 1 call
0025     # 1000 ticks total: 400 ticks local, 600 ticks in subfunctions
0026     global ticks
0027     ticks += 199
0028     helper()                            # 300
0029     helper()                            # 300
0030     ticks += 201
0031 
0032 def helper():
0033     # 2 calls
0034     # 300 ticks total: 40 ticks local, 260 ticks in subfunctions
0035     global ticks
0036     ticks += 1
0037     helper1()                           # 30
0038     ticks += 3
0039     helper1()                           # 30
0040     ticks += 6
0041     helper2()                           # 50
0042     ticks += 5
0043     helper2()                           # 50
0044     ticks += 4
0045     helper2()                           # 50
0046     ticks += 7
0047     helper2()                           # 50
0048     ticks += 14
0049 
0050 def helper1():
0051     # 4 calls
0052     # 30 ticks total: 29 ticks local, 1 tick in subfunctions
0053     global ticks
0054     ticks += 10
0055     hasattr(C(), "foo")
0056     ticks += 19
0057 
0058 def helper2():
0059     # 8 calls
0060     # 50 ticks local: 39 ticks local, 11 ticks in subfunctions
0061     global ticks
0062     ticks += 11
0063     hasattr(C(), "bar")                 # 1
0064     ticks += 13
0065     subhelper()                         # 10
0066     ticks += 15
0067 
0068 def subhelper():
0069     # 8 calls
0070     # 10 ticks total: 8 ticks local, 2 ticks in subfunctions
0071     global ticks
0072     ticks += 2
0073     for i in range(2):
0074         try:
0075             C().foo                     # 1 x 2
0076         except AttributeError:
0077             ticks += 3                  # 3 x 2
0078 
0079 class C:
0080     def __getattr__(self, name):
0081         # 28 calls
0082         # 1 tick, local
0083         global ticks
0084         ticks += 1
0085         raise AttributeError
0086 
0087 
0088 def test_2():
0089     d = globals().copy()
0090     def testfunc():
0091         global x
0092         x = 1
0093     d['testfunc'] = testfunc
0094     profile.runctx("testfunc()", d, d, TESTFN)
0095     vereq (x, 1)
0096     os.unlink (TESTFN)
0097 
0098 if __name__ == "__main__":
0099     test_main()
0100     test_2()
0101 

Generated by PyXR 0.9.4
SourceForge.net Logo