|
| 1 | +# test_performance_logger.py |
| 2 | +import unittest |
| 3 | +import os |
| 4 | +import pandas as pd |
| 5 | +from UTILS.perfmonitor.performance_logger import PerformanceLogger, default_plot_config, default_summary_config |
| 6 | + |
| 7 | +class TestPerformanceLogger(unittest.TestCase): |
| 8 | + def setUp(self): |
| 9 | + self.log_path = "test_log.txt" |
| 10 | + self.logger = PerformanceLogger(self.log_path) |
| 11 | + # Ensure the log file is empty before each test |
| 12 | + if os.path.exists(self.log_path): |
| 13 | + os.remove(self.log_path) |
| 14 | + |
| 15 | + def tearDown(self): |
| 16 | + # Clean up the log file after each test |
| 17 | + if os.path.exists(self.log_path): |
| 18 | + os.remove(self.log_path) |
| 19 | + |
| 20 | + def test_log(self): |
| 21 | + self.logger.log("TestStep") |
| 22 | + self.assertTrue(os.path.exists(self.log_path)) |
| 23 | + with open(self.log_path, "r") as f: |
| 24 | + lines = f.readlines() |
| 25 | + self.assertEqual(len(lines), 1) |
| 26 | + self.assertIn("TestStep", lines[0]) |
| 27 | + |
| 28 | + def test_log_to_dataframe(self): |
| 29 | + self.logger.log("TestStep") |
| 30 | + df = PerformanceLogger.log_to_dataframe(self.log_path) |
| 31 | + self.assertEqual(len(df), 1) |
| 32 | + self.assertEqual(df.iloc[0]["step"], "TestStep") |
| 33 | + |
| 34 | + def test_summarize_with_config(self): |
| 35 | + self.logger.log("Step1") |
| 36 | + self.logger.log("Step2") |
| 37 | + df = PerformanceLogger.log_to_dataframe(self.log_path) |
| 38 | + summary = PerformanceLogger.summarize_with_config(df, default_summary_config["summary_by_step"]) |
| 39 | + self.assertIn("elapsed_sec", summary.columns) |
| 40 | + self.assertIn("rss_gb", summary.columns) |
| 41 | + |
| 42 | + def test_plot(self): |
| 43 | + self.logger.log("Step1") |
| 44 | + self.logger.log("Step2") |
| 45 | + df = PerformanceLogger.log_to_dataframe(self.log_path) |
| 46 | + try: |
| 47 | + PerformanceLogger.plot(df, default_plot_config) |
| 48 | + except Exception as e: |
| 49 | + self.fail(f"Plotting failed with exception: {e}") |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + unittest.main() |
| 53 | + |
0 commit comments