#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 截图性能测试脚本 测试截图性能并查看细粒度计时日志,验证优化效果。 """ import os import sys import time import logging # 配置日志 - 使用 DEBUG 级别以查看所有详细计时信息 logging.basicConfig( level=logging.DEBUG, format='[%(levelname)s] %(message)s', handlers=[ logging.StreamHandler(sys.stdout) ] ) def test_screenshot_with_timing(): """测试截图并显示计时信息""" try: from Underlying_Operations import underlying_operations except ImportError: print("[错误] 无法导入 Underlying_Operations") return # 获取设备ID import subprocess try: result = subprocess.run(["adb", "devices"], capture_output=True, text=True, timeout=10) if result.returncode != 0: print("[错误] adb devices 失败") return lines = result.stdout.strip().split("\n")[1:] devices = [] for line in lines: if line.strip() and "\t" in line: device_id = line.split("\t")[0].strip() if device_id: devices.append(device_id) if not devices: print("[错误] 未找到连接的设备") return device_id = devices[0] print(f"[信息] 使用设备: {device_id}") except Exception as e: print(f"[错误] 获取设备ID失败: {e}") return # 创建操作对象 ops = underlying_operations() print("\n" + "="*60) print("测试 1: 正常截图(启用所有验证和镜像)") print("="*60) # 测试1: 正常截图 start = time.time() result1 = ops.get_screenshot(device_id, use_service=False) # 禁用服务,使用直接截图以便看到详细日志 elapsed1 = (time.time() - start) * 1000.0 print(f"\n[结果] 测试1完成: 成功={result1}, 总耗时={elapsed1:.1f}ms") time.sleep(1) print("\n" + "="*60) print("测试 2: 禁用 PNG 验证 (SKIP_SCREENSHOT_VERIFY=1)") print("="*60) # 测试2: 禁用验证 os.environ["SKIP_SCREENSHOT_VERIFY"] = "1" start = time.time() result2 = ops.get_screenshot(device_id, use_service=False) elapsed2 = (time.time() - start) * 1000.0 print(f"\n[结果] 测试2完成: 成功={result2}, 总耗时={elapsed2:.1f}ms") if elapsed1 > 0: improvement = ((elapsed1 - elapsed2) / elapsed1) * 100 print(f"[对比] 相比测试1: {improvement:+.1f}% ({elapsed1 - elapsed2:+.1f}ms)") time.sleep(1) print("\n" + "="*60) print("测试 3: 禁用 PNG 验证 + 禁用镜像 (SKIP_SCREENSHOT_VERIFY=1, DISABLE_SCREENSHOT_MIRROR=1)") print("="*60) # 测试3: 禁用验证和镜像 os.environ["DISABLE_SCREENSHOT_MIRROR"] = "1" start = time.time() result3 = ops.get_screenshot(device_id, use_service=False) elapsed3 = (time.time() - start) * 1000.0 print(f"\n[结果] 测试3完成: 成功={result3}, 总耗时={elapsed3:.1f}ms") if elapsed1 > 0: improvement = ((elapsed1 - elapsed3) / elapsed1) * 100 print(f"[对比] 相比测试1: {improvement:+.1f}% ({elapsed1 - elapsed3:+.1f}ms)") # 恢复环境变量 if "SKIP_SCREENSHOT_VERIFY" in os.environ: del os.environ["SKIP_SCREENSHOT_VERIFY"] if "DISABLE_SCREENSHOT_MIRROR" in os.environ: del os.environ["DISABLE_SCREENSHOT_MIRROR"] print("\n" + "="*60) print("测试总结") print("="*60) print(f"测试1 (正常): {elapsed1:.1f}ms") print(f"测试2 (禁用验证): {elapsed2:.1f}ms") print(f"测试3 (禁用全部): {elapsed3:.1f}ms") if elapsed1 > 0 and elapsed3 > 0: total_improvement = ((elapsed1 - elapsed3) / elapsed1) * 100 print(f"\n最大优化效果: {total_improvement:+.1f}% ({elapsed1 - elapsed3:+.1f}ms)") if total_improvement > 10: print("\n[建议] 如果优化效果明显,可以考虑:") print(" 1. 在生产环境谨慎使用 SKIP_SCREENSHOT_VERIFY(可能影响数据完整性)") print(" 2. 如果不需要外部镜像,可以设置 DISABLE_SCREENSHOT_MIRROR=1") print(" 3. 检查外部镜像路径是否是网络路径(可能很慢)") if __name__ == "__main__": test_screenshot_with_timing()