performance.py

Performance Monitoring - System Optimization and Resource Management

LOCATION: lunaengine/utils/performance.py

DESCRIPTION:
Comprehensive performance monitoring system that tracks frame rates,
system resources, and provides optimization utilities. Includes hardware
detection, garbage collection, and performance statistics.

KEY COMPONENTS:
- PerformanceMonitor: Real-time FPS tracking and hardware monitoring
- GarbageCollector: Automatic resource cleanup and memory management
- Hardware detection for system-specific optimizations
- Frame time analysis and performance statistics

LIBRARIES USED:
- psutil: System resource monitoring (CPU, memory)
- pygame: Version detection and integration
- platform: System information and platform detection
- time: Precise timing measurements
- threading: Background monitoring capabilities
- collections: Efficient data structures for performance tracking

USAGE:
>>> monitor = PerformanceMonitor()
>>> stats = monitor.get_stats()
>>> hardware = monitor.get_hardware_info()
>>> gc = GarbageCollector()
>>> gc.cleanup()

class TimeProfile

Description

No documentation

Attributes
end: Any = stop
Methods
def __init__(self: Any, category: str = '', max_history: int = 60) -> Any
No documentation
def start(self: Any) -> Any
No documentation
def stop(self: Any) -> Any
No documentation
def _insert_new_history(self: Any) -> Any
No documentation
def duration(self: Any) -> float
No documentation
def record_current(self: Any) -> Any
Record the current duration into history.

class PerformanceProfiler

Description

Performance profiling with detailed timing breakdown

Methods
def __init__(self: Any, max_history: int = 100) -> Any
No documentation
def enable(self: Any, enabled: bool = True) -> Any
No documentation
def disable(self: Any) -> Any
No documentation
def is_enabled(self: Any) -> bool
No documentation
def get_timer(self: Any, category: str) -> TimeProfile
No documentation
def get_all_timers(self: Any) -> Dict[str, TimeProfile]
No documentation
def get_timers_list(self: Any) -> List[TimeProfile]
No documentation
def start_timer(self: Any, category: str) -> Any
No documentation
def stop_timer(self: Any, category: str) -> Any
No documentation
def create_timer(self: Any, category: str) -> Any
No documentation
def begin_frame(self: Any) -> Any
Begin a new frame of profiling
def end_frame(self: Any) -> Any
End current frame: record all used timers and store durations.
def get_frame_timings(self: Any) -> Dict[str, float]
Get durations (ms) of the last completed frame.
def get_timing_stats(self: Any, category: Literal['update', 'render']) -> Dict[str, Any]
No documentation

class PerformanceMonitor

Description

Optimized performance monitoring with minimal overhead

Methods
def __init__(self: Any, history_size: int = 300) -> Any
No documentation
def get_frame_timing_breakdown(self: Any) -> Dict[str, float]
Convenience: get last frame's timing per category.
def get_performance_summary(self: Any) -> Dict[str, Any]
Comprehensive summary including FPS and last frame timings.
def enable_profiling(self: Any, enabled: bool = True) -> Any
Enable or disable detailed performance profiling
def is_profiling_enabled(self: Any) -> bool
Check if detailed profiling is enabled
def create_timer(self: Any, category: str) -> Any
No documentation
def start_timer(self: Any, category: str) -> Any
No documentation
def end_timer(self: Any, category: str) -> Any
No documentation
def get_all_timers(self: Any) -> Dict[str, TimeProfile]
No documentation
def get_list_timers(self: Any) -> List[TimeProfile]
No documentation
def timers_names(self: Any) -> List[str]
No documentation
def get_update_timing_stats(self: Any) -> Dict[str, Any]
Get update timing statistics
def get_render_timing_stats(self: Any) -> Dict[str, Any]
Get render timing statistics
def get_performance_summary(self: Any) -> Dict[str, Any]
Get comprehensive performance summary
def get_hardware_info(self: Any) -> Dict[str, str]
Get system hardware information with caching
def update_frame(self: Any) -> Any
Update frame timing - FIXED VERSION
def end_frame(self: Any) -> Any
End the current frame and record profiling data
def get_stats(self: Any) -> Dict[str, float]
Get FPS statistics with optimized calculations - FIXED
def _get_empty_stats(self: Any) -> Dict[str, float]
Return empty stats structure - FIXED

class GarbageCollector

Description

Manages cleanup of unused resources

Methods
def __init__(self: Any) -> Any
No documentation
def mark_font_unused(self: Any, font: Any) -> Any
Mark a font as potentially unused
def mark_surface_unused(self: Any, surface: Any) -> Any
Mark a surface as potentially unused
def cleanup(self: Any, force: bool = False) -> Any
Clean up unused resources
Back to Utils Module