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