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

# Source Code for Module Products.ZenUtils.debugtools

     1  ###########################################################################
     2  #
     3  # This program is part of Zenoss Core, an open source monitoring platform.
     4  # Copyright (C) 2007, Zenoss Inc.
     5  #
     6  # This program is free software; you can redistribute it and/or modify it
     7  # under the terms of the GNU General Public License version 2 or (at your
     8  # option) any later version as published by the Free Software Foundation.
     9  #
    10  # For complete information please visit: http://www.zenoss.com/oss/
    11  #
    12  ###########################################################################
    13  import os
    14  import hotshot
    15  import hotshot.stats
    16  import tempfile
    17
    18 -class Profiler(object):
    19
    20 -    def __init__(self):
    21          self.fname = tempfile.mktemp()
    22          self.profiler = hotshot.Profile(self.fname, True)
    23
    24 -    def print_stats(self, limit=20):
    25          stats = hotshot.stats.load(self.fname)
    26          stats.sort_stats('time', 'calls')
    27          stats.print_stats(limit)
    28
    29 -    def runcall(self, *args, **kwargs):
    30          result = self.profiler.runcall(*args, **kwargs)
    31          self.profiler.close()
    32          return result
    33
    34 -    def __del__(self):
    35          os.remove(self.fname)
    36
    37
    38 -def profile(f):
    39      """
    40      Decorator that will profile a function and print stats.
    41      """
    42      def inner(*args, **kwargs):
    43          p = Profiler()
    44          result = p.runcall(f, *args, **kwargs)
    45          p.print_stats()
    46          return result
    47      return inner
    48