for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
# -*- coding: utf-8 -*-
"""
.. moduleauthor:: Kenny Freeman <[email protected]>
__author__ = 'Kenny Freeman'
__email__ = '[email protected]'
__version__ = '0.3.3'
__license__ = "ISCL"
__docformat__ = 'reStructuredText'
import platform
# use CNT_LIMIT to detect counters wrapping over
arch = platform.architecture()[0] # eg. ('64bit', 'ELF')
if arch == '64bit':
MAX_CNT = 18446744073709551615L
elif arch == '32bit':
MAX_CNT = 4294967295
else:
MAX_CNT = sys.maxint
class Differential(object):
"""Computes the rate of change of a set of metrics."""
def __init__(self):
self.metrics = {}
def per_second(self, name, val, ts):
"""Records and returns the rate of change of a metric in units/second.
Assumes values will be < MAX_CNT and detects and corrects overflow.
:param name: The name of a metric
:type name: str
:param val: The value of a metric
:type val: int or float
:param max: The maximum possible value for val
:rtype: int or float
ret = type(val)(0)
if name not in self.metrics:
self.metrics[name] = (val, ts)
# get the previous value
pval, ptime = self.metrics[name]
# check for counter wrap
if val < pval:
pval = pval - MAX_CNT
dval = val - pval
dtime = ts - ptime
if dtime > 0:
ret = float(dval) / float(dtime)
ret = 0
return ret