Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 1 | """Test suite for the profile module.""" |
| 2 | |
| 3 | import profile |
Nicholas Bastin | 1eb4bfc | 2004-03-22 20:12:56 +0000 | [diff] [blame] | 4 | import os |
| 5 | from test.test_support import TESTFN, vereq |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 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.) |
| 11 | ticks = 0 |
Tim Peters | 527e64f | 2001-10-04 05:36:56 +0000 | [diff] [blame] | 12 | |
Armin Rigo | f879024 | 2005-09-20 18:50:13 +0000 | [diff] [blame] | 13 | def test_1(): |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 14 | global ticks |
| 15 | ticks = 0 |
| 16 | prof = profile.Profile(timer) |
| 17 | prof.runctx("testfunc()", globals(), globals()) |
| 18 | prof.print_stats() |
| 19 | |
| 20 | def timer(): |
| 21 | return ticks*0.001 |
| 22 | |
| 23 | def 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 | |
| 32 | def 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 | |
| 50 | def 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 | |
| 58 | def 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 | |
| 68 | def 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 | |
| 79 | class C: |
| 80 | def __getattr__(self, name): |
| 81 | # 28 calls |
| 82 | # 1 tick, local |
| 83 | global ticks |
| 84 | ticks += 1 |
| 85 | raise AttributeError |
| 86 | |
Nicholas Bastin | 1eb4bfc | 2004-03-22 20:12:56 +0000 | [diff] [blame] | 87 | |
| 88 | def test_2(): |
Tim Peters | 27f8836 | 2004-07-08 04:22:35 +0000 | [diff] [blame] | 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) |
Nicholas Bastin | 1eb4bfc | 2004-03-22 20:12:56 +0000 | [diff] [blame] | 97 | |
Armin Rigo | f879024 | 2005-09-20 18:50:13 +0000 | [diff] [blame] | 98 | def 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 | |
| 113 | def test_main(): |
| 114 | test_1() |
| 115 | test_2() |
| 116 | test_3() |
| 117 | |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 118 | if __name__ == "__main__": |
| 119 | test_main() |