diff --git a/pyzenkit/tests/test_zendaemon.py b/pyzenkit/tests/test_zendaemon.py index ec568002ea395ac6c734ad72ac276053e6146d1b..db6743f8ef60913db873a26b632072e4b73de353 100644 --- a/pyzenkit/tests/test_zendaemon.py +++ b/pyzenkit/tests/test_zendaemon.py @@ -55,5 +55,24 @@ class TestPyzenkitZenDaemon(unittest.TestCase): """ self.assertTrue(True) + def test_02_calc_statistics(self): + """ + Perform the test of statistics calculation. + """ + self.maxDiff = None + + result = pyzenkit.zendaemon.calc_statistics( + {'cnt_test_a1': 50, 'cnt_test_a2': 100, 'sub': {'cnt_test_b1': 500, 'cnt_test_b2': 1000}}, + {}, + 50 + ) + self.assertEqual(result, { + 'cnt_test_a1': {'cnt': 50, 'inc': 50, 'pct': 100.0, 'spd': 1.0}, + 'cnt_test_a2': {'cnt': 100, 'inc': 100, 'pct': 100.0, 'spd': 2.0}, + 'sub': { + 'cnt_test_b1': {'cnt': 500, 'inc': 500, 'pct': 100.0, 'spd': 10.0}, + 'cnt_test_b2': {'cnt': 1000, 'inc': 1000, 'pct': 100.0, 'spd': 20.0}}} + ) + if __name__ == "__main__": unittest.main() diff --git a/pyzenkit/zendaemon.py b/pyzenkit/zendaemon.py index 5e46dd9b1cbe14306c571bdc6e3120c10e031ebc..eae668c8d197f00e601cbe956326b57143a55179 100644 --- a/pyzenkit/zendaemon.py +++ b/pyzenkit/zendaemon.py @@ -217,6 +217,31 @@ class EventQueueManager: #------------------------------------------------------------------------------- +def calc_statistics(stats_cur, stats_prev, tdiff): + """ + Calculate statistics. + """ + result = {} + for key in stats_cur: + if isinstance(stats_cur[key], dict): + result[key] = calc_statistics(stats_cur[key], stats_prev.get(key, {}), tdiff) + else: + result[key] = { + # Absolute count. + 'cnt': stats_cur[key], + # Increase count (delta from previous value). + 'inc': stats_cur[key] - stats_prev.get(key, 0), + # Processing speed (#/s). + 'spd': (stats_cur[key] - stats_prev.get(key, 0)) / tdiff, + # Percentage increase count. + 'pct': (stats_cur[key] - stats_prev.get(key, 0)) / (stats_cur[key] / 100) + } + return result + + +#------------------------------------------------------------------------------- + + class ZenDaemonComponentException(pyzenkit.baseapp.ZenAppProcessException): """ Describes problems specific to daemon components. @@ -260,25 +285,6 @@ class ZenDaemonComponent: 'statistics': self.statistics_cur } - @staticmethod - def calc_statistics(stats_cur, stats_prev, tdiff): - """ - Calculate daemon component statistics. - """ - result = {} - for key in stats_cur: - result[key] = { - # Absolute count. - 'cnt': stats_cur[key], - # Increase from previous value. - 'inc': stats_cur[key] - stats_prev.get(key, 0), - # Processing speed (#/s) - 'spd': (stats_cur[key] - stats_prev.get(key, 0)) / tdiff, - # Percentage increase. - 'pct': (stats_cur[key] - stats_prev.get(key, 0)) / (stats_cur[key] / 100) - } - return result - def get_statistics(self): """ Calculate processing statistics @@ -286,7 +292,7 @@ class ZenDaemonComponent: curts = time.time() tdiff = curts - self.statistics_ts - stats = self.calc_statistics(self.statistics_cur, self.statistics_prev, tdiff) + stats = calc_statistics(self.statistics_cur, self.statistics_prev, tdiff) self.statistics_prev = copy.copy(self.statistics_cur) self.statistics_ts = curts @@ -940,6 +946,7 @@ class DemoDaemonComponent(ZenDaemonComponent): """ daemon.queue.schedule('default') daemon.logger.info("Working") + self.inc_statistic('cnt_default') time.sleep(1) return (daemon.FLAG_CONTINUE, None)