| 
									
										
										
										
											2001-10-04 00:58:24 +00:00
										 |  |  | """Test suite for the profile module.""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-08 12:53:56 +00:00
										 |  |  | import profile, pstats, sys | 
					
						
							| 
									
										
										
										
											2001-10-04 00:58:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # In order to have reproducible time, we simulate a timer in the global | 
					
						
							|  |  |  | # variable 'ticks', which represents simulated time in milliseconds. | 
					
						
							|  |  |  | # (We can't use a helper function increment the timer since it would be | 
					
						
							|  |  |  | # included in the profile and would appear to consume all the time.) | 
					
						
							|  |  |  | ticks = 0 | 
					
						
							| 
									
										
										
										
											2001-10-04 05:36:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-08 12:53:56 +00:00
										 |  |  | # IMPORTANT: this is an output test.  *ALL* NUMBERS in the expected | 
					
						
							|  |  |  | # output are relevant.  If you change the formatting of pstats, | 
					
						
							|  |  |  | # please don't just regenerate output/test_profile without checking | 
					
						
							|  |  |  | # very carefully that not a single number has changed. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_main(): | 
					
						
							| 
									
										
										
										
											2001-10-04 00:58:24 +00:00
										 |  |  |     global ticks | 
					
						
							| 
									
										
										
										
											2006-02-08 12:53:56 +00:00
										 |  |  |     ticks = 42000 | 
					
						
							| 
									
										
										
										
											2001-10-04 00:58:24 +00:00
										 |  |  |     prof = profile.Profile(timer) | 
					
						
							| 
									
										
										
										
											2006-02-08 12:53:56 +00:00
										 |  |  |     prof.runctx("testfunc()", globals(), locals()) | 
					
						
							|  |  |  |     assert ticks == 43000, ticks | 
					
						
							|  |  |  |     st = pstats.Stats(prof) | 
					
						
							|  |  |  |     st.strip_dirs().sort_stats('stdname').print_stats() | 
					
						
							|  |  |  |     st.print_callees() | 
					
						
							|  |  |  |     st.print_callers() | 
					
						
							| 
									
										
										
										
											2001-10-04 00:58:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def timer(): | 
					
						
							|  |  |  |     return ticks*0.001 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def testfunc(): | 
					
						
							|  |  |  |     # 1 call | 
					
						
							| 
									
										
										
										
											2006-02-08 12:53:56 +00:00
										 |  |  |     # 1000 ticks total: 270 ticks local, 730 ticks in subfunctions | 
					
						
							| 
									
										
										
										
											2001-10-04 00:58:24 +00:00
										 |  |  |     global ticks | 
					
						
							| 
									
										
										
										
											2006-02-08 12:53:56 +00:00
										 |  |  |     ticks += 99 | 
					
						
							| 
									
										
										
										
											2001-10-04 00:58:24 +00:00
										 |  |  |     helper()                            # 300 | 
					
						
							|  |  |  |     helper()                            # 300 | 
					
						
							| 
									
										
										
										
											2006-02-08 12:53:56 +00:00
										 |  |  |     ticks += 171 | 
					
						
							|  |  |  |     factorial(14)                       # 130 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def factorial(n): | 
					
						
							|  |  |  |     # 23 calls total | 
					
						
							|  |  |  |     # 170 ticks total, 150 ticks local | 
					
						
							|  |  |  |     # 3 primitive calls, 130, 20 and 20 ticks total | 
					
						
							|  |  |  |     # including 116, 17, 17 ticks local | 
					
						
							|  |  |  |     global ticks | 
					
						
							|  |  |  |     if n > 0: | 
					
						
							|  |  |  |         ticks += n | 
					
						
							|  |  |  |         return mul(n, factorial(n-1)) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         ticks += 11 | 
					
						
							|  |  |  |         return 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def mul(a, b): | 
					
						
							|  |  |  |     # 20 calls | 
					
						
							|  |  |  |     # 1 tick, local | 
					
						
							|  |  |  |     global ticks | 
					
						
							|  |  |  |     ticks += 1 | 
					
						
							|  |  |  |     return a * b | 
					
						
							| 
									
										
										
										
											2001-10-04 00:58:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def helper(): | 
					
						
							|  |  |  |     # 2 calls | 
					
						
							| 
									
										
										
										
											2006-02-08 12:53:56 +00:00
										 |  |  |     # 300 ticks total: 20 ticks local, 260 ticks in subfunctions | 
					
						
							| 
									
										
										
										
											2001-10-04 00:58:24 +00:00
										 |  |  |     global ticks | 
					
						
							|  |  |  |     ticks += 1 | 
					
						
							|  |  |  |     helper1()                           # 30 | 
					
						
							| 
									
										
										
										
											2006-02-08 12:53:56 +00:00
										 |  |  |     ticks += 2 | 
					
						
							| 
									
										
										
										
											2001-10-04 00:58:24 +00:00
										 |  |  |     helper1()                           # 30 | 
					
						
							|  |  |  |     ticks += 6 | 
					
						
							|  |  |  |     helper2()                           # 50 | 
					
						
							| 
									
										
										
										
											2006-02-08 12:53:56 +00:00
										 |  |  |     ticks += 3 | 
					
						
							| 
									
										
										
										
											2001-10-04 00:58:24 +00:00
										 |  |  |     helper2()                           # 50 | 
					
						
							| 
									
										
										
										
											2006-02-08 12:53:56 +00:00
										 |  |  |     ticks += 2 | 
					
						
							| 
									
										
										
										
											2001-10-04 00:58:24 +00:00
										 |  |  |     helper2()                           # 50 | 
					
						
							| 
									
										
										
										
											2006-02-08 12:53:56 +00:00
										 |  |  |     ticks += 5 | 
					
						
							|  |  |  |     helper2_indirect()                  # 70 | 
					
						
							|  |  |  |     ticks += 1 | 
					
						
							| 
									
										
										
										
											2001-10-04 00:58:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def helper1(): | 
					
						
							|  |  |  |     # 4 calls | 
					
						
							|  |  |  |     # 30 ticks total: 29 ticks local, 1 tick in subfunctions | 
					
						
							|  |  |  |     global ticks | 
					
						
							|  |  |  |     ticks += 10 | 
					
						
							| 
									
										
										
										
											2006-02-08 12:53:56 +00:00
										 |  |  |     hasattr(C(), "foo")                 # 1 | 
					
						
							| 
									
										
										
										
											2001-10-04 00:58:24 +00:00
										 |  |  |     ticks += 19 | 
					
						
							| 
									
										
										
										
											2006-02-08 12:53:56 +00:00
										 |  |  |     lst = [] | 
					
						
							|  |  |  |     lst.append(42)                      # 0 | 
					
						
							|  |  |  |     sys.exc_info()                      # 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def helper2_indirect(): | 
					
						
							|  |  |  |     helper2()                           # 50 | 
					
						
							|  |  |  |     factorial(3)                        # 20 | 
					
						
							| 
									
										
										
										
											2001-10-04 00:58:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def helper2(): | 
					
						
							|  |  |  |     # 8 calls | 
					
						
							|  |  |  |     # 50 ticks local: 39 ticks local, 11 ticks in subfunctions | 
					
						
							|  |  |  |     global ticks | 
					
						
							|  |  |  |     ticks += 11 | 
					
						
							|  |  |  |     hasattr(C(), "bar")                 # 1 | 
					
						
							|  |  |  |     ticks += 13 | 
					
						
							|  |  |  |     subhelper()                         # 10 | 
					
						
							|  |  |  |     ticks += 15 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def subhelper(): | 
					
						
							|  |  |  |     # 8 calls | 
					
						
							|  |  |  |     # 10 ticks total: 8 ticks local, 2 ticks in subfunctions | 
					
						
							|  |  |  |     global ticks | 
					
						
							|  |  |  |     ticks += 2 | 
					
						
							| 
									
										
										
										
											2006-02-08 12:53:56 +00:00
										 |  |  |     for i in range(2):                  # 0 | 
					
						
							| 
									
										
										
										
											2001-10-04 00:58:24 +00:00
										 |  |  |         try: | 
					
						
							|  |  |  |             C().foo                     # 1 x 2 | 
					
						
							|  |  |  |         except AttributeError: | 
					
						
							|  |  |  |             ticks += 3                  # 3 x 2 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class C: | 
					
						
							|  |  |  |     def __getattr__(self, name): | 
					
						
							|  |  |  |         # 28 calls | 
					
						
							|  |  |  |         # 1 tick, local | 
					
						
							|  |  |  |         global ticks | 
					
						
							|  |  |  |         ticks += 1 | 
					
						
							|  |  |  |         raise AttributeError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     test_main() |