#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 屏幕录制性能测试脚本 用于测试屏幕录制的CPU优化功能 """ import time import logging import threading from devices.screen_recorder import RecordingManager # 配置日志 logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' ) def test_screen_recording_performance(): """ 测试屏幕录制性能优化功能 """ print("开始屏幕录制性能测试...") # 创建录制管理器 recording_manager = RecordingManager() # 配置性能参数(更严格的阈值用于测试) recording_manager.configure_performance_settings( cpu_threshold=70.0, # 降低CPU阈值以便测试 memory_threshold=80.0, adaptive_fps=True, min_fps=10, max_fps=30 ) try: # 启动屏幕录制 print("启动屏幕录制...") result = recording_manager.start_recording( session_id="test_session_001", patient_id="test_patient", screen_location=[0, 0, 1920, 1080], # 全屏录制 camera_location=[0, 0, 640, 480], # 默认相机区域 femtobolt_location=[0, 0, 640, 480], # 默认FemtoBolt区域 recording_types=["screen"] # 只录制屏幕 ) if not result['success']: print(f"录制启动失败: {result['message']}") return print("录制已启动,开始性能监控...") # 监控性能状态 def monitor_performance(): for i in range(30): # 监控30秒 time.sleep(1) status = recording_manager.get_status() if status['recording']: perf = status['performance'] print(f"[{i+1:2d}s] CPU: {perf['cpu_percent']:.1f}% | " f"内存: {perf['memory_percent']:.1f}% | " f"当前帧率: {status['current_fps']:.1f}fps | " f"跳帧: {status['frame_skip_count']}") # 如果CPU或内存超过阈值,显示警告 if perf['cpu_percent'] > perf['cpu_threshold']: print(f" ⚠️ CPU使用率超过阈值 ({perf['cpu_threshold']}%)") if perf['memory_percent'] > perf['memory_threshold']: print(f" ⚠️ 内存使用率超过阈值 ({perf['memory_threshold']}%)") else: print("录制已停止") break # 在单独线程中监控性能 monitor_thread = threading.Thread(target=monitor_performance) monitor_thread.start() # 等待监控完成 monitor_thread.join() except KeyboardInterrupt: print("\n用户中断测试") except Exception as e: print(f"测试过程中发生错误: {e}") finally: # 停止录制 print("停止录制...") stop_result = recording_manager.stop_recording() if stop_result['success']: print(f"录制已停止,视频文件: {stop_result.get('video_files', [])}") else: print(f"停止录制失败: {stop_result['message']}") def test_performance_configuration(): """ 测试性能配置功能 """ print("\n测试性能配置功能...") recording_manager = RecordingManager() # 测试各种配置 print("测试CPU阈值配置:") recording_manager.configure_performance_settings(cpu_threshold=60) recording_manager.configure_performance_settings(cpu_threshold=40) # 应该被限制到50 recording_manager.configure_performance_settings(cpu_threshold=100) # 应该被限制到95 print("\n测试帧率配置:") recording_manager.configure_performance_settings(min_fps=15, max_fps=25) recording_manager.configure_performance_settings(min_fps=35, max_fps=20) # min > max,应该调整 print("\n当前状态:") status = recording_manager.get_status() perf = status['performance'] print(f"CPU阈值: {perf['cpu_threshold']}%") print(f"内存阈值: {perf['memory_threshold']}%") print(f"自适应帧率: {status['adaptive_fps_enabled']}") print(f"当前帧率: {status['current_fps']}fps") if __name__ == "__main__": print("屏幕录制性能测试") print("=" * 50) # 测试配置功能 test_performance_configuration() # 询问是否进行实际录制测试 response = input("\n是否进行实际录制测试?(y/n): ") if response.lower() in ['y', 'yes', '是']: test_screen_recording_performance() else: print("跳过录制测试") print("\n测试完成")