From 0617833973ccbab14367a190ba9223a577252797 Mon Sep 17 00:00:00 2001 From: derekpierre Date: Tue, 5 Nov 2024 14:33:51 -0500 Subject: [PATCH] Add unit test for moving average window. --- tests/unit/test_node_latency_collector.py | 36 ++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_node_latency_collector.py b/tests/unit/test_node_latency_collector.py index 9c1514800..ed303918b 100644 --- a/tests/unit/test_node_latency_collector.py +++ b/tests/unit/test_node_latency_collector.py @@ -26,7 +26,7 @@ def execution_data(get_random_checksum_address): executions[node_3] = node_3_exec_times node_4 = get_random_checksum_address() - node_4_exec_times = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8] + node_4_exec_times = [0.1, 0.2, 0.3, 0.4, 0.5] executions[node_4] = node_4_exec_times sorted_order = sorted( @@ -103,6 +103,40 @@ def test_collector_stats_obtained(execution_data): ) +@pytest.mark.parametrize( + "max_window", [NodeLatencyStatsCollector.MAX_MOVING_AVERAGE_WINDOW, 3, 7, 15] +) +def test_collector_moving_average_window(max_window, get_random_checksum_address): + node_collector_stats = NodeLatencyStatsCollector( + max_moving_average_window=max_window + ) + node = get_random_checksum_address() + exec_times = [] + + # <= moving average window + for i in range(max_window): + value = random.uniform(0, 40) + exec_times.append(value) + node_collector_stats._update_stats(node, value) + # all values available used + assert floats_sufficiently_equal( + node_collector_stats.get_average_latency_time(node), + sum(exec_times) / len(exec_times), + ) + + # > moving average window + for i in range(max_window * 2): + value = random.uniform(0, 40) + exec_times.append(value) + node_collector_stats._update_stats(node, value) + + # only the latest "max_window" values are used for the average, even though additional values were collected + assert floats_sufficiently_equal( + node_collector_stats.get_average_latency_time(node), + sum(exec_times[-max_window:]) / len(exec_times[-max_window:]), + ) + + def test_collector_stats_reset(execution_data): executions, original_expected_node_sorted_order = execution_data node_latency_collector = NodeLatencyStatsCollector()