From 22488407d2f30e9044f5d54af3c3003f31f08ed7 Mon Sep 17 00:00:00 2001 From: Mika Date: Tue, 13 Jan 2026 11:17:04 +0000 Subject: [PATCH] Add trace_analysis/tests/test_core.py --- trace_analysis/tests/test_core.py | 48 +++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 trace_analysis/tests/test_core.py diff --git a/trace_analysis/tests/test_core.py b/trace_analysis/tests/test_core.py new file mode 100644 index 0000000..5531cb7 --- /dev/null +++ b/trace_analysis/tests/test_core.py @@ -0,0 +1,48 @@ +import pytest +from trace_analysis import core + + +def test_analyze_trace_basic_case(): + trace_data = { + "events": [ + {"timestamp": 0.0, "cpu_path": "CPU0", "reorder_score": 0.1, "mixed_snapshot_signature_count": 2}, + {"timestamp": 0.1, "cpu_path": "CPU1", "reorder_score": 0.3, "mixed_snapshot_signature_count": 1}, + {"timestamp": 0.2, "cpu_path": "CPU1", "reorder_score": 0.4, "mixed_snapshot_signature_count": 0}, + {"timestamp": 0.3, "cpu_path": "CPU0", "reorder_score": 0.2, "mixed_snapshot_signature_count": 3} + ] + } + + result = core.analyze_trace(trace_data) + + assert isinstance(result, dict) + assert "cpu_migrations" in result + assert result["cpu_migrations"] > 0 + assert "avg_reorder_score" in result + assert result["avg_reorder_score"] == pytest.approx(0.25, 0.05) + assert "anomaly_count" in result + assert isinstance(result["anomaly_count"], int) + + +def test_analyze_trace_empty_data(): + trace_data = {"events": []} + result = core.analyze_trace(trace_data) + + assert result["cpu_migrations"] == 0 + assert result["avg_reorder_score"] == 0.0 + assert result["anomaly_count"] == 0 + + +def test_analyze_trace_detection_of_anomalies(): + trace_data = { + "events": [ + {"timestamp": 0.0, "cpu_path": "CPU0", "reorder_score": 0.9, "mixed_snapshot_signature_count": 6}, + {"timestamp": 0.1, "cpu_path": "CPU1", "reorder_score": 0.8, "mixed_snapshot_signature_count": 7}, + {"timestamp": 0.2, "cpu_path": "CPU2", "reorder_score": 0.2, "mixed_snapshot_signature_count": 8} + ] + } + + result = core.analyze_trace(trace_data) + + assert result["cpu_migrations"] >= 2 + assert result["avg_reorder_score"] > 0.0 + assert result["anomaly_count"] >= 1 \ No newline at end of file