Source

saturnalia / src / python / task / dropcounter.py

Full commit
import time

class DropCounter ( object ):

    # This class uses seconds for time measurement.

    def __init__ ( self, logger, reportInterval, description ):
        self._logger         = logger
        self._intervalStart  = time.time()
        self._dropped        = 0
        self._reportInterval = reportInterval
        self._description    = description

    def logReportIfNecessary ( self ):
        delta = time.time() - self._intervalStart
        if delta > self._reportInterval:
            if self._dropped > 0:
                self._logger.info( 'Dropped %r messages from %s in the last %.2f seconds ( %.2f messages / second )', self._dropped, self._description, delta, float( self._dropped ) / delta )

            self._intervalStart = time.time()
            self._dropped       = 0

    def recordDropEvent ( self ):
        self._dropped += 1