Package Products :: Package ZenUtils :: Module debugtools
[hide private]
[frames] | no frames]

Source Code for Module Products.ZenUtils.debugtools

 1  ############################################################################## 
 2  #  
 3  # Copyright (C) Zenoss, Inc. 2007, all rights reserved. 
 4  #  
 5  # This content is made available according to terms specified in 
 6  # License.zenoss under the directory where your Zenoss product is installed. 
 7  #  
 8  ############################################################################## 
 9   
10   
11  import os 
12  import hotshot 
13  import hotshot.stats 
14  import tempfile 
15   
16 -class Profiler(object):
17
18 - def __init__(self):
19 self.fname = tempfile.mktemp() 20 self.profiler = hotshot.Profile(self.fname, True)
21
22 - def print_stats(self, limit=20):
23 stats = hotshot.stats.load(self.fname) 24 stats.sort_stats('time', 'calls') 25 stats.print_stats(limit)
26
27 - def runcall(self, *args, **kwargs):
28 result = self.profiler.runcall(*args, **kwargs) 29 self.profiler.close() 30 return result
31
32 - def __del__(self):
33 os.remove(self.fname)
34 35
36 -def profile(f):
37 """ 38 Decorator that will profile a function and print stats. 39 """ 40 def inner(*args, **kwargs): 41 p = Profiler() 42 result = p.runcall(f, *args, **kwargs) 43 p.print_stats() 44 return result
45 return inner 46