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