Files
manual_slop/tests/test_performance_monitor.py

52 lines
1.8 KiB
Python

import unittest
import time
from unittest.mock import MagicMock
from performance_monitor import PerformanceMonitor
class TestPerformanceMonitor(unittest.TestCase):
def setUp(self):
self.monitor = PerformanceMonitor()
def test_frame_time_collection(self):
# Simulate frames for 1.1 seconds to trigger FPS calculation
start = time.time()
while time.time() - start < 1.1:
self.monitor.start_frame()
time.sleep(0.01) # ~100 FPS
self.monitor.end_frame()
metrics = self.monitor.get_metrics()
self.assertAlmostEqual(metrics['last_frame_time_ms'], 10, delta=10)
self.assertGreater(metrics['fps'], 0)
def test_cpu_usage_collection(self):
metrics = self.monitor.get_metrics()
self.assertIn('cpu_percent', metrics)
self.assertIsInstance(metrics['cpu_percent'], float)
def test_input_lag_collection(self):
self.monitor.start_frame()
self.monitor.record_input_event()
time.sleep(0.02) # 20ms lag
self.monitor.end_frame()
metrics = self.monitor.get_metrics()
self.assertGreaterEqual(metrics['input_lag_ms'], 20)
self.assertLess(metrics['input_lag_ms'], 40)
def test_alerts_triggering(self):
mock_callback = MagicMock()
self.monitor.alert_callback = mock_callback
self.monitor.thresholds['frame_time_ms'] = 5.0 # Low threshold
self.monitor._alert_cooldown = 0 # No cooldown for test
self.monitor.start_frame()
time.sleep(0.01) # 10ms > 5ms
self.monitor.end_frame()
mock_callback.assert_called_once()
self.assertIn("Frame time high", mock_callback.call_args[0][0])
if __name__ == '__main__':
unittest.main()