Skip to content
Snippets Groups Projects
Commit c3071422 authored by Honza Mach's avatar Honza Mach
Browse files

Added support for statistics calculation for nested statistics.

parent be8d9767
Branches
Tags
No related merge requests found
...@@ -55,5 +55,24 @@ class TestPyzenkitZenDaemon(unittest.TestCase): ...@@ -55,5 +55,24 @@ class TestPyzenkitZenDaemon(unittest.TestCase):
""" """
self.assertTrue(True) 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__": if __name__ == "__main__":
unittest.main() unittest.main()
...@@ -217,6 +217,31 @@ class EventQueueManager: ...@@ -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): class ZenDaemonComponentException(pyzenkit.baseapp.ZenAppProcessException):
""" """
Describes problems specific to daemon components. Describes problems specific to daemon components.
...@@ -260,25 +285,6 @@ class ZenDaemonComponent: ...@@ -260,25 +285,6 @@ class ZenDaemonComponent:
'statistics': self.statistics_cur '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): def get_statistics(self):
""" """
Calculate processing statistics Calculate processing statistics
...@@ -286,7 +292,7 @@ class ZenDaemonComponent: ...@@ -286,7 +292,7 @@ class ZenDaemonComponent:
curts = time.time() curts = time.time()
tdiff = curts - self.statistics_ts 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_prev = copy.copy(self.statistics_cur)
self.statistics_ts = curts self.statistics_ts = curts
...@@ -940,6 +946,7 @@ class DemoDaemonComponent(ZenDaemonComponent): ...@@ -940,6 +946,7 @@ class DemoDaemonComponent(ZenDaemonComponent):
""" """
daemon.queue.schedule('default') daemon.queue.schedule('default')
daemon.logger.info("Working") daemon.logger.info("Working")
self.inc_statistic('cnt_default')
time.sleep(1) time.sleep(1)
return (daemon.FLAG_CONTINUE, None) return (daemon.FLAG_CONTINUE, None)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment