chore: 删除已废弃的 deleted_tests_backup 目录
22 个备份测试文件已废弃多月,从代码库中移除以减少干扰。 如需历史,仍可从 git 历史中恢复。 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6b22939c7e
commit
92753c51a5
22 files changed
-3615
No files matched your search
@@ -1,222 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
ADB 截图基准测试脚本
|
||||
|
||||
用于测试 ADB exec-out screencap 的实际耗时,帮助诊断截图慢的问题。
|
||||
|
||||
使用方法:
|
||||
1. 确保设备已连接: adb devices
|
||||
2. 运行: python test_adb_screenshot_timing.py [device_id]
|
||||
如果不提供 device_id,将使用第一个设备
|
||||
"""
|
||||
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import os
|
||||
from typing import Optional
|
||||
|
||||
|
||||
def get_adb_path() -> str:
|
||||
"""获取 ADB 路径"""
|
||||
# 尝试从环境变量获取
|
||||
adb = os.environ.get("ADB_PATH", "adb")
|
||||
# 尝试常见路径
|
||||
if not os.path.exists(adb):
|
||||
common_paths = [
|
||||
"adb",
|
||||
r"C:\Users\{}\AppData\Local\Android\Sdk\platform-tools\adb.exe".format(os.environ.get("USERNAME", "")),
|
||||
r"C:\Android\platform-tools\adb.exe",
|
||||
]
|
||||
for path in common_paths:
|
||||
try:
|
||||
result = subprocess.run([path, "version"], capture_output=True, timeout=5)
|
||||
if result.returncode == 0:
|
||||
return path
|
||||
except Exception:
|
||||
continue
|
||||
return adb
|
||||
|
||||
|
||||
def get_device_id() -> Optional[str]:
|
||||
"""获取第一个连接的设备ID"""
|
||||
adb = get_adb_path()
|
||||
try:
|
||||
result = subprocess.run([adb, "devices"], capture_output=True, text=True, timeout=10)
|
||||
if result.returncode != 0:
|
||||
print(f"[错误] adb devices 失败: {result.stderr}")
|
||||
return None
|
||||
|
||||
lines = result.stdout.strip().split("\n")[1:] # 跳过第一行 "List of devices attached"
|
||||
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 None
|
||||
|
||||
if len(devices) > 1:
|
||||
print(f"[提示] 找到多个设备: {devices},使用第一个: {devices[0]}")
|
||||
|
||||
return devices[0]
|
||||
except Exception as e:
|
||||
print(f"[错误] 获取设备列表失败: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def test_exec_out_screencap(device_id: str, iterations: int = 5) -> None:
|
||||
"""测试 exec-out screencap 耗时"""
|
||||
adb = get_adb_path()
|
||||
print(f"\n{'='*60}")
|
||||
print(f"测试 ADB exec-out screencap")
|
||||
print(f"设备: {device_id}")
|
||||
print(f"迭代次数: {iterations}")
|
||||
print(f"{'='*60}\n")
|
||||
|
||||
times = []
|
||||
sizes = []
|
||||
|
||||
for i in range(iterations):
|
||||
try:
|
||||
start = time.time()
|
||||
result = subprocess.run(
|
||||
[adb, "-s", device_id, "exec-out", "screencap", "-p"],
|
||||
capture_output=True,
|
||||
timeout=15,
|
||||
)
|
||||
elapsed = (time.time() - start) * 1000.0 # 转换为毫秒
|
||||
|
||||
if result.returncode == 0:
|
||||
size = len(result.stdout)
|
||||
times.append(elapsed)
|
||||
sizes.append(size)
|
||||
print(f" 迭代 {i+1}: {elapsed:.1f}ms, 大小: {size/1024:.1f}KB")
|
||||
else:
|
||||
print(f" 迭代 {i+1}: 失败 (返回码: {result.returncode})")
|
||||
except subprocess.TimeoutExpired:
|
||||
print(f" 迭代 {i+1}: 超时 (>15秒)")
|
||||
except Exception as e:
|
||||
print(f" 迭代 {i+1}: 异常: {e}")
|
||||
|
||||
if times:
|
||||
avg_time = sum(times) / len(times)
|
||||
min_time = min(times)
|
||||
max_time = max(times)
|
||||
avg_size = sum(sizes) / len(sizes) if sizes else 0
|
||||
|
||||
print(f"\n{'='*60}")
|
||||
print(f"统计结果:")
|
||||
print(f" 平均耗时: {avg_time:.1f}ms")
|
||||
print(f" 最小耗时: {min_time:.1f}ms")
|
||||
print(f" 最大耗时: {max_time:.1f}ms")
|
||||
print(f" 平均大小: {avg_size/1024:.1f}KB")
|
||||
print(f"{'='*60}\n")
|
||||
|
||||
if avg_time > 3000:
|
||||
print("[警告] 平均耗时超过3秒,可能存在问题:")
|
||||
print(" - 检查 USB 连接是否稳定")
|
||||
print(" - 检查是否使用 Wi-Fi ADB(通常较慢)")
|
||||
print(" - 检查设备性能是否正常")
|
||||
elif avg_time > 2000:
|
||||
print("[提示] 平均耗时超过2秒,建议:")
|
||||
print(" - 考虑使用 USB 连接而非 Wi-Fi")
|
||||
print(" - 检查设备性能")
|
||||
|
||||
|
||||
def test_shell_screencap_pull(device_id: str, iterations: int = 3) -> None:
|
||||
"""测试 shell screencap + pull 耗时(回退方案)"""
|
||||
adb = get_adb_path()
|
||||
print(f"\n{'='*60}")
|
||||
print(f"测试 ADB shell screencap + pull (回退方案)")
|
||||
print(f"设备: {device_id}")
|
||||
print(f"迭代次数: {iterations}")
|
||||
print(f"{'='*60}\n")
|
||||
|
||||
times = []
|
||||
|
||||
for i in range(iterations):
|
||||
try:
|
||||
# 清理旧文件
|
||||
subprocess.run([adb, "-s", device_id, "shell", "rm", "/sdcard/DCIM/screenshot.png"],
|
||||
capture_output=True, timeout=5)
|
||||
|
||||
start = time.time()
|
||||
|
||||
# Step 1: shell screencap
|
||||
step1_start = time.time()
|
||||
result1 = subprocess.run(
|
||||
[adb, "-s", device_id, "shell", "screencap", "-p", "/sdcard/DCIM/screenshot.png"],
|
||||
capture_output=True,
|
||||
timeout=10,
|
||||
)
|
||||
step1_dur = (time.time() - step1_start) * 1000.0
|
||||
|
||||
if result1.returncode != 0:
|
||||
print(f" 迭代 {i+1}: shell screencap 失败")
|
||||
continue
|
||||
|
||||
# Step 2: pull
|
||||
step2_start = time.time()
|
||||
result2 = subprocess.run(
|
||||
[adb, "-s", device_id, "pull", "/sdcard/DCIM/screenshot.png", "test_screenshot.png"],
|
||||
capture_output=True,
|
||||
timeout=10,
|
||||
)
|
||||
step2_dur = (time.time() - step2_start) * 1000.0
|
||||
|
||||
elapsed = (time.time() - start) * 1000.0
|
||||
|
||||
if result2.returncode == 0 and os.path.exists("test_screenshot.png"):
|
||||
size = os.path.getsize("test_screenshot.png")
|
||||
times.append(elapsed)
|
||||
print(f" 迭代 {i+1}: 总耗时 {elapsed:.1f}ms (screencap: {step1_dur:.1f}ms, pull: {step2_dur:.1f}ms), 大小: {size/1024:.1f}KB")
|
||||
try:
|
||||
os.remove("test_screenshot.png")
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
print(f" 迭代 {i+1}: pull 失败")
|
||||
except Exception as e:
|
||||
print(f" 迭代 {i+1}: 异常: {e}")
|
||||
|
||||
if times:
|
||||
avg_time = sum(times) / len(times)
|
||||
min_time = min(times)
|
||||
max_time = max(times)
|
||||
|
||||
print(f"\n{'='*60}")
|
||||
print(f"统计结果 (回退方案):")
|
||||
print(f" 平均耗时: {avg_time:.1f}ms")
|
||||
print(f" 最小耗时: {min_time:.1f}ms")
|
||||
print(f" 最大耗时: {max_time:.1f}ms")
|
||||
print(f"{'='*60}\n")
|
||||
|
||||
|
||||
def main():
|
||||
device_id = None
|
||||
if len(sys.argv) > 1:
|
||||
device_id = sys.argv[1]
|
||||
else:
|
||||
device_id = get_device_id()
|
||||
|
||||
if not device_id:
|
||||
print("[错误] 无法获取设备ID")
|
||||
sys.exit(1)
|
||||
|
||||
print(f"使用设备: {device_id}")
|
||||
print(f"ADB 路径: {get_adb_path()}")
|
||||
|
||||
# 测试 exec-out(主要方案)
|
||||
test_exec_out_screencap(device_id, iterations=5)
|
||||
|
||||
# 测试 shell + pull(回退方案,较少迭代)
|
||||
test_shell_screencap_pull(device_id, iterations=3)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,106 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
测试完全禁用截图服务功能
|
||||
"""
|
||||
|
||||
import time
|
||||
import sys
|
||||
import os
|
||||
|
||||
# 添加项目根目录到路径
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
def test_completely_disable_screenshot_service():
|
||||
"""测试完全禁用截图服务"""
|
||||
|
||||
print("测试完全禁用截图服务功能...")
|
||||
print("=" * 50)
|
||||
|
||||
# 1. 测试截图服务的get_screenshot方法
|
||||
print("\n1. 测试截图服务的get_screenshot方法")
|
||||
|
||||
from operations.screenshot_service import ScreenshotService
|
||||
service = ScreenshotService.get_instance()
|
||||
|
||||
# 调用get_screenshot,应该直接返回False
|
||||
result = service.get_screenshot("test_device")
|
||||
print(f"service.get_screenshot() 返回: {result}")
|
||||
|
||||
if result == False:
|
||||
print("[OK] 截图服务正确返回False,不启动后台服务")
|
||||
else:
|
||||
print("[ERROR] 截图服务返回了非False值")
|
||||
return False
|
||||
|
||||
# 2. 测试服务状态
|
||||
print("\n2. 检查服务状态")
|
||||
|
||||
if service.is_running():
|
||||
print("[WARN] 服务正在运行,但应该已被禁用")
|
||||
else:
|
||||
print("[OK] 服务未运行")
|
||||
|
||||
print(f"当前设备ID: {service._current_device_id}")
|
||||
|
||||
# 3. 测试直接截图调用
|
||||
print("\n3. 测试直接截图调用")
|
||||
|
||||
from operations.screenshot_operations import ScreenshotOperations
|
||||
ops = ScreenshotOperations()
|
||||
|
||||
# 检查默认参数
|
||||
import inspect
|
||||
sig = inspect.signature(ops.get_screenshot)
|
||||
params = sig.parameters
|
||||
use_service_default = params['use_service'].default
|
||||
|
||||
print(f"直接截图方法use_service默认值: {use_service_default}")
|
||||
|
||||
if use_service_default == False:
|
||||
print("[OK] 直接截图方法默认不使用服务")
|
||||
else:
|
||||
print("[WARN] 直接截图方法仍可能使用服务")
|
||||
|
||||
# 4. 验证所有修改
|
||||
print("\n4. 验证所有修改")
|
||||
|
||||
modified_files = [
|
||||
"operations/screenshot_operations.py",
|
||||
"operations/screenshot_service.py",
|
||||
"douyin/core/douyin_fudai.py",
|
||||
"douyin/core/douyin_main_flow.py",
|
||||
"fudai_detail_modules/fudai_detail_reader.py"
|
||||
]
|
||||
|
||||
for file_path in modified_files:
|
||||
if os.path.exists(file_path):
|
||||
print(f"[OK] {file_path} 存在")
|
||||
else:
|
||||
print(f"[ERROR] {file_path} 不存在")
|
||||
return False
|
||||
|
||||
# 5. 总结禁用状态
|
||||
print("\n5. 截图服务禁用总结")
|
||||
print("-" * 40)
|
||||
print("已完成的禁用:")
|
||||
print("1. [DONE] 修改 get_screenshot() 默认 use_service=False")
|
||||
print("2. [DONE] 让 ScreenshotService.get_screenshot() 直接返回False")
|
||||
print("3. [DONE] 移除所有后台服务启动代码")
|
||||
print("4. [DONE] 移除设备ID变更时的自动服务启动")
|
||||
print("5. [DONE] 移除OCR操作后的服务重启")
|
||||
|
||||
print("\n预期效果:")
|
||||
print("- 所有截图调用都会使用直接ADB命令")
|
||||
print("- 后台截图服务完全不会启动")
|
||||
print("- 每次截图都是独立的,不会复用缓存")
|
||||
print("- 系统更加稳定,但响应稍慢")
|
||||
|
||||
return True
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = test_completely_disable_screenshot_service()
|
||||
print(f"\n{'='*50}")
|
||||
print(f"测试结果: {'PASS' if success else 'FAIL'}")
|
||||
print("后台截图服务已完全禁用,使用原始直接截图模式")
|
||||
print(f"{'='*50}")
|
||||
@@ -1,287 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
测试直接截图功能
|
||||
验证所有截图调用都使用直接模式(use_service=False)
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from datetime import datetime
|
||||
|
||||
# 添加项目根目录到路径
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
def test_screenshot_operations():
|
||||
"""测试 operations.screenshot_operations 模块"""
|
||||
print("=" * 80)
|
||||
print("测试 1: operations.screenshot_operations.get_screenshot()")
|
||||
print("=" * 80)
|
||||
|
||||
try:
|
||||
from operations.screenshot_operations import get_screenshot, ScreenshotOperations
|
||||
|
||||
# 获取设备ID
|
||||
device_id = os.environ.get("DEVICE_ID", "")
|
||||
if not device_id:
|
||||
print("[WARN] 未设置 DEVICE_ID 环境变量,尝试从配置读取...")
|
||||
try:
|
||||
import json
|
||||
import path_config
|
||||
config_path = path_config.get_config_path()
|
||||
# 尝试从常见位置读取
|
||||
device_id = "MB75DEFQZHZTGMLJ" # 从日志中看到的设备ID
|
||||
except Exception:
|
||||
print("[ERROR] 无法获取设备ID,请设置环境变量 DEVICE_ID")
|
||||
return False
|
||||
|
||||
print(f"设备ID: {device_id}")
|
||||
print(f"测试模式: 直接截图 (use_service=False)")
|
||||
print()
|
||||
|
||||
# 测试1: 使用默认参数(应该是 False)
|
||||
print("测试 1.1: 使用默认参数调用 get_screenshot()")
|
||||
start = time.time()
|
||||
try:
|
||||
result = get_screenshot(device_id)
|
||||
elapsed = (time.time() - start) * 1000
|
||||
status = "[OK] 成功" if result else "[FAIL] 失败"
|
||||
print(f" 结果: {status}, 耗时: {elapsed:.1f}ms")
|
||||
except Exception as e:
|
||||
elapsed = (time.time() - start) * 1000
|
||||
print(f" [ERROR] 异常: {e}, 耗时: {elapsed:.1f}ms")
|
||||
|
||||
print()
|
||||
|
||||
# 测试2: 显式传递 use_service=False
|
||||
print("测试 1.2: 显式传递 use_service=False")
|
||||
start = time.time()
|
||||
try:
|
||||
ops = ScreenshotOperations()
|
||||
result = ops.get_screenshot(device_id, use_service=False)
|
||||
elapsed = (time.time() - start) * 1000
|
||||
status = "[OK] 成功" if result else "[FAIL] 失败"
|
||||
print(f" 结果: {status}, 耗时: {elapsed:.1f}ms")
|
||||
except Exception as e:
|
||||
elapsed = (time.time() - start) * 1000
|
||||
print(f" [ERROR] 异常: {e}, 耗时: {elapsed:.1f}ms")
|
||||
|
||||
print()
|
||||
|
||||
# 测试3: 验证不会使用服务模式
|
||||
print("测试 1.3: 验证不会调用后台服务")
|
||||
try:
|
||||
from operations.screenshot_service import ScreenshotService
|
||||
service = ScreenshotService.get_instance()
|
||||
service_running_before = service.is_running()
|
||||
print(f" 服务运行状态(测试前): {service_running_before}")
|
||||
|
||||
# 调用截图(应该不会启动服务)
|
||||
get_screenshot(device_id)
|
||||
|
||||
service_running_after = service.is_running()
|
||||
print(f" 服务运行状态(测试后): {service_running_after}")
|
||||
|
||||
if not service_running_after:
|
||||
print(" [OK] 确认:服务未被启动(符合预期)")
|
||||
else:
|
||||
print(" [WARN] 警告:服务仍在运行(可能之前已启动)")
|
||||
except Exception as e:
|
||||
print(f" [WARN] 无法检查服务状态: {e}")
|
||||
|
||||
return True
|
||||
|
||||
except ImportError as e:
|
||||
print(f"[ERROR] 导入错误: {e}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"[ERROR] 测试失败: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
|
||||
def test_underlying_operations():
|
||||
"""测试 Underlying_Operations 模块"""
|
||||
print()
|
||||
print("=" * 80)
|
||||
print("测试 2: Underlying_Operations.get_screenshot()")
|
||||
print("=" * 80)
|
||||
|
||||
try:
|
||||
from Underlying_Operations import underlying_operations
|
||||
|
||||
device_id = os.environ.get("DEVICE_ID", "MB75DEFQZHZTGMLJ")
|
||||
print(f"设备ID: {device_id}")
|
||||
print()
|
||||
|
||||
ops = underlying_operations()
|
||||
|
||||
# 测试默认参数
|
||||
print("测试 2.1: 使用默认参数(应该是 False)")
|
||||
start = time.time()
|
||||
try:
|
||||
result = ops.get_screenshot(device_id)
|
||||
elapsed = (time.time() - start) * 1000
|
||||
status = "[OK] 成功" if result else "[FAIL] 失败"
|
||||
print(f" 结果: {status}, 耗时: {elapsed:.1f}ms")
|
||||
|
||||
if elapsed > 2000:
|
||||
print(f" [WARN] 警告:耗时较长 ({elapsed:.1f}ms),但应该 < 2秒")
|
||||
elif elapsed > 6000:
|
||||
print(f" [ERROR] 错误:耗时过长 ({elapsed:.1f}ms),可能仍在使用服务模式")
|
||||
else:
|
||||
print(f" [OK] 正常:耗时在预期范围内")
|
||||
except Exception as e:
|
||||
elapsed = (time.time() - start) * 1000
|
||||
print(f" [ERROR] 异常: {e}, 耗时: {elapsed:.1f}ms")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"[ERROR] 测试失败: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
|
||||
def test_stage_diagnosis_screenshot():
|
||||
"""测试阶段诊断中的截图调用"""
|
||||
print()
|
||||
print("=" * 80)
|
||||
print("测试 3: 阶段诊断截图调用(模拟)")
|
||||
print("=" * 80)
|
||||
|
||||
try:
|
||||
# 检查关键文件中的调用
|
||||
import inspect
|
||||
from douyin.core import douyin_fudai
|
||||
|
||||
# 查找 get_screenshot 调用
|
||||
source_file = inspect.getfile(douyin_fudai.fudai_analyse)
|
||||
with open(source_file, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
# 检查是否包含 use_service=False
|
||||
if 'get_screenshot(self.device_id, use_service=False)' in content:
|
||||
print(" [OK] 确认:douyin_fudai.py 中已使用 use_service=False")
|
||||
elif 'get_screenshot(self.device_id)' in content:
|
||||
print(" [WARN] 警告:douyin_fudai.py 中未显式传递 use_service=False")
|
||||
print(" (但默认值应该是 False)")
|
||||
else:
|
||||
print(" [WARN] 未找到 get_screenshot 调用")
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f" [WARN] 无法检查: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def check_logs_for_timeouts():
|
||||
"""检查日志中是否有超时记录"""
|
||||
print()
|
||||
print("=" * 80)
|
||||
print("测试 4: 检查日志中的超时记录")
|
||||
print("=" * 80)
|
||||
|
||||
try:
|
||||
import glob
|
||||
log_files = glob.glob("logs/unified_*.log")
|
||||
if not log_files:
|
||||
print(" [WARN] 未找到日志文件")
|
||||
return True
|
||||
|
||||
# 读取最新的日志文件
|
||||
latest_log = max(log_files, key=os.path.getmtime)
|
||||
print(f" 检查日志文件: {latest_log}")
|
||||
|
||||
timeout_count = 0
|
||||
screenshot_count = 0
|
||||
|
||||
with open(latest_log, 'r', encoding='utf-8', errors='ignore') as f:
|
||||
# 只读取最后 1000 行
|
||||
lines = f.readlines()[-1000:]
|
||||
|
||||
for line in lines:
|
||||
if '[TIMEOUT] 等待超时' in line or '等待超时' in line:
|
||||
timeout_count += 1
|
||||
if 'screenshot_elapsed_seconds' in line:
|
||||
screenshot_count += 1
|
||||
# 提取耗时
|
||||
try:
|
||||
import re
|
||||
match = re.search(r'screenshot_elapsed_seconds=([\d.]+)', line)
|
||||
if match:
|
||||
elapsed = float(match.group(1))
|
||||
if elapsed > 5.0:
|
||||
print(f" [WARN] 发现耗时过长的截图: {elapsed:.2f}秒")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
print(f" 超时记录数(最近1000行): {timeout_count}")
|
||||
print(f" 截图耗时记录数: {screenshot_count}")
|
||||
|
||||
if timeout_count == 0:
|
||||
print(" [OK] 未发现超时记录(符合预期)")
|
||||
else:
|
||||
print(f" [WARN] 发现 {timeout_count} 条超时记录")
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f" [WARN] 无法检查日志: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
"""主测试函数"""
|
||||
print()
|
||||
print("=" * 80)
|
||||
print("直接截图功能测试")
|
||||
print("=" * 80)
|
||||
print(f"测试时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
print()
|
||||
|
||||
results = []
|
||||
|
||||
# 测试1: screenshot_operations
|
||||
results.append(("screenshot_operations", test_screenshot_operations()))
|
||||
|
||||
# 测试2: underlying_operations
|
||||
results.append(("underlying_operations", test_underlying_operations()))
|
||||
|
||||
# 测试3: 代码检查
|
||||
results.append(("stage_diagnosis", test_stage_diagnosis_screenshot()))
|
||||
|
||||
# 测试4: 日志检查
|
||||
results.append(("log_check", check_logs_for_timeouts()))
|
||||
|
||||
# 总结
|
||||
print()
|
||||
print("=" * 80)
|
||||
print("测试总结")
|
||||
print("=" * 80)
|
||||
|
||||
passed = sum(1 for _, result in results if result)
|
||||
total = len(results)
|
||||
|
||||
for name, result in results:
|
||||
status = "[PASS] 通过" if result else "[FAIL] 失败"
|
||||
print(f" {name}: {status}")
|
||||
|
||||
print()
|
||||
print(f"总计: {passed}/{total} 测试通过")
|
||||
|
||||
if passed == total:
|
||||
print("[SUCCESS] 所有测试通过!")
|
||||
return 0
|
||||
else:
|
||||
print("[WARN] 部分测试未通过,请检查上述输出")
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
exit(main())
|
||||
@@ -1,118 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
测试禁用流式截图功能
|
||||
"""
|
||||
|
||||
import time
|
||||
import sys
|
||||
import os
|
||||
|
||||
# 添加项目根目录到路径
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
def test_disable_streaming_screenshots():
|
||||
"""测试禁用流式截图功能"""
|
||||
|
||||
print("测试禁用流式截图功能...")
|
||||
print("=" * 50)
|
||||
|
||||
# 1. 检查截图服务的默认行为
|
||||
print("\n1. 检查截图服务默认行为")
|
||||
|
||||
from operations.screenshot_operations import ScreenshotOperations
|
||||
ops = ScreenshotOperations()
|
||||
|
||||
# 检查get_screenshot方法的默认参数
|
||||
import inspect
|
||||
sig = inspect.signature(ops.get_screenshot)
|
||||
params = sig.parameters
|
||||
|
||||
use_service_default = params['use_service'].default
|
||||
print(f"get_screenshot use_service 默认值: {use_service_default}")
|
||||
|
||||
if use_service_default == False:
|
||||
print("[OK] 默认禁用后台服务,使用直接截图")
|
||||
else:
|
||||
print("[FAIL] 仍然使用后台服务")
|
||||
|
||||
# 2. 模拟截图调用
|
||||
print("\n2. 模拟截图调用测试")
|
||||
|
||||
# 创建模拟的analyser
|
||||
class MockAnalyser:
|
||||
def __init__(self):
|
||||
self.device_id = "test_device_123"
|
||||
self.operation = ops
|
||||
|
||||
def _log_kv(self, key, value, **kwargs):
|
||||
print(f"[{key}] {value}")
|
||||
|
||||
analyser = MockAnalyser()
|
||||
|
||||
# 测试调用(这里不会实际执行截图,因为没有真实设备)
|
||||
try:
|
||||
# 这里只是检查方法调用路径,不会实际截图
|
||||
print("截图方法调用路径:")
|
||||
print("- get_screenshot(use_service=False) -> 直接截图")
|
||||
print("- 跳过后台服务检查")
|
||||
print("[OK] 确认使用直接截图模式")
|
||||
except Exception as e:
|
||||
print(f"[ERROR] 测试失败: {e}")
|
||||
|
||||
# 3. 检查后台服务状态
|
||||
print("\n3. 检查后台服务状态")
|
||||
|
||||
try:
|
||||
from operations.screenshot_service import ScreenshotService
|
||||
service = ScreenshotService.get_instance()
|
||||
|
||||
if service.is_running():
|
||||
print("[WARN] 后台服务正在运行(可能有其他地方启动)")
|
||||
else:
|
||||
print("[OK] 后台服务未运行")
|
||||
|
||||
print(f"服务状态: running={service.is_running()}")
|
||||
print(f"当前设备ID: {service._current_device_id}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"检查服务状态失败: {e}")
|
||||
|
||||
# 4. 验证修改的文件
|
||||
print("\n4. 验证修改的文件")
|
||||
|
||||
modified_files = [
|
||||
"operations/screenshot_operations.py",
|
||||
"douyin/core/douyin_fudai.py",
|
||||
"douyin/core/douyin_main_flow.py"
|
||||
]
|
||||
|
||||
for file_path in modified_files:
|
||||
if os.path.exists(file_path):
|
||||
print(f"[OK] {file_path} 存在")
|
||||
else:
|
||||
print(f"[ERROR] {file_path} 不存在")
|
||||
|
||||
# 5. 总结
|
||||
print("\n5. 修改总结")
|
||||
print("-" * 30)
|
||||
print("已完成的修改:")
|
||||
print("1. [DONE] 修改 get_screenshot() 默认 use_service=False")
|
||||
print("2. [DONE] 移除 douyin_fudai.py 中的后台服务启动")
|
||||
print("3. [DONE] 移除 douyin_main_flow.py 中的后台服务启动")
|
||||
print("4. [DONE] 所有截图调用将使用直接截图模式")
|
||||
|
||||
print("\n预期效果:")
|
||||
print("- 每次截图都会直接调用ADB命令")
|
||||
print("- 不再有后台截图服务持续运行")
|
||||
print("- 截图响应时间会变慢,但更稳定")
|
||||
print("- 消除了后台服务的复杂性")
|
||||
|
||||
return True
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = test_disable_streaming_screenshots()
|
||||
print(f"\n{'='*50}")
|
||||
print(f"测试结果: {'PASS' if success else 'FAIL'}")
|
||||
print("流式截图已暂时禁用,使用直接截图模式")
|
||||
print(f"{'='*50}")
|
||||
@@ -1,95 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
测试循环期间停止后台截图服务的功能
|
||||
"""
|
||||
|
||||
import time
|
||||
import sys
|
||||
import os
|
||||
|
||||
# 添加项目根目录到路径
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
def test_loop_screenshot_service_stop():
|
||||
"""测试循环期间后台截图服务是否被停止"""
|
||||
|
||||
print("测试循环期间停止后台截图服务功能...")
|
||||
|
||||
# 1. 启动截图服务(模拟正常启动)
|
||||
print("\n1. 启动后台截图服务...")
|
||||
from operations.screenshot_service import ScreenshotService
|
||||
service = ScreenshotService.get_instance()
|
||||
|
||||
device_id = "emulator-5554"
|
||||
|
||||
if not service.is_running():
|
||||
service.start(device_id, interval=1.5)
|
||||
time.sleep(0.5)
|
||||
|
||||
if service.is_running():
|
||||
print("[OK] 后台截图服务已启动")
|
||||
else:
|
||||
print("[FAIL] 后台截图服务启动失败")
|
||||
return False
|
||||
|
||||
# 2. 模拟fudai_choujiang的逻辑
|
||||
print("\n2. 模拟循环开始前的截图服务停止...")
|
||||
|
||||
# 这部分代码模拟fudai_choujiang中的逻辑
|
||||
screenshot_service_stopped = False
|
||||
try:
|
||||
service_inner = ScreenshotService.get_instance()
|
||||
if service_inner.is_running():
|
||||
service_inner.stop()
|
||||
screenshot_service_stopped = True
|
||||
print("[OK] 循环开始前停止后台截图服务")
|
||||
except Exception as e:
|
||||
print(f"[FAIL] 停止后台截图服务失败: {e}")
|
||||
return False
|
||||
|
||||
# 检查服务是否已停止
|
||||
if service.is_running():
|
||||
print("[FAIL] 后台截图服务仍然在运行")
|
||||
return False
|
||||
else:
|
||||
print("[OK] 后台截图服务已停止")
|
||||
|
||||
# 3. 模拟循环中的操作(这里不会触发截图服务)
|
||||
print("\n3. 模拟循环中的阶段诊断...")
|
||||
time.sleep(2) # 模拟一些操作
|
||||
|
||||
# 4. 模拟程序结束(atexit会重新启动服务)
|
||||
print("\n4. 模拟程序结束,检查服务重新启动...")
|
||||
|
||||
# 手动调用atexit函数(在实际运行中会自动调用)
|
||||
if screenshot_service_stopped:
|
||||
try:
|
||||
service_restart = ScreenshotService.get_instance()
|
||||
if device_id:
|
||||
service_restart.start(device_id, interval=1.5)
|
||||
print("[OK] 循环结束后重新启动后台截图服务")
|
||||
except Exception as e:
|
||||
print(f"[FAIL] 重新启动后台截图服务失败: {e}")
|
||||
return False
|
||||
|
||||
# 检查服务是否重新启动
|
||||
time.sleep(0.5)
|
||||
if service.is_running():
|
||||
print("[OK] 后台截图服务已重新启动")
|
||||
success = True
|
||||
else:
|
||||
print("[FAIL] 后台截图服务未重新启动")
|
||||
success = False
|
||||
|
||||
# 5. 清理
|
||||
if service.is_running():
|
||||
service.stop()
|
||||
print("\n5. 测试完成,停止后台截图服务")
|
||||
|
||||
print(f"\n测试结果: {'PASS' if success else 'FAIL'}")
|
||||
return success
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = test_loop_screenshot_service_stop()
|
||||
sys.exit(0 if success else 1)
|
||||
@@ -1,100 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
测试OCR多线程优化效果
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from operations.screenshot_service import get_optimal_workers
|
||||
|
||||
def test_ocr_multithread_config():
|
||||
"""测试OCR多线程配置"""
|
||||
print("=" * 70)
|
||||
print("OCR多线程配置检查")
|
||||
print("=" * 70)
|
||||
print()
|
||||
|
||||
# 1. CPU信息
|
||||
cpu_count = os.cpu_count() or 4
|
||||
print(f"1. CPU信息")
|
||||
print("-" * 70)
|
||||
print(f" CPU核心数: {cpu_count}")
|
||||
print()
|
||||
|
||||
# 2. OMP线程数配置
|
||||
print("2. OMP线程数配置(底层库多线程)")
|
||||
print("-" * 70)
|
||||
omp_threads = os.environ.get("OMP_NUM_THREADS", "未设置")
|
||||
print(f" OMP_NUM_THREADS: {omp_threads}")
|
||||
|
||||
# 计算推荐的OMP线程数
|
||||
optimal_omp_threads = min(4, max(2, cpu_count // 3))
|
||||
print(f" 推荐值: {optimal_omp_threads} (根据CPU核心数动态计算)")
|
||||
print()
|
||||
|
||||
# 3. 线程池大小配置
|
||||
print("3. 线程池大小配置(任务级多线程)")
|
||||
print("-" * 70)
|
||||
optimal_workers = get_optimal_workers()
|
||||
print(f" 推荐线程池大小: {optimal_workers} (根据CPU核心数动态计算)")
|
||||
print(f" 说明: 用于并行执行多个OCR任务")
|
||||
print()
|
||||
|
||||
# 4. 优化说明
|
||||
print("4. 优化说明")
|
||||
print("-" * 70)
|
||||
print(" 优化前:")
|
||||
print(" - OMP_NUM_THREADS=1 (限制底层库只能使用1个线程)")
|
||||
print(" - 线程池大小固定为6 (未充分利用多核CPU)")
|
||||
print()
|
||||
print(" 优化后:")
|
||||
print(f" - OMP_NUM_THREADS={optimal_omp_threads} (允许底层库使用多线程)")
|
||||
print(f" - 线程池大小={optimal_workers} (根据CPU核心数动态调整)")
|
||||
print()
|
||||
print(" 预期效果:")
|
||||
print(" - 底层库(PaddleOCR/EasyOCR)可以并行处理图像")
|
||||
print(" - 多个OCR任务可以并行执行")
|
||||
print(" - 充分利用多核CPU资源")
|
||||
print(" - 预计性能提升: 30-50% (取决于任务数量和CPU核心数)")
|
||||
print()
|
||||
|
||||
# 5. 验证OCR引擎是否已初始化
|
||||
print("5. OCR引擎状态")
|
||||
print("-" * 70)
|
||||
try:
|
||||
from operations.ocr_operations import _ocr_processor
|
||||
if _ocr_processor.ocr is not None:
|
||||
print(f" OCR引擎: {_ocr_processor.ocr_type}")
|
||||
print(" 状态: 已初始化")
|
||||
else:
|
||||
print(" 状态: 未初始化(延迟初始化)")
|
||||
except Exception as e:
|
||||
print(f" 状态: 检查失败 - {e}")
|
||||
print()
|
||||
|
||||
print("=" * 70)
|
||||
print("检查完成")
|
||||
print("=" * 70)
|
||||
|
||||
def test_ocr_performance():
|
||||
"""测试OCR性能(需要实际运行OCR)"""
|
||||
print()
|
||||
print("=" * 70)
|
||||
print("OCR性能测试")
|
||||
print("=" * 70)
|
||||
print()
|
||||
print("提示: 此测试需要实际运行OCR任务")
|
||||
print(" 可以在实际使用中观察OCR耗时变化")
|
||||
print()
|
||||
print("测试方法:")
|
||||
print("1. 运行程序,观察OCR任务的耗时")
|
||||
print("2. 对比优化前后的性能差异")
|
||||
print("3. 查看日志中的OCR耗时信息")
|
||||
print()
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_ocr_multithread_config()
|
||||
test_ocr_performance()
|
||||
@@ -1,325 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
OCR多线程性能测试
|
||||
测试优化前后的性能差异
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from PIL import Image
|
||||
import numpy as np
|
||||
import tempfile
|
||||
|
||||
def test_ocr_single(image_path, ocr_processor, omp_threads):
|
||||
"""测试单个OCR任务的性能"""
|
||||
old_omp = os.environ.get('OMP_NUM_THREADS', '4')
|
||||
os.environ['OMP_NUM_THREADS'] = str(omp_threads)
|
||||
|
||||
try:
|
||||
img = Image.open(image_path)
|
||||
if img.mode != 'RGB':
|
||||
img = img.convert('RGB')
|
||||
img_np = np.array(img)
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
if ocr_processor.ocr_type == "easyocr":
|
||||
result = ocr_processor.ocr.readtext(img_np)
|
||||
else:
|
||||
result = ocr_processor.ocr.ocr(img_np)
|
||||
|
||||
elapsed = (time.time() - start_time) * 1000
|
||||
|
||||
# 提取文本
|
||||
from Underlying_Operations import underlying_operations
|
||||
ops = underlying_operations()
|
||||
text = ops.extract_ocr_content(result)
|
||||
|
||||
return {
|
||||
'time': elapsed,
|
||||
'text': text,
|
||||
'success': True
|
||||
}
|
||||
except Exception as e:
|
||||
return {
|
||||
'time': 0,
|
||||
'text': '',
|
||||
'success': False,
|
||||
'error': str(e)
|
||||
}
|
||||
finally:
|
||||
os.environ['OMP_NUM_THREADS'] = old_omp
|
||||
|
||||
def test_ocr_config(image_path, config_name, omp_threads, num_iterations=5):
|
||||
"""测试特定配置下的OCR性能"""
|
||||
print(f"\n{'='*70}")
|
||||
print(f"测试配置: {config_name}")
|
||||
print(f"{'='*70}")
|
||||
print(f"OMP_NUM_THREADS: {omp_threads}")
|
||||
print(f"迭代次数: {num_iterations}")
|
||||
print()
|
||||
|
||||
# 初始化OCR
|
||||
try:
|
||||
from operations.ocr_operations import OCRProcessor
|
||||
ocr_processor = OCRProcessor()
|
||||
ensure_ocr = getattr(ocr_processor, '_ensure_ocr')
|
||||
ensure_ocr()
|
||||
|
||||
if ocr_processor.ocr is None:
|
||||
print("[ERROR] OCR引擎未初始化")
|
||||
return None
|
||||
|
||||
print(f"OCR引擎: {ocr_processor.ocr_type}")
|
||||
print()
|
||||
except Exception as e:
|
||||
print(f"[ERROR] OCR引擎初始化失败: {e}")
|
||||
return None
|
||||
|
||||
# 执行多次测试
|
||||
times = []
|
||||
for i in range(num_iterations):
|
||||
result = test_ocr_single(image_path, ocr_processor, omp_threads)
|
||||
if result['success']:
|
||||
times.append(result['time'])
|
||||
print(f" 第{i+1}次: {result['time']:.0f}ms - {result['text'][:30] if result['text'] else '(空)'}")
|
||||
else:
|
||||
print(f" 第{i+1}次: 失败 - {result.get('error', 'Unknown')}")
|
||||
|
||||
if not times:
|
||||
print("[ERROR] 所有测试都失败")
|
||||
return None
|
||||
|
||||
avg_time = sum(times) / len(times)
|
||||
min_time = min(times)
|
||||
max_time = max(times)
|
||||
|
||||
print()
|
||||
print(f"平均耗时: {avg_time:.0f}ms")
|
||||
print(f"最快: {min_time:.0f}ms")
|
||||
print(f"最慢: {max_time:.0f}ms")
|
||||
print()
|
||||
|
||||
return {
|
||||
'avg_time': avg_time,
|
||||
'min_time': min_time,
|
||||
'max_time': max_time,
|
||||
'times': times
|
||||
}
|
||||
|
||||
def test_parallel_ocr(image_path, config_name, omp_threads, max_workers, num_tasks=6):
|
||||
"""测试并行OCR性能"""
|
||||
print(f"\n{'='*70}")
|
||||
print(f"测试配置: {config_name}")
|
||||
print(f"{'='*70}")
|
||||
print(f"OMP_NUM_THREADS: {omp_threads}")
|
||||
print(f"线程池大小: {max_workers}")
|
||||
print(f"任务数量: {num_tasks}")
|
||||
print()
|
||||
|
||||
# 设置OMP线程数
|
||||
old_omp = os.environ.get('OMP_NUM_THREADS', '4')
|
||||
os.environ['OMP_NUM_THREADS'] = str(omp_threads)
|
||||
|
||||
# 初始化OCR
|
||||
try:
|
||||
from operations.ocr_operations import OCRProcessor
|
||||
ocr_processor = OCRProcessor()
|
||||
ensure_ocr = getattr(ocr_processor, '_ensure_ocr')
|
||||
ensure_ocr()
|
||||
|
||||
if ocr_processor.ocr is None:
|
||||
print("[ERROR] OCR引擎未初始化")
|
||||
os.environ['OMP_NUM_THREADS'] = old_omp
|
||||
return None
|
||||
|
||||
print(f"OCR引擎: {ocr_processor.ocr_type}")
|
||||
print()
|
||||
except Exception as e:
|
||||
print(f"[ERROR] OCR引擎初始化失败: {e}")
|
||||
os.environ['OMP_NUM_THREADS'] = old_omp
|
||||
return None
|
||||
|
||||
# 创建多个相同的任务(使用同一张图片)
|
||||
def ocr_task():
|
||||
return test_ocr_single(image_path, ocr_processor, omp_threads)
|
||||
|
||||
# 并行执行
|
||||
print("开始并行OCR识别...")
|
||||
start_time = time.time()
|
||||
results = []
|
||||
|
||||
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
||||
futures = [executor.submit(ocr_task) for _ in range(num_tasks)]
|
||||
|
||||
for i, future in enumerate(futures):
|
||||
try:
|
||||
result = future.result(timeout=60)
|
||||
results.append(result)
|
||||
if result['success']:
|
||||
print(f" 任务{i+1}: {result['time']:.0f}ms - {result['text'][:20] if result['text'] else '(空)'}")
|
||||
else:
|
||||
print(f" 任务{i+1}: 失败 - {result.get('error', 'Unknown')}")
|
||||
except Exception as e:
|
||||
print(f" 任务{i+1}: 异常 - {e}")
|
||||
results.append({'success': False, 'error': str(e)})
|
||||
|
||||
total_elapsed = (time.time() - start_time) * 1000
|
||||
|
||||
# 恢复OMP设置
|
||||
os.environ['OMP_NUM_THREADS'] = old_omp
|
||||
|
||||
# 统计结果
|
||||
successful_results = [r for r in results if r.get('success', False)]
|
||||
efficiency = 0 # 初始化效率变量
|
||||
if successful_results:
|
||||
task_times = [r['time'] for r in successful_results]
|
||||
total_task_time = sum(task_times)
|
||||
avg_task_time = total_task_time / len(task_times)
|
||||
|
||||
print()
|
||||
print(f"总耗时: {total_elapsed:.0f}ms ({total_elapsed/1000:.2f}秒)")
|
||||
print(f"任务总耗时: {total_task_time:.0f}ms")
|
||||
print(f"平均每个任务: {avg_task_time:.0f}ms")
|
||||
if total_task_time > 0:
|
||||
efficiency = total_task_time / total_elapsed
|
||||
print(f"并行效率: {efficiency:.2f}x (理想值: {num_tasks:.1f}x)")
|
||||
print()
|
||||
|
||||
return {
|
||||
'total_time': total_elapsed,
|
||||
'avg_task_time': avg_task_time,
|
||||
'task_times': task_times,
|
||||
'efficiency': efficiency if total_task_time > 0 else 0
|
||||
}
|
||||
else:
|
||||
print("[ERROR] 所有任务都失败")
|
||||
return None
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
# 默认图片路径
|
||||
default_image = "pic/debug_captures/2025-12-31/20251231_152118_ocr_福袋倒计时_分钟_识别后_(390,1110)-(480,1230)_模式_1_文本长度_2_数字_14_类型_digits_平均置信度_0.9991_最小置信度_0.9991_最大置信度_0.9991_置信度项数_1_有完整截图_是_有坐标_是.png"
|
||||
|
||||
if len(sys.argv) > 1:
|
||||
image_path = sys.argv[1]
|
||||
else:
|
||||
image_path = default_image
|
||||
|
||||
if not os.path.isabs(image_path):
|
||||
project_root = os.path.dirname(os.path.abspath(__file__))
|
||||
image_path = os.path.join(project_root, image_path)
|
||||
|
||||
if not os.path.exists(image_path):
|
||||
print(f"[ERROR] 图片文件不存在: {image_path}")
|
||||
return
|
||||
|
||||
print("=" * 70)
|
||||
print("OCR多线程性能测试")
|
||||
print("=" * 70)
|
||||
print()
|
||||
print(f"测试图片: {os.path.basename(image_path)}")
|
||||
|
||||
# 获取系统信息
|
||||
cpu_count = os.cpu_count() or 4
|
||||
print(f"CPU核心数: {cpu_count}")
|
||||
try:
|
||||
from operations.screenshot_service import get_optimal_workers
|
||||
optimal_workers = get_optimal_workers()
|
||||
print(f"推荐线程池大小: {optimal_workers}")
|
||||
except (ImportError, AttributeError):
|
||||
optimal_workers = 6
|
||||
print()
|
||||
|
||||
# 测试1: 单次OCR - 优化前
|
||||
print("\n" + "="*70)
|
||||
print("测试1: 单次OCR性能对比")
|
||||
print("="*70)
|
||||
result_single_old = test_ocr_config(
|
||||
image_path,
|
||||
"优化前 (OMP_NUM_THREADS=1)",
|
||||
omp_threads=1,
|
||||
num_iterations=5
|
||||
)
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
# 测试2: 单次OCR - 优化后
|
||||
result_single_new = test_ocr_config(
|
||||
image_path,
|
||||
"优化后 (OMP_NUM_THREADS=6)",
|
||||
omp_threads=6,
|
||||
num_iterations=5
|
||||
)
|
||||
|
||||
# 测试3: 并行OCR - 优化前
|
||||
print("\n" + "="*70)
|
||||
print("测试2: 并行OCR性能对比")
|
||||
print("="*70)
|
||||
result_parallel_old = test_parallel_ocr(
|
||||
image_path,
|
||||
"优化前 (OMP=1, workers=6)",
|
||||
omp_threads=1,
|
||||
max_workers=6,
|
||||
num_tasks=6
|
||||
)
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
# 测试4: 并行OCR - 优化后
|
||||
result_parallel_new = test_parallel_ocr(
|
||||
image_path,
|
||||
"优化后 (OMP=6, workers=10)",
|
||||
omp_threads=6,
|
||||
max_workers=optimal_workers,
|
||||
num_tasks=6
|
||||
)
|
||||
|
||||
# 性能对比总结
|
||||
print()
|
||||
print("=" * 70)
|
||||
print("性能对比总结")
|
||||
print("=" * 70)
|
||||
print()
|
||||
|
||||
if result_single_old and result_single_new:
|
||||
old_avg = result_single_old['avg_time']
|
||||
new_avg = result_single_new['avg_time']
|
||||
speedup = old_avg / new_avg if new_avg > 0 else 0
|
||||
|
||||
print("单次OCR性能:")
|
||||
print(f" 优化前平均: {old_avg:.0f}ms")
|
||||
print(f" 优化后平均: {new_avg:.0f}ms")
|
||||
print(f" 性能提升: {speedup:.2f}x")
|
||||
print(f" 时间减少: {(1 - new_avg/old_avg)*100:.1f}%")
|
||||
print()
|
||||
|
||||
if result_parallel_old and result_parallel_new:
|
||||
old_total = result_parallel_old['total_time']
|
||||
new_total = result_parallel_new['total_time']
|
||||
speedup = old_total / new_total if new_total > 0 else 0
|
||||
|
||||
print("并行OCR性能:")
|
||||
print(f" 优化前总耗时: {old_total:.0f}ms ({old_total/1000:.2f}秒)")
|
||||
print(f" 优化后总耗时: {new_total:.0f}ms ({new_total/1000:.2f}秒)")
|
||||
print(f" 性能提升: {speedup:.2f}x")
|
||||
print(f" 时间减少: {(1 - new_total/old_total)*100:.1f}%")
|
||||
print()
|
||||
|
||||
old_avg = result_parallel_old['avg_task_time']
|
||||
new_avg = result_parallel_new['avg_task_time']
|
||||
print("单任务性能:")
|
||||
print(f" 优化前平均任务: {old_avg:.0f}ms")
|
||||
print(f" 优化后平均任务: {new_avg:.0f}ms")
|
||||
print(f" 单任务性能提升: {old_avg/new_avg:.2f}x" if new_avg > 0 else " 单任务性能提升: N/A")
|
||||
print()
|
||||
|
||||
print("=" * 70)
|
||||
print("测试完成")
|
||||
print("=" * 70)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,196 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
测试OCR识别速度
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
def test_ocr_speed(image_path: str, iterations: int = 5):
|
||||
"""测试OCR识别速度"""
|
||||
print("=" * 70)
|
||||
print("OCR速度测试")
|
||||
print("=" * 70)
|
||||
print()
|
||||
|
||||
# 检查图片文件是否存在
|
||||
if not os.path.exists(image_path):
|
||||
print(f"[ERROR] 图片文件不存在: {image_path}")
|
||||
return
|
||||
|
||||
file_size = os.path.getsize(image_path) / 1024 # KB
|
||||
print(f"测试图片: {image_path}")
|
||||
print(f"文件大小: {file_size:.2f} KB")
|
||||
print(f"测试次数: {iterations} 次")
|
||||
print()
|
||||
|
||||
# 获取图片信息
|
||||
try:
|
||||
from PIL import Image
|
||||
img = Image.open(image_path)
|
||||
width, height = img.size
|
||||
print(f"图片尺寸: {width}x{height}")
|
||||
print(f"图片模式: {img.mode}")
|
||||
|
||||
# 计算图片像素数
|
||||
total_pixels = width * height
|
||||
print(f"总像素数: {total_pixels:,}")
|
||||
|
||||
# 判断图片大小
|
||||
if total_pixels > 2000000: # 大于200万像素
|
||||
print(f" [提示] 图片较大,OCR处理可能需要较长时间")
|
||||
print(f" 建议: 先裁剪到目标区域(如200x200)再OCR,可以显著提升速度")
|
||||
print()
|
||||
except Exception as e:
|
||||
print(f"[WARNING] 无法读取图片信息: {e}")
|
||||
print()
|
||||
|
||||
# 检查OCR引擎
|
||||
print("1. OCR引擎信息")
|
||||
print("-" * 70)
|
||||
try:
|
||||
from operations.ocr_operations import _ocr_processor
|
||||
_ocr_processor._ensure_ocr()
|
||||
|
||||
if _ocr_processor.ocr is None:
|
||||
print("[ERROR] OCR引擎未初始化")
|
||||
return
|
||||
|
||||
print(f" OCR引擎: {_ocr_processor.ocr_type}")
|
||||
print(f" OMP线程数: {os.environ.get('OMP_NUM_THREADS', '未设置')}")
|
||||
print()
|
||||
except Exception as e:
|
||||
print(f"[ERROR] OCR引擎检查失败: {e}")
|
||||
return
|
||||
|
||||
# 测试OCR速度
|
||||
print("2. OCR速度测试")
|
||||
print("-" * 70)
|
||||
|
||||
times = []
|
||||
results = []
|
||||
|
||||
for i in range(iterations):
|
||||
try:
|
||||
# 导入OCR操作
|
||||
from Underlying_Operations import underlying_operations
|
||||
ops = underlying_operations()
|
||||
|
||||
# 确保OCR已初始化
|
||||
ops._ensure_ocr()
|
||||
|
||||
# 执行OCR(使用analyse_pic_digits,因为这是数字识别)
|
||||
start_time = time.time()
|
||||
|
||||
# 直接使用图片路径进行OCR
|
||||
# 注意:如果图片很大,OCR会处理整个图片,这可能会很慢
|
||||
# 建议使用预处理后的小图片进行测试
|
||||
result = ops.analyse_pic_digits(
|
||||
"测试图片",
|
||||
change_color=1, # 数字识别模式
|
||||
pic_path=image_path
|
||||
)
|
||||
|
||||
# 记录各个阶段的耗时(如果可能)
|
||||
# 注意:analyse_pic_digits 内部会进行图像预处理和OCR识别
|
||||
|
||||
elapsed = (time.time() - start_time) * 1000 # 转换为毫秒
|
||||
times.append(elapsed)
|
||||
results.append(result)
|
||||
|
||||
print(f" 第 {i+1} 次: {elapsed:.0f}ms, 识别结果: {result[:50] if result else '(空)'}")
|
||||
|
||||
except Exception as e:
|
||||
print(f" 第 {i+1} 次: 失败 - {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
print()
|
||||
|
||||
# 统计结果
|
||||
if times:
|
||||
print("3. 性能统计")
|
||||
print("-" * 70)
|
||||
avg_time = sum(times) / len(times)
|
||||
min_time = min(times)
|
||||
max_time = max(times)
|
||||
|
||||
print(f" 平均耗时: {avg_time:.0f}ms")
|
||||
print(f" 最快耗时: {min_time:.0f}ms")
|
||||
print(f" 最慢耗时: {max_time:.0f}ms")
|
||||
print(f" 耗时范围: {max_time - min_time:.0f}ms")
|
||||
print()
|
||||
|
||||
# 识别结果统计
|
||||
print("4. 识别结果")
|
||||
print("-" * 70)
|
||||
unique_results = list(set(results))
|
||||
for i, result in enumerate(unique_results, 1):
|
||||
count = results.count(result)
|
||||
print(f" 结果 {i}: {result[:100] if result else '(空)'} (出现 {count} 次)")
|
||||
print()
|
||||
|
||||
# 性能评估
|
||||
print("5. 性能评估")
|
||||
print("-" * 70)
|
||||
if avg_time < 500:
|
||||
print(" [优秀] OCR速度很快 (< 500ms)")
|
||||
elif avg_time < 1000:
|
||||
print(" [良好] OCR速度良好 (500-1000ms)")
|
||||
elif avg_time < 2000:
|
||||
print(" [一般] OCR速度一般 (1000-2000ms)")
|
||||
elif avg_time < 5000:
|
||||
print(" [较慢] OCR速度较慢 (2000-5000ms)")
|
||||
else:
|
||||
print(" [很慢] OCR速度很慢 (> 5000ms)")
|
||||
print(" 提示: 如果图片很大,建议先裁剪到目标区域再OCR")
|
||||
|
||||
# 多线程效果评估
|
||||
cpu_count = os.cpu_count() or 4
|
||||
omp_threads = int(os.environ.get('OMP_NUM_THREADS', '1'))
|
||||
print(f" CPU核心数: {cpu_count}")
|
||||
print(f" OMP线程数: {omp_threads}")
|
||||
if omp_threads > 1:
|
||||
print(f" [优化] 已启用底层多线程 (OMP_NUM_THREADS={omp_threads})")
|
||||
else:
|
||||
print(f" [提示] 未启用底层多线程,可以优化")
|
||||
print()
|
||||
else:
|
||||
print("[ERROR] 所有测试都失败了")
|
||||
|
||||
print("=" * 70)
|
||||
print("测试完成")
|
||||
print("=" * 70)
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
# 默认图片路径
|
||||
default_image = "pic/debug_captures/2025-12-31/20251231_152116_ocr_福袋倒计时_秒_预处理后_(480,1110)-(570,1230)_模式_1_尺寸_270x360_来源_福袋倒计时_秒.png_有完整截图_是_有坐标_是_类型_digits.png"
|
||||
|
||||
# 从命令行参数获取图片路径
|
||||
if len(sys.argv) > 1:
|
||||
image_path = sys.argv[1]
|
||||
else:
|
||||
image_path = default_image
|
||||
|
||||
# 获取测试次数
|
||||
iterations = 5
|
||||
if len(sys.argv) > 2:
|
||||
try:
|
||||
iterations = int(sys.argv[2])
|
||||
except:
|
||||
pass
|
||||
|
||||
# 转换为绝对路径
|
||||
if not os.path.isabs(image_path):
|
||||
# 尝试相对于项目根目录
|
||||
project_root = os.path.dirname(os.path.abspath(__file__))
|
||||
image_path = os.path.join(project_root, image_path)
|
||||
|
||||
test_ocr_speed(image_path, iterations)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,212 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
测试OCR识别速度(优化版 - 支持裁剪区域测试)
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from typing import Optional
|
||||
from PIL import Image
|
||||
|
||||
def test_ocr_on_region(image_path: str, region: Optional[tuple] = None, iterations: int = 5):
|
||||
"""在指定区域测试OCR速度"""
|
||||
print("=" * 70)
|
||||
print("OCR速度测试(区域优化版)")
|
||||
print("=" * 70)
|
||||
print()
|
||||
|
||||
# 检查图片文件是否存在
|
||||
if not os.path.exists(image_path):
|
||||
print(f"[ERROR] 图片文件不存在: {image_path}")
|
||||
return
|
||||
|
||||
# 加载图片
|
||||
try:
|
||||
img = Image.open(image_path)
|
||||
original_size = img.size
|
||||
print(f"原始图片: {image_path}")
|
||||
print(f"原始尺寸: {original_size[0]}x{original_size[1]}")
|
||||
print(f"原始像素数: {original_size[0] * original_size[1]:,}")
|
||||
print()
|
||||
except Exception as e:
|
||||
print(f"[ERROR] 无法加载图片: {e}")
|
||||
return
|
||||
|
||||
# 如果指定了区域,裁剪图片
|
||||
if region:
|
||||
x1, y1, x2, y2 = region
|
||||
img = img.crop((x1, y1, x2, y2))
|
||||
print(f"裁剪区域: ({x1}, {y1}) - ({x2}, {y2})")
|
||||
print(f"裁剪后尺寸: {img.size[0]}x{img.size[1]}")
|
||||
print(f"裁剪后像素数: {img.size[0] * img.size[1]:,}")
|
||||
print()
|
||||
|
||||
# 保存裁剪后的临时图片
|
||||
import tempfile
|
||||
temp_dir = tempfile.gettempdir()
|
||||
temp_image = os.path.join(temp_dir, "ocr_test_crop.png")
|
||||
img.save(temp_image)
|
||||
test_image_path = temp_image
|
||||
else:
|
||||
test_image_path = image_path
|
||||
|
||||
# 测试OCR速度
|
||||
print("开始OCR速度测试...")
|
||||
print("-" * 70)
|
||||
|
||||
try:
|
||||
from operations.ocr_operations import _ocr_processor
|
||||
_ocr_processor._ensure_ocr()
|
||||
|
||||
if _ocr_processor.ocr is None:
|
||||
print("[ERROR] OCR引擎未初始化")
|
||||
return
|
||||
|
||||
print(f"OCR引擎: {_ocr_processor.ocr_type}")
|
||||
print(f"OMP线程数: {os.environ.get('OMP_NUM_THREADS', '未设置')}")
|
||||
print()
|
||||
except Exception as e:
|
||||
print(f"[ERROR] OCR引擎检查失败: {e}")
|
||||
return
|
||||
|
||||
# 执行多次测试
|
||||
times = []
|
||||
results = []
|
||||
|
||||
for i in range(iterations):
|
||||
try:
|
||||
import numpy as np
|
||||
|
||||
# 准备图像
|
||||
if img.mode != 'RGB':
|
||||
img_rgb = img.convert('RGB')
|
||||
else:
|
||||
img_rgb = img
|
||||
|
||||
# 转换为numpy数组
|
||||
img_np = np.array(img_rgb)
|
||||
|
||||
# 执行OCR
|
||||
start_time = time.time()
|
||||
|
||||
if _ocr_processor.ocr_type == "easyocr":
|
||||
result = _ocr_processor.ocr.readtext(img_np)
|
||||
else:
|
||||
result = _ocr_processor.ocr.ocr(img_np)
|
||||
|
||||
elapsed = (time.time() - start_time) * 1000 # 转换为毫秒
|
||||
|
||||
# 提取文本
|
||||
from Underlying_Operations import underlying_operations
|
||||
ops = underlying_operations()
|
||||
text = ops.extract_ocr_content(result)
|
||||
|
||||
times.append(elapsed)
|
||||
results.append(text)
|
||||
|
||||
print(f"第 {i+1} 次: {elapsed:.0f}ms, 识别结果: {text[:50] if text else '(空)'}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"第 {i+1} 次: 失败 - {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
print()
|
||||
|
||||
# 统计结果
|
||||
if times:
|
||||
print("性能统计")
|
||||
print("-" * 70)
|
||||
avg_time = sum(times) / len(times)
|
||||
min_time = min(times)
|
||||
max_time = max(times)
|
||||
|
||||
print(f"平均耗时: {avg_time:.0f}ms ({avg_time/1000:.2f}秒)")
|
||||
print(f"最快耗时: {min_time:.0f}ms ({min_time/1000:.2f}秒)")
|
||||
print(f"最慢耗时: {max_time:.0f}ms ({max_time/1000:.2f}秒)")
|
||||
print(f"耗时范围: {max_time - min_time:.0f}ms")
|
||||
print()
|
||||
|
||||
# 识别结果
|
||||
print("识别结果")
|
||||
print("-" * 70)
|
||||
unique_results = list(set(results))
|
||||
for i, result in enumerate(unique_results, 1):
|
||||
count = results.count(result)
|
||||
print(f"结果 {i}: {result[:100] if result else '(空)'} (出现 {count} 次)")
|
||||
print()
|
||||
|
||||
# 性能对比
|
||||
if region:
|
||||
original_pixels = original_size[0] * original_size[1]
|
||||
crop_pixels = img.size[0] * img.size[1]
|
||||
reduction = (1 - crop_pixels / original_pixels) * 100
|
||||
print("性能对比")
|
||||
print("-" * 70)
|
||||
print(f"原始图片像素: {original_pixels:,}")
|
||||
print(f"裁剪后像素: {crop_pixels:,}")
|
||||
print(f"像素减少: {reduction:.1f}%")
|
||||
print(f"预计原始图片OCR耗时: {avg_time * (original_pixels / crop_pixels):.0f}ms")
|
||||
print()
|
||||
|
||||
# 清理临时文件
|
||||
if region and os.path.exists(test_image_path):
|
||||
try:
|
||||
os.remove(test_image_path)
|
||||
except:
|
||||
pass
|
||||
|
||||
print("=" * 70)
|
||||
print("测试完成")
|
||||
print("=" * 70)
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
# 默认图片路径
|
||||
default_image = "pic/debug_captures/2025-12-31/20251231_152116_ocr_福袋倒计时_秒_预处理后_(480,1110)-(570,1230)_模式_1_尺寸_270x360_来源_福袋倒计时_秒.png_有完整截图_是_有坐标_是_类型_digits.png"
|
||||
|
||||
# 从命令行参数获取图片路径
|
||||
if len(sys.argv) > 1:
|
||||
image_path = sys.argv[1]
|
||||
else:
|
||||
image_path = default_image
|
||||
|
||||
# 转换为绝对路径
|
||||
if not os.path.isabs(image_path):
|
||||
project_root = os.path.dirname(os.path.abspath(__file__))
|
||||
image_path = os.path.join(project_root, image_path)
|
||||
|
||||
# 从文件名提取区域信息(如果文件名包含坐标)
|
||||
region = None
|
||||
if "(480,1110)-(570,1230)" in image_path:
|
||||
# 提取坐标
|
||||
region = (480, 1110, 570, 1230)
|
||||
print("[INFO] 检测到文件名中的坐标信息,将使用裁剪区域进行测试")
|
||||
print()
|
||||
|
||||
# 获取测试次数
|
||||
iterations = 5
|
||||
if len(sys.argv) > 2:
|
||||
try:
|
||||
iterations = int(sys.argv[2])
|
||||
except:
|
||||
pass
|
||||
|
||||
# 先测试完整图片
|
||||
print("=" * 70)
|
||||
print("测试1: 完整图片OCR")
|
||||
print("=" * 70)
|
||||
test_ocr_on_region(image_path, region=None, iterations=iterations)
|
||||
|
||||
# 如果检测到区域信息,再测试裁剪区域
|
||||
if region:
|
||||
print()
|
||||
print("=" * 70)
|
||||
print("测试2: 裁剪区域OCR(推荐)")
|
||||
print("=" * 70)
|
||||
test_ocr_on_region(image_path, region=region, iterations=iterations)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,383 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
测试并行OCR性能,对比优化前后的提升
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from PIL import Image
|
||||
import numpy as np
|
||||
|
||||
def test_serial_ocr(image_path: str, num_tasks: int = 3):
|
||||
"""测试串行OCR(优化前的方式)"""
|
||||
print("=" * 70)
|
||||
print("测试1: 串行OCR(优化前)")
|
||||
print("=" * 70)
|
||||
print(f"任务数量: {num_tasks}")
|
||||
print(f"OMP_NUM_THREADS: {os.environ.get('OMP_NUM_THREADS', '未设置')}")
|
||||
print()
|
||||
|
||||
# 加载图片
|
||||
try:
|
||||
img = Image.open(image_path)
|
||||
if img.mode != 'RGB':
|
||||
img = img.convert('RGB')
|
||||
img_np = np.array(img)
|
||||
print(f"图片尺寸: {img.size[0]}x{img.size[1]}")
|
||||
print()
|
||||
except Exception as e:
|
||||
print(f"[ERROR] 无法加载图片: {e}")
|
||||
return None
|
||||
|
||||
# 初始化OCR
|
||||
try:
|
||||
from operations.ocr_operations import _ocr_processor
|
||||
_ocr_processor._ensure_ocr()
|
||||
|
||||
if _ocr_processor.ocr is None:
|
||||
print("[ERROR] OCR引擎未初始化")
|
||||
return None
|
||||
|
||||
print(f"OCR引擎: {_ocr_processor.ocr_type}")
|
||||
print()
|
||||
except Exception as e:
|
||||
print(f"[ERROR] OCR引擎初始化失败: {e}")
|
||||
return None
|
||||
|
||||
# 串行执行OCR任务
|
||||
start_time = time.time()
|
||||
results = []
|
||||
|
||||
print("开始串行OCR测试...")
|
||||
for i in range(num_tasks):
|
||||
task_start = time.time()
|
||||
|
||||
try:
|
||||
if _ocr_processor.ocr_type == "easyocr":
|
||||
result = _ocr_processor.ocr.readtext(img_np)
|
||||
else:
|
||||
result = _ocr_processor.ocr.ocr(img_np)
|
||||
|
||||
# 提取文本
|
||||
from Underlying_Operations import underlying_operations
|
||||
ops = underlying_operations()
|
||||
text = ops.extract_ocr_content(result)
|
||||
|
||||
task_elapsed = (time.time() - task_start) * 1000
|
||||
results.append(text)
|
||||
print(f" 任务 {i+1}/{num_tasks}: {task_elapsed:.0f}ms, 结果: {text[:30] if text else '(空)'}")
|
||||
except Exception as e:
|
||||
print(f" 任务 {i+1}/{num_tasks}: 失败 - {e}")
|
||||
results.append(None)
|
||||
|
||||
total_elapsed = (time.time() - start_time) * 1000
|
||||
|
||||
print()
|
||||
print(f"总耗时: {total_elapsed:.0f}ms ({total_elapsed/1000:.2f}秒)")
|
||||
print(f"平均每个任务: {total_elapsed/num_tasks:.0f}ms")
|
||||
print()
|
||||
|
||||
return total_elapsed
|
||||
|
||||
def test_parallel_ocr(image_path: str, num_tasks: int = 3):
|
||||
"""测试并行OCR(优化后的方式)"""
|
||||
print("=" * 70)
|
||||
print("测试2: 并行OCR(优化后)")
|
||||
print("=" * 70)
|
||||
print(f"任务数量: {num_tasks}")
|
||||
print(f"OMP_NUM_THREADS: {os.environ.get('OMP_NUM_THREADS', '未设置')}")
|
||||
|
||||
# 获取最优线程数
|
||||
try:
|
||||
from operations.screenshot_service import get_optimal_workers
|
||||
optimal_workers = get_optimal_workers()
|
||||
print(f"线程池大小: {optimal_workers}")
|
||||
except:
|
||||
optimal_workers = 6
|
||||
print(f"线程池大小: {optimal_workers} (默认)")
|
||||
print()
|
||||
|
||||
# 加载图片
|
||||
try:
|
||||
img = Image.open(image_path)
|
||||
if img.mode != 'RGB':
|
||||
img = img.convert('RGB')
|
||||
img_np = np.array(img)
|
||||
print(f"图片尺寸: {img.size[0]}x{img.size[1]}")
|
||||
print()
|
||||
except Exception as e:
|
||||
print(f"[ERROR] 无法加载图片: {e}")
|
||||
return None
|
||||
|
||||
# 初始化OCR
|
||||
try:
|
||||
from operations.ocr_operations import _ocr_processor
|
||||
_ocr_processor._ensure_ocr()
|
||||
|
||||
if _ocr_processor.ocr is None:
|
||||
print("[ERROR] OCR引擎未初始化")
|
||||
return None
|
||||
|
||||
print(f"OCR引擎: {_ocr_processor.ocr_type}")
|
||||
print()
|
||||
except Exception as e:
|
||||
print(f"[ERROR] OCR引擎初始化失败: {e}")
|
||||
return None
|
||||
|
||||
# 定义OCR任务函数
|
||||
def ocr_task(task_id):
|
||||
"""单个OCR任务"""
|
||||
try:
|
||||
if _ocr_processor.ocr_type == "easyocr":
|
||||
result = _ocr_processor.ocr.readtext(img_np)
|
||||
else:
|
||||
result = _ocr_processor.ocr.ocr(img_np)
|
||||
|
||||
# 提取文本
|
||||
from Underlying_Operations import underlying_operations
|
||||
ops = underlying_operations()
|
||||
text = ops.extract_ocr_content(result)
|
||||
|
||||
return text
|
||||
except Exception as e:
|
||||
print(f" 任务 {task_id} 失败: {e}")
|
||||
return None
|
||||
|
||||
# 并行执行OCR任务
|
||||
start_time = time.time()
|
||||
results = []
|
||||
|
||||
print("开始并行OCR测试...")
|
||||
with ThreadPoolExecutor(max_workers=optimal_workers) as executor:
|
||||
# 提交所有任务
|
||||
futures = {executor.submit(ocr_task, i+1): i+1 for i in range(num_tasks)}
|
||||
|
||||
# 收集结果
|
||||
task_times = {}
|
||||
for future in futures:
|
||||
task_id = futures[future]
|
||||
task_start = time.time()
|
||||
try:
|
||||
result = future.result()
|
||||
task_elapsed = (time.time() - task_start) * 1000
|
||||
task_times[task_id] = task_elapsed
|
||||
results.append(result)
|
||||
print(f" 任务 {task_id}/{num_tasks}: {task_elapsed:.0f}ms, 结果: {result[:30] if result else '(空)'}")
|
||||
except Exception as e:
|
||||
print(f" 任务 {task_id}/{num_tasks}: 失败 - {e}")
|
||||
results.append(None)
|
||||
|
||||
total_elapsed = (time.time() - start_time) * 1000
|
||||
|
||||
print()
|
||||
print(f"总耗时: {total_elapsed:.0f}ms ({total_elapsed/1000:.2f}秒)")
|
||||
if task_times:
|
||||
avg_task_time = sum(task_times.values()) / len(task_times)
|
||||
print(f"平均每个任务: {avg_task_time:.0f}ms")
|
||||
print(f"并行效率: {avg_task_time * num_tasks / total_elapsed:.1f}x (理想情况下应该接近{num_tasks}x)")
|
||||
print()
|
||||
|
||||
return total_elapsed
|
||||
|
||||
def test_parallel_ocr_old_config(image_path: str, num_tasks: int = 3):
|
||||
"""测试并行OCR(使用旧的配置:OMP_NUM_THREADS=1, max_workers=6)"""
|
||||
print("=" * 70)
|
||||
print("测试3: 并行OCR(旧配置:OMP=1, 线程池=6)")
|
||||
print("=" * 70)
|
||||
print(f"任务数量: {num_tasks}")
|
||||
|
||||
# 临时设置旧配置
|
||||
old_omp = os.environ.get('OMP_NUM_THREADS', '4')
|
||||
os.environ['OMP_NUM_THREADS'] = '1'
|
||||
print(f"OMP_NUM_THREADS: 1 (临时设置)")
|
||||
print(f"线程池大小: 6 (固定)")
|
||||
print()
|
||||
|
||||
# 加载图片
|
||||
try:
|
||||
img = Image.open(image_path)
|
||||
if img.mode != 'RGB':
|
||||
img = img.convert('RGB')
|
||||
img_np = np.array(img)
|
||||
print(f"图片尺寸: {img.size[0]}x{img.size[1]}")
|
||||
print()
|
||||
except Exception as e:
|
||||
print(f"[ERROR] 无法加载图片: {e}")
|
||||
os.environ['OMP_NUM_THREADS'] = old_omp
|
||||
return None
|
||||
|
||||
# 重新初始化OCR(使用新的OMP设置)
|
||||
try:
|
||||
from operations.ocr_operations import _ocr_processor
|
||||
# 注意:OCR引擎已经初始化,OMP设置可能不会立即生效
|
||||
# 但我们可以测试线程池的效果
|
||||
print(f"OCR引擎: {_ocr_processor.ocr_type}")
|
||||
print("注意: OMP设置可能需要重启程序才能完全生效")
|
||||
print()
|
||||
except Exception as e:
|
||||
print(f"[ERROR] OCR引擎检查失败: {e}")
|
||||
os.environ['OMP_NUM_THREADS'] = old_omp
|
||||
return None
|
||||
|
||||
# 定义OCR任务函数
|
||||
def ocr_task(task_id):
|
||||
"""单个OCR任务"""
|
||||
try:
|
||||
if _ocr_processor.ocr_type == "easyocr":
|
||||
result = _ocr_processor.ocr.readtext(img_np)
|
||||
else:
|
||||
result = _ocr_processor.ocr.ocr(img_np)
|
||||
|
||||
# 提取文本
|
||||
from Underlying_Operations import underlying_operations
|
||||
ops = underlying_operations()
|
||||
text = ops.extract_ocr_content(result)
|
||||
|
||||
return text
|
||||
except Exception as e:
|
||||
print(f" 任务 {task_id} 失败: {e}")
|
||||
return None
|
||||
|
||||
# 并行执行OCR任务(使用旧的线程池大小)
|
||||
start_time = time.time()
|
||||
results = []
|
||||
|
||||
print("开始并行OCR测试(旧配置)...")
|
||||
with ThreadPoolExecutor(max_workers=6) as executor:
|
||||
# 提交所有任务
|
||||
futures = {executor.submit(ocr_task, i+1): i+1 for i in range(num_tasks)}
|
||||
|
||||
# 收集结果
|
||||
task_times = {}
|
||||
for future in futures:
|
||||
task_id = futures[future]
|
||||
task_start = time.time()
|
||||
try:
|
||||
result = future.result()
|
||||
task_elapsed = (time.time() - task_start) * 1000
|
||||
task_times[task_id] = task_elapsed
|
||||
results.append(result)
|
||||
print(f" 任务 {task_id}/{num_tasks}: {task_elapsed:.0f}ms, 结果: {result[:30] if result else '(空)'}")
|
||||
except Exception as e:
|
||||
print(f" 任务 {task_id}/{num_tasks}: 失败 - {e}")
|
||||
results.append(None)
|
||||
|
||||
total_elapsed = (time.time() - start_time) * 1000
|
||||
|
||||
# 恢复OMP设置
|
||||
os.environ['OMP_NUM_THREADS'] = old_omp
|
||||
|
||||
print()
|
||||
print(f"总耗时: {total_elapsed:.0f}ms ({total_elapsed/1000:.2f}秒)")
|
||||
if task_times:
|
||||
avg_task_time = sum(task_times.values()) / len(task_times)
|
||||
print(f"平均每个任务: {avg_task_time:.0f}ms")
|
||||
print()
|
||||
|
||||
return total_elapsed
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
# 默认图片路径
|
||||
default_image = "pic/debug_captures/2025-12-31/20251231_152116_ocr_福袋倒计时_秒_预处理后_(480,1110)-(570,1230)_模式_1_尺寸_270x360_来源_福袋倒计时_秒.png_有完整截图_是_有坐标_是_类型_digits.png"
|
||||
|
||||
# 从命令行参数获取图片路径
|
||||
if len(sys.argv) > 1:
|
||||
image_path = sys.argv[1]
|
||||
else:
|
||||
image_path = default_image
|
||||
|
||||
# 转换为绝对路径
|
||||
if not os.path.isabs(image_path):
|
||||
project_root = os.path.dirname(os.path.abspath(__file__))
|
||||
image_path = os.path.join(project_root, image_path)
|
||||
|
||||
# 检查文件是否存在
|
||||
if not os.path.exists(image_path):
|
||||
print(f"[ERROR] 图片文件不存在: {image_path}")
|
||||
return
|
||||
|
||||
# 获取任务数量(默认3个,减少测试时间)
|
||||
num_tasks = 3
|
||||
if len(sys.argv) > 2:
|
||||
try:
|
||||
num_tasks = int(sys.argv[2])
|
||||
except:
|
||||
pass
|
||||
|
||||
print("=" * 70)
|
||||
print("并行OCR性能对比测试")
|
||||
print("=" * 70)
|
||||
print()
|
||||
print(f"测试图片: {os.path.basename(image_path)}")
|
||||
print(f"任务数量: {num_tasks}")
|
||||
print()
|
||||
|
||||
# 获取当前配置信息
|
||||
cpu_count = os.cpu_count() or 4
|
||||
omp_threads = os.environ.get('OMP_NUM_THREADS', '未设置')
|
||||
print(f"系统信息:")
|
||||
print(f" CPU核心数: {cpu_count}")
|
||||
print(f" OMP_NUM_THREADS: {omp_threads}")
|
||||
try:
|
||||
from operations.screenshot_service import get_optimal_workers
|
||||
optimal_workers = get_optimal_workers()
|
||||
print(f" 推荐线程池大小: {optimal_workers}")
|
||||
except:
|
||||
pass
|
||||
print()
|
||||
print()
|
||||
|
||||
# 测试1: 串行OCR
|
||||
serial_time = test_serial_ocr(image_path, num_tasks)
|
||||
|
||||
print()
|
||||
print()
|
||||
|
||||
# 测试2: 并行OCR(优化后)
|
||||
parallel_time = test_parallel_ocr(image_path, num_tasks)
|
||||
|
||||
print()
|
||||
print()
|
||||
|
||||
# 测试3: 并行OCR(旧配置)
|
||||
parallel_old_time = test_parallel_ocr_old_config(image_path, num_tasks)
|
||||
|
||||
# 性能对比总结
|
||||
print()
|
||||
print("=" * 70)
|
||||
print("性能对比总结")
|
||||
print("=" * 70)
|
||||
print()
|
||||
|
||||
if serial_time and parallel_time:
|
||||
speedup = serial_time / parallel_time
|
||||
print(f"串行OCR: {serial_time:.0f}ms ({serial_time/1000:.2f}秒)")
|
||||
print(f"并行OCR: {parallel_time:.0f}ms ({parallel_time/1000:.2f}秒)")
|
||||
print(f"性能提升: {speedup:.2f}x ({(1 - parallel_time/serial_time)*100:.1f}% 时间减少)")
|
||||
print()
|
||||
|
||||
if parallel_old_time and parallel_time:
|
||||
speedup_vs_old = parallel_old_time / parallel_time
|
||||
print(f"旧配置并行: {parallel_old_time:.0f}ms ({parallel_old_time/1000:.2f}秒)")
|
||||
print(f"新配置并行: {parallel_time:.0f}ms ({parallel_time/1000:.2f}秒)")
|
||||
print(f"配置优化提升: {speedup_vs_old:.2f}x ({(1 - parallel_time/parallel_old_time)*100:.1f}% 时间减少)")
|
||||
print()
|
||||
|
||||
if serial_time and parallel_time and parallel_old_time:
|
||||
total_speedup = serial_time / parallel_time
|
||||
print(f"总体优化效果:")
|
||||
print(f" 串行 → 并行: {serial_time/parallel_time:.2f}x")
|
||||
print(f" 旧配置 → 新配置: {parallel_old_time/parallel_time:.2f}x")
|
||||
print(f" 串行 → 新配置: {total_speedup:.2f}x")
|
||||
print()
|
||||
|
||||
print("=" * 70)
|
||||
print("测试完成")
|
||||
print("=" * 70)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,141 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
快速测试并行OCR性能(只测试并行,不测试串行)
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from PIL import Image
|
||||
import numpy as np
|
||||
|
||||
def test_parallel_ocr_quick(image_path: str, num_tasks: int = 3):
|
||||
"""快速测试并行OCR"""
|
||||
print("=" * 70)
|
||||
print("并行OCR性能测试(快速版)")
|
||||
print("=" * 70)
|
||||
print()
|
||||
|
||||
# 检查文件
|
||||
if not os.path.exists(image_path):
|
||||
print(f"[ERROR] 图片文件不存在: {image_path}")
|
||||
return None
|
||||
|
||||
# 加载图片
|
||||
try:
|
||||
img = Image.open(image_path)
|
||||
if img.mode != 'RGB':
|
||||
img = img.convert('RGB')
|
||||
img_np = np.array(img)
|
||||
print(f"图片: {os.path.basename(image_path)}")
|
||||
print(f"尺寸: {img.size[0]}x{img.size[1]}")
|
||||
print(f"任务数: {num_tasks}")
|
||||
print()
|
||||
except Exception as e:
|
||||
print(f"[ERROR] 无法加载图片: {e}")
|
||||
return None
|
||||
|
||||
# 初始化OCR
|
||||
try:
|
||||
from operations.ocr_operations import _ocr_processor
|
||||
_ocr_processor._ensure_ocr()
|
||||
|
||||
if _ocr_processor.ocr is None:
|
||||
print("[ERROR] OCR引擎未初始化")
|
||||
return None
|
||||
|
||||
print(f"OCR引擎: {_ocr_processor.ocr_type}")
|
||||
print(f"OMP_NUM_THREADS: {os.environ.get('OMP_NUM_THREADS', '未设置')}")
|
||||
|
||||
from operations.screenshot_service import get_optimal_workers
|
||||
optimal_workers = get_optimal_workers()
|
||||
print(f"线程池大小: {optimal_workers}")
|
||||
print()
|
||||
except Exception as e:
|
||||
print(f"[ERROR] OCR引擎初始化失败: {e}")
|
||||
return None
|
||||
|
||||
# 定义OCR任务
|
||||
def ocr_task(task_id):
|
||||
try:
|
||||
if _ocr_processor.ocr_type == "easyocr":
|
||||
result = _ocr_processor.ocr.readtext(img_np)
|
||||
else:
|
||||
result = _ocr_processor.ocr.ocr(img_np)
|
||||
|
||||
from Underlying_Operations import underlying_operations
|
||||
ops = underlying_operations()
|
||||
text = ops.extract_ocr_content(result)
|
||||
return text
|
||||
except Exception as e:
|
||||
return f"错误: {e}"
|
||||
|
||||
# 执行并行OCR
|
||||
print("开始并行OCR测试...")
|
||||
start_time = time.time()
|
||||
|
||||
with ThreadPoolExecutor(max_workers=optimal_workers) as executor:
|
||||
futures = {executor.submit(ocr_task, i+1): i+1 for i in range(num_tasks)}
|
||||
results = []
|
||||
|
||||
for future in futures:
|
||||
task_id = futures[future]
|
||||
try:
|
||||
result = future.result()
|
||||
results.append(result)
|
||||
print(f" 任务 {task_id}: 完成, 结果: {result[:30] if result else '(空)'}")
|
||||
except Exception as e:
|
||||
print(f" 任务 {task_id}: 失败 - {e}")
|
||||
results.append(None)
|
||||
|
||||
total_elapsed = (time.time() - start_time) * 1000
|
||||
|
||||
print()
|
||||
print(f"总耗时: {total_elapsed:.0f}ms ({total_elapsed/1000:.2f}秒)")
|
||||
print(f"平均每个任务: {total_elapsed/num_tasks:.0f}ms")
|
||||
print()
|
||||
|
||||
# 估算串行时间(基于单次OCR耗时)
|
||||
print("性能估算:")
|
||||
print("-" * 70)
|
||||
single_task_time = total_elapsed / num_tasks
|
||||
estimated_serial_time = single_task_time * num_tasks
|
||||
speedup = estimated_serial_time / total_elapsed if total_elapsed > 0 else 0
|
||||
|
||||
print(f"估算串行时间: {estimated_serial_time:.0f}ms ({estimated_serial_time/1000:.2f}秒)")
|
||||
print(f"实际并行时间: {total_elapsed:.0f}ms ({total_elapsed/1000:.2f}秒)")
|
||||
print(f"性能提升: {speedup:.2f}x (节省 {(1 - total_elapsed/estimated_serial_time)*100:.1f}% 时间)")
|
||||
print()
|
||||
|
||||
return total_elapsed
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
default_image = "pic/debug_captures/2025-12-31/20251231_152116_ocr_福袋倒计时_秒_预处理后_(480,1110)-(570,1230)_模式_1_尺寸_270x360_来源_福袋倒计时_秒.png_有完整截图_是_有坐标_是_类型_digits.png"
|
||||
|
||||
if len(sys.argv) > 1:
|
||||
image_path = sys.argv[1]
|
||||
else:
|
||||
image_path = default_image
|
||||
|
||||
if not os.path.isabs(image_path):
|
||||
project_root = os.path.dirname(os.path.abspath(__file__))
|
||||
image_path = os.path.join(project_root, image_path)
|
||||
|
||||
num_tasks = 3
|
||||
if len(sys.argv) > 2:
|
||||
try:
|
||||
num_tasks = int(sys.argv[2])
|
||||
except:
|
||||
pass
|
||||
|
||||
test_parallel_ocr_quick(image_path, num_tasks)
|
||||
|
||||
print("=" * 70)
|
||||
print("测试完成")
|
||||
print("=" * 70)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,206 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
简化版并行OCR性能对比测试
|
||||
直接使用提供的图片进行测试
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from PIL import Image
|
||||
import numpy as np
|
||||
|
||||
def test_ocr_with_config(image_path, config_name, omp_threads, max_workers, num_tasks=6):
|
||||
"""使用指定配置测试OCR"""
|
||||
print(f"\n{'='*70}")
|
||||
print(f"{config_name}")
|
||||
print(f"{'='*70}")
|
||||
print(f"OMP_NUM_THREADS={omp_threads}, max_workers={max_workers}, 任务数={num_tasks}")
|
||||
print()
|
||||
|
||||
# 设置OMP
|
||||
old_omp = os.environ.get('OMP_NUM_THREADS', '4')
|
||||
os.environ['OMP_NUM_THREADS'] = str(omp_threads)
|
||||
|
||||
# 加载图片
|
||||
try:
|
||||
img = Image.open(image_path)
|
||||
if img.mode != 'RGB':
|
||||
img = img.convert('RGB')
|
||||
img_np = np.array(img)
|
||||
print(f"图片: {os.path.basename(image_path)}")
|
||||
print(f"尺寸: {img.size[0]}x{img.size[1]} 像素")
|
||||
print()
|
||||
except Exception as e:
|
||||
print(f"[ERROR] 无法加载图片: {e}")
|
||||
os.environ['OMP_NUM_THREADS'] = old_omp
|
||||
return None
|
||||
|
||||
# 初始化OCR
|
||||
try:
|
||||
from operations.ocr_operations import OCRProcessor
|
||||
ocr_processor = OCRProcessor()
|
||||
ocr_processor._ensure_ocr()
|
||||
|
||||
if ocr_processor.ocr is None:
|
||||
print("[ERROR] OCR引擎未初始化")
|
||||
os.environ['OMP_NUM_THREADS'] = old_omp
|
||||
return None
|
||||
|
||||
print(f"OCR引擎: {ocr_processor.ocr_type}")
|
||||
print()
|
||||
except Exception as e:
|
||||
print(f"[ERROR] OCR引擎初始化失败: {e}")
|
||||
os.environ['OMP_NUM_THREADS'] = old_omp
|
||||
return None
|
||||
|
||||
# OCR任务函数
|
||||
def ocr_task(task_id):
|
||||
try:
|
||||
start = time.time()
|
||||
if ocr_processor.ocr_type == "easyocr":
|
||||
result = ocr_processor.ocr.readtext(img_np)
|
||||
else:
|
||||
result = ocr_processor.ocr.ocr(img_np)
|
||||
|
||||
from Underlying_Operations import underlying_operations
|
||||
ops = underlying_operations()
|
||||
text = ops.extract_ocr_content(result)
|
||||
elapsed = (time.time() - start) * 1000
|
||||
return {'task_id': task_id, 'text': text, 'time': elapsed, 'success': True}
|
||||
except Exception as e:
|
||||
return {'task_id': task_id, 'text': '', 'time': 0, 'success': False, 'error': str(e)}
|
||||
|
||||
# 执行并行OCR
|
||||
print("开始并行OCR测试...")
|
||||
start_time = time.time()
|
||||
results = []
|
||||
|
||||
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
||||
futures = {executor.submit(ocr_task, i+1): i+1 for i in range(num_tasks)}
|
||||
|
||||
for future in futures:
|
||||
try:
|
||||
result = future.result()
|
||||
results.append(result)
|
||||
status = "✓" if result['success'] else "✗"
|
||||
print(f" {status} 任务{result['task_id']}: {result['time']:.0f}ms, 结果: {result['text'][:20] if result['text'] else '(空)'}")
|
||||
except Exception as e:
|
||||
print(f" ✗ 任务失败: {e}")
|
||||
|
||||
total_elapsed = (time.time() - start_time) * 1000
|
||||
|
||||
# 恢复OMP设置
|
||||
os.environ['OMP_NUM_THREADS'] = old_omp
|
||||
|
||||
# 统计
|
||||
successful = [r for r in results if r['success']]
|
||||
total_task_time = sum(r['time'] for r in successful)
|
||||
avg_task_time = total_task_time / len(successful) if successful else 0
|
||||
|
||||
print()
|
||||
print(f"总耗时: {total_elapsed:.0f}ms ({total_elapsed/1000:.2f}秒)")
|
||||
print(f"任务总耗时: {total_task_time:.0f}ms")
|
||||
print(f"平均每个任务: {avg_task_time:.0f}ms")
|
||||
if total_task_time > 0:
|
||||
efficiency = total_task_time / total_elapsed
|
||||
print(f"并行效率: {efficiency:.2f}x (理想值: {num_tasks:.1f}x)")
|
||||
print()
|
||||
|
||||
return {
|
||||
'total_time': total_elapsed,
|
||||
'avg_task_time': avg_task_time,
|
||||
'config': config_name
|
||||
}
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
default_image = "pic/debug_captures/2025-12-31/20251231_152116_ocr_福袋倒计时_秒_预处理后_(480,1110)-(570,1230)_模式_1_尺寸_270x360_来源_福袋倒计时_秒.png_有完整截图_是_有坐标_是_类型_digits.png"
|
||||
|
||||
if len(sys.argv) > 1:
|
||||
image_path = sys.argv[1]
|
||||
else:
|
||||
image_path = default_image
|
||||
|
||||
if not os.path.isabs(image_path):
|
||||
project_root = os.path.dirname(os.path.abspath(__file__))
|
||||
image_path = os.path.join(project_root, image_path)
|
||||
|
||||
if not os.path.exists(image_path):
|
||||
print(f"[ERROR] 图片文件不存在: {image_path}")
|
||||
return
|
||||
|
||||
print("=" * 70)
|
||||
print("并行OCR性能对比测试(简化版)")
|
||||
print("=" * 70)
|
||||
print()
|
||||
print(f"测试图片: {os.path.basename(image_path)}")
|
||||
|
||||
# 获取系统信息
|
||||
cpu_count = os.cpu_count() or 4
|
||||
print(f"CPU核心数: {cpu_count}")
|
||||
try:
|
||||
from operations.screenshot_service import get_optimal_workers
|
||||
optimal_workers = get_optimal_workers()
|
||||
print(f"推荐线程池大小: {optimal_workers}")
|
||||
except:
|
||||
optimal_workers = 6
|
||||
|
||||
num_tasks = 6
|
||||
print(f"测试任务数: {num_tasks}")
|
||||
print()
|
||||
|
||||
# 测试1: 优化前
|
||||
result_old = test_ocr_with_config(
|
||||
image_path,
|
||||
"优化前配置 (OMP=1, workers=6)",
|
||||
omp_threads=1,
|
||||
max_workers=6,
|
||||
num_tasks=num_tasks
|
||||
)
|
||||
|
||||
time.sleep(1) # 等待系统稳定
|
||||
|
||||
# 测试2: 优化后
|
||||
result_new = test_ocr_with_config(
|
||||
image_path,
|
||||
"优化后配置 (OMP=4, workers=10)",
|
||||
omp_threads=4,
|
||||
max_workers=optimal_workers,
|
||||
num_tasks=num_tasks
|
||||
)
|
||||
|
||||
# 对比结果
|
||||
print()
|
||||
print("=" * 70)
|
||||
print("性能对比总结")
|
||||
print("=" * 70)
|
||||
print()
|
||||
|
||||
if result_old and result_new:
|
||||
old_time = result_old['total_time']
|
||||
new_time = result_new['total_time']
|
||||
speedup = old_time / new_time if new_time > 0 else 0
|
||||
|
||||
print(f"优化前总耗时: {old_time:.0f}ms ({old_time/1000:.2f}秒)")
|
||||
print(f"优化后总耗时: {new_time:.0f}ms ({new_time/1000:.2f}秒)")
|
||||
print(f"性能提升: {speedup:.2f}x")
|
||||
print(f"时间减少: {(1 - new_time/old_time)*100:.1f}%")
|
||||
print()
|
||||
|
||||
old_avg = result_old['avg_task_time']
|
||||
new_avg = result_new['avg_task_time']
|
||||
print(f"优化前平均任务耗时: {old_avg:.0f}ms")
|
||||
print(f"优化后平均任务耗时: {new_avg:.0f}ms")
|
||||
if new_avg > 0:
|
||||
print(f"单任务性能提升: {old_avg/new_avg:.2f}x")
|
||||
|
||||
print()
|
||||
print("=" * 70)
|
||||
print("测试完成")
|
||||
print("=" * 70)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,376 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
真实场景并行OCR性能对比测试
|
||||
模拟实际使用中的多区域并行OCR识别
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from PIL import Image
|
||||
import numpy as np
|
||||
import tempfile
|
||||
|
||||
def create_test_regions(full_image_path):
|
||||
"""从完整截图中创建多个测试区域(模拟真实OCR场景)"""
|
||||
try:
|
||||
img = Image.open(full_image_path)
|
||||
if img.mode != 'RGB':
|
||||
img = img.convert('RGB')
|
||||
|
||||
width, height = img.size
|
||||
|
||||
# 判断是否是完整截图(1080x2400)还是裁剪区域
|
||||
is_full_screenshot = width >= 1000 and height >= 2000
|
||||
|
||||
if is_full_screenshot:
|
||||
# 完整截图:创建多个测试区域
|
||||
# 模拟真实的OCR区域(基于福袋详情页的常见区域)
|
||||
regions = {
|
||||
"福袋倒计时_分钟": (400, 1050, 500, 1150), # 倒计时分钟区域
|
||||
"福袋倒计时_秒": (480, 1110, 570, 1230), # 倒计时秒区域(用户提供的区域)
|
||||
"福袋内容": (50, 800, 500, 1000), # 福袋内容区域
|
||||
"福袋价值": (200, 1300, 400, 1400), # 价值区域
|
||||
"任务列表": (50, 600, 500, 750), # 任务列表区域
|
||||
"参与按钮": (300, 1500, 800, 1600), # 按钮状态区域
|
||||
}
|
||||
else:
|
||||
# 裁剪区域:使用当前图片作为测试区域之一,创建其他相似大小的区域
|
||||
# 从文件名提取坐标信息(如果存在)
|
||||
coords_from_filename = None
|
||||
if "(480,1110)-(570,1230)" in full_image_path:
|
||||
coords_from_filename = (480, 1110, 570, 1230)
|
||||
|
||||
# 创建多个相似大小的测试区域
|
||||
# 如果图片较小,创建多个相同大小的区域用于测试
|
||||
# 这些区域会在完整截图的合理位置
|
||||
regions = {}
|
||||
|
||||
# 使用当前图片作为第一个区域
|
||||
if coords_from_filename:
|
||||
x1, y1, x2, y2 = coords_from_filename
|
||||
regions["福袋倒计时_秒"] = (x1, y1, x2, y2)
|
||||
# 创建其他相邻区域
|
||||
offset_x = x2 - x1
|
||||
offset_y = y2 - y1
|
||||
regions["福袋倒计时_分钟"] = (x1 - offset_x - 20, y1, x1 - 20, y2)
|
||||
regions["福袋内容"] = (x1, y1 - offset_y - 20, x2, y1 - 20)
|
||||
regions["福袋价值"] = (x1, y2 + 20, x2, y2 + offset_y + 20)
|
||||
else:
|
||||
# 如果无法从文件名提取坐标,使用图片本身作为唯一测试区域
|
||||
# 创建多个相同大小的区域(通过复制当前图片)
|
||||
regions["区域1"] = (0, 0, width, height)
|
||||
regions["区域2"] = (0, 0, width, height) # 相同区域,用于测试并行
|
||||
regions["区域3"] = (0, 0, width, height) # 相同区域,用于测试并行
|
||||
regions["区域4"] = (0, 0, width, height) # 相同区域,用于测试并行
|
||||
regions["区域5"] = (0, 0, width, height) # 相同区域,用于测试并行
|
||||
regions["区域6"] = (0, 0, width, height) # 相同区域,用于测试并行
|
||||
|
||||
# 裁剪并保存区域图片
|
||||
region_images = {}
|
||||
temp_dir = tempfile.gettempdir()
|
||||
|
||||
for name, (x1, y1, x2, y2) in regions.items():
|
||||
try:
|
||||
# 确保坐标在图片范围内
|
||||
x1 = max(0, min(x1, img.width))
|
||||
y1 = max(0, min(y1, img.height))
|
||||
x2 = max(x1, min(x2, img.width))
|
||||
y2 = max(y1, min(y2, img.height))
|
||||
|
||||
if x2 > x1 and y2 > y1:
|
||||
crop_img = img.crop((x1, y1, x2, y2))
|
||||
temp_path = os.path.join(temp_dir, f"ocr_test_{name}.png")
|
||||
crop_img.save(temp_path)
|
||||
region_images[name] = {
|
||||
'path': temp_path,
|
||||
'size': crop_img.size,
|
||||
'coords': (x1, y1, x2, y2)
|
||||
}
|
||||
except (ValueError, OSError, IOError) as e:
|
||||
print(f"[WARNING] 无法裁剪区域 {name}: {e}")
|
||||
|
||||
return region_images, img.size
|
||||
except (OSError, IOError, ValueError) as e:
|
||||
print(f"[ERROR] 无法处理图片: {e}")
|
||||
return {}, None
|
||||
|
||||
def ocr_single_region(image_path, region_name, ocr_processor):
|
||||
"""对单个区域进行OCR识别"""
|
||||
try:
|
||||
img = Image.open(image_path)
|
||||
if img.mode != 'RGB':
|
||||
img = img.convert('RGB')
|
||||
img_np = np.array(img)
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
if ocr_processor.ocr_type == "easyocr":
|
||||
result = ocr_processor.ocr.readtext(img_np)
|
||||
else:
|
||||
result = ocr_processor.ocr.ocr(img_np)
|
||||
|
||||
elapsed = (time.time() - start_time) * 1000
|
||||
|
||||
# 提取文本
|
||||
from Underlying_Operations import underlying_operations
|
||||
ops = underlying_operations()
|
||||
text = ops.extract_ocr_content(result)
|
||||
|
||||
return {
|
||||
'region': region_name,
|
||||
'text': text,
|
||||
'time': elapsed,
|
||||
'success': True
|
||||
}
|
||||
except Exception as e:
|
||||
import traceback
|
||||
error_msg = f"{type(e).__name__}: {str(e)}"
|
||||
print(f" [DEBUG] {region_name} OCR错误: {error_msg}")
|
||||
return {
|
||||
'region': region_name,
|
||||
'text': '',
|
||||
'time': 0,
|
||||
'success': False,
|
||||
'error': error_msg
|
||||
}
|
||||
|
||||
def test_parallel_ocr_real(region_images, config_name, omp_threads, max_workers):
|
||||
"""测试真实场景的并行OCR"""
|
||||
print(f"\n{'='*70}")
|
||||
print(f"测试: {config_name}")
|
||||
print(f"{'='*70}")
|
||||
print(f"配置: OMP_NUM_THREADS={omp_threads}, max_workers={max_workers}")
|
||||
print(f"区域数量: {len(region_images)}")
|
||||
print()
|
||||
|
||||
# 设置OMP线程数
|
||||
old_omp = os.environ.get('OMP_NUM_THREADS', '4')
|
||||
os.environ['OMP_NUM_THREADS'] = str(omp_threads)
|
||||
|
||||
# 初始化OCR(使用新的OMP设置)
|
||||
try:
|
||||
from operations.ocr_operations import OCRProcessor
|
||||
ocr_processor = OCRProcessor()
|
||||
# 访问受保护方法以初始化OCR引擎(测试代码中需要)
|
||||
# 使用 getattr 来避免 pylance 警告
|
||||
ensure_ocr = getattr(ocr_processor, '_ensure_ocr')
|
||||
ensure_ocr()
|
||||
|
||||
if ocr_processor.ocr is None:
|
||||
print("[ERROR] OCR引擎未初始化")
|
||||
os.environ['OMP_NUM_THREADS'] = old_omp
|
||||
return None
|
||||
|
||||
print(f"OCR引擎: {ocr_processor.ocr_type}")
|
||||
print()
|
||||
except (ImportError, AttributeError, RuntimeError) as e:
|
||||
print(f"[ERROR] OCR引擎初始化失败: {e}")
|
||||
os.environ['OMP_NUM_THREADS'] = old_omp
|
||||
return None
|
||||
|
||||
# 显示区域信息
|
||||
print("OCR区域信息:")
|
||||
for name, info in region_images.items():
|
||||
print(f" {name}: {info['size'][0]}x{info['size'][1]} 像素, 坐标: {info['coords']}")
|
||||
print()
|
||||
|
||||
# 并行执行OCR
|
||||
print("开始并行OCR识别...")
|
||||
start_time = time.time()
|
||||
results = []
|
||||
|
||||
def ocr_task(name, path):
|
||||
return ocr_single_region(path, name, ocr_processor)
|
||||
|
||||
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
||||
# 提交所有任务
|
||||
futures = {
|
||||
executor.submit(ocr_task, name, info['path']): name
|
||||
for name, info in region_images.items()
|
||||
}
|
||||
|
||||
# 收集结果
|
||||
for future in futures:
|
||||
region_name = futures[future]
|
||||
try:
|
||||
result = future.result(timeout=60) # 每个任务最多60秒
|
||||
results.append(result)
|
||||
status = "✓" if result['success'] else "✗"
|
||||
print(f" {status} {region_name}: {result['time']:.0f}ms, 结果: {result['text'][:30] if result['text'] else '(空)'}")
|
||||
except TimeoutError:
|
||||
print(f" ✗ {region_name}: 超时(>60秒)")
|
||||
results.append({
|
||||
'region': region_name,
|
||||
'text': '',
|
||||
'time': 0,
|
||||
'success': False,
|
||||
'error': 'Timeout'
|
||||
})
|
||||
except (RuntimeError, AttributeError, KeyError) as e:
|
||||
print(f" ✗ {region_name}: 失败 - {e}")
|
||||
results.append({
|
||||
'region': region_name,
|
||||
'text': '',
|
||||
'time': 0,
|
||||
'success': False,
|
||||
'error': str(e)
|
||||
})
|
||||
|
||||
total_elapsed = (time.time() - start_time) * 1000
|
||||
|
||||
# 恢复OMP设置
|
||||
os.environ['OMP_NUM_THREADS'] = old_omp
|
||||
|
||||
# 统计结果
|
||||
successful_results = [r for r in results if r['success']]
|
||||
total_task_time = sum(r['time'] for r in successful_results)
|
||||
avg_task_time = total_task_time / len(successful_results) if successful_results else 0
|
||||
|
||||
print()
|
||||
print(f"总耗时: {total_elapsed:.0f}ms ({total_elapsed/1000:.2f}秒)")
|
||||
print(f"任务总耗时: {total_task_time:.0f}ms")
|
||||
print(f"平均每个任务: {avg_task_time:.0f}ms")
|
||||
if total_task_time > 0:
|
||||
efficiency = total_task_time / total_elapsed
|
||||
print(f"并行效率: {efficiency:.2f}x (理想值: {len(region_images):.1f}x)")
|
||||
print()
|
||||
|
||||
return {
|
||||
'total_time': total_elapsed,
|
||||
'task_times': [r['time'] for r in successful_results],
|
||||
'results': results,
|
||||
'config': config_name
|
||||
}
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
# 默认图片路径(完整截图)
|
||||
default_image = "pic/debug_captures/2025-12-31/20251231_152116_ocr_福袋倒计时_秒_预处理后_(480,1110)-(570,1230)_模式_1_尺寸_270x360_来源_福袋倒计时_秒.png_有完整截图_是_有坐标_是_类型_digits.png"
|
||||
|
||||
if len(sys.argv) > 1:
|
||||
image_path = sys.argv[1]
|
||||
else:
|
||||
image_path = default_image
|
||||
|
||||
if not os.path.isabs(image_path):
|
||||
project_root = os.path.dirname(os.path.abspath(__file__))
|
||||
image_path = os.path.join(project_root, image_path)
|
||||
|
||||
if not os.path.exists(image_path):
|
||||
print(f"[ERROR] 图片文件不存在: {image_path}")
|
||||
return
|
||||
|
||||
print("=" * 70)
|
||||
print("真实场景并行OCR性能对比测试")
|
||||
print("=" * 70)
|
||||
print()
|
||||
print(f"测试图片: {os.path.basename(image_path)}")
|
||||
print()
|
||||
|
||||
# 创建测试区域
|
||||
print("准备测试区域...")
|
||||
region_images, full_size = create_test_regions(image_path)
|
||||
|
||||
if not region_images:
|
||||
print("[ERROR] 无法创建测试区域")
|
||||
return
|
||||
|
||||
if full_size is not None:
|
||||
print(f"完整截图尺寸: {full_size[0]}x{full_size[1]}")
|
||||
else:
|
||||
print("完整截图尺寸: 未知")
|
||||
print(f"成功创建 {len(region_images)} 个测试区域")
|
||||
print()
|
||||
|
||||
# 获取系统信息
|
||||
cpu_count = os.cpu_count() or 4
|
||||
print("系统信息:")
|
||||
print(f" CPU核心数: {cpu_count}")
|
||||
try:
|
||||
from operations.screenshot_service import get_optimal_workers
|
||||
optimal_workers = get_optimal_workers()
|
||||
print(f" 推荐线程池大小: {optimal_workers}")
|
||||
except (ImportError, AttributeError):
|
||||
optimal_workers = 6
|
||||
print()
|
||||
|
||||
# 测试1: 优化前配置(OMP=1, workers=6)
|
||||
result_old = test_parallel_ocr_real(
|
||||
region_images,
|
||||
"优化前配置",
|
||||
omp_threads=1,
|
||||
max_workers=6
|
||||
)
|
||||
|
||||
# 等待一下,让系统稳定
|
||||
time.sleep(1)
|
||||
|
||||
# 测试2: 优化后配置(OMP=4, workers=10)
|
||||
result_new = test_parallel_ocr_real(
|
||||
region_images,
|
||||
"优化后配置",
|
||||
omp_threads=4,
|
||||
max_workers=optimal_workers
|
||||
)
|
||||
|
||||
# 性能对比总结
|
||||
print()
|
||||
print("=" * 70)
|
||||
print("性能对比总结")
|
||||
print("=" * 70)
|
||||
print()
|
||||
|
||||
if result_old and result_new:
|
||||
old_time = result_old['total_time']
|
||||
new_time = result_new['total_time']
|
||||
speedup = old_time / new_time if new_time > 0 else 0
|
||||
|
||||
print(f"优化前总耗时: {old_time:.0f}ms ({old_time/1000:.2f}秒)")
|
||||
print(f"优化后总耗时: {new_time:.0f}ms ({new_time/1000:.2f}秒)")
|
||||
print(f"性能提升: {speedup:.2f}x")
|
||||
print(f"时间减少: {(1 - new_time/old_time)*100:.1f}%")
|
||||
print()
|
||||
|
||||
# 详细对比
|
||||
if result_old['task_times'] and result_new['task_times']:
|
||||
old_avg = sum(result_old['task_times']) / len(result_old['task_times'])
|
||||
new_avg = sum(result_new['task_times']) / len(result_new['task_times'])
|
||||
|
||||
print("任务级性能对比:")
|
||||
print(f" 优化前平均任务耗时: {old_avg:.0f}ms")
|
||||
print(f" 优化后平均任务耗时: {new_avg:.0f}ms")
|
||||
print(f" 单任务性能提升: {old_avg/new_avg:.2f}x")
|
||||
print()
|
||||
|
||||
# 识别结果对比
|
||||
print("识别结果对比:")
|
||||
for old_result in result_old['results']:
|
||||
region_name = old_result['region']
|
||||
new_result = next((r for r in result_new['results'] if r['region'] == region_name), None)
|
||||
|
||||
if old_result['success'] and new_result and new_result['success']:
|
||||
old_text = old_result['text'][:20] if old_result['text'] else '(空)'
|
||||
new_text = new_result['text'][:20] if new_result['text'] else '(空)'
|
||||
match = "✓" if old_text == new_text else "✗"
|
||||
print(f" {match} {region_name}: 结果一致")
|
||||
|
||||
# 清理临时文件
|
||||
print()
|
||||
print("清理临时文件...")
|
||||
for info in region_images.values():
|
||||
try:
|
||||
if os.path.exists(info['path']):
|
||||
os.remove(info['path'])
|
||||
except (OSError, PermissionError):
|
||||
pass
|
||||
|
||||
print()
|
||||
print("=" * 70)
|
||||
print("测试完成")
|
||||
print("=" * 70)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,109 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
测试截图错误处理和重试机制
|
||||
"""
|
||||
|
||||
import time
|
||||
import sys
|
||||
import os
|
||||
import subprocess
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
# 添加项目根目录到路径
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
def test_adb_error_codes():
|
||||
"""测试ADB错误码处理"""
|
||||
|
||||
print("测试ADB错误码处理...")
|
||||
print("=" * 50)
|
||||
|
||||
from operations.screenshot_operations import ScreenshotOperations
|
||||
ops = ScreenshotOperations()
|
||||
|
||||
# 测试错误码4294967295的处理
|
||||
print("\n1. 测试错误码4294967295 (设备连接失败)")
|
||||
|
||||
try:
|
||||
# 模拟adb命令返回4294967295
|
||||
with patch('subprocess.Popen') as mock_popen:
|
||||
mock_process = MagicMock()
|
||||
mock_process.wait.return_value = 4294967295
|
||||
mock_process.stdout.read.side_effect = [b'', b''] # 空数据
|
||||
mock_popen.return_value = mock_process
|
||||
|
||||
result = ops._get_screenshot_direct("test_device")
|
||||
print(f"截图结果: {result}")
|
||||
print("预期: False (截图失败)")
|
||||
|
||||
except Exception as e:
|
||||
print(f"异常: {e}")
|
||||
|
||||
print("\n2. 测试正常截图流程")
|
||||
# 这里不实际测试,因为需要真实的adb环境
|
||||
print("跳过实际截图测试(需要真实设备)")
|
||||
|
||||
def test_retry_mechanism():
|
||||
"""测试重试机制"""
|
||||
|
||||
print("\n3. 测试重试机制模拟")
|
||||
|
||||
# 模拟3次失败后成功的情况
|
||||
retry_count = 0
|
||||
max_retries = 3
|
||||
success = False
|
||||
|
||||
for retry in range(max_retries):
|
||||
retry_count = retry + 1
|
||||
print(f"重试 {retry_count}/{max_retries}")
|
||||
|
||||
# 模拟前两次失败,第3次成功
|
||||
if retry < 2:
|
||||
print(" 截图失败")
|
||||
else:
|
||||
print(" 截图成功")
|
||||
success = True
|
||||
break
|
||||
|
||||
if retry < max_retries - 1:
|
||||
print(" 等待1秒后重试...")
|
||||
time.sleep(0.1) # 实际测试中用较短时间
|
||||
|
||||
print(f"\n最终结果: {'成功' if success else '失败'}")
|
||||
print(f"重试次数: {retry_count}")
|
||||
|
||||
def test_error_messages():
|
||||
"""测试错误消息"""
|
||||
|
||||
print("\n4. 测试错误消息")
|
||||
|
||||
# 模拟不同的错误情况
|
||||
error_scenarios = [
|
||||
(4294967295, "设备连接失败"),
|
||||
(1, "ADB命令错误"),
|
||||
("timeout", "操作超时"),
|
||||
("unknown", "未知错误")
|
||||
]
|
||||
|
||||
for error_code, expected_desc in error_scenarios:
|
||||
print(f"错误码 {error_code}: {expected_desc}")
|
||||
|
||||
print("\n错误处理改进:")
|
||||
print("- 添加了设备连接状态检查")
|
||||
print("- 区分不同类型的ADB错误")
|
||||
print("- 提供更详细的错误诊断信息")
|
||||
print("- 实现截图失败时的重试机制")
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_adb_error_codes()
|
||||
test_retry_mechanism()
|
||||
test_error_messages()
|
||||
|
||||
print("\n" + "=" * 50)
|
||||
print("截图错误处理测试完成")
|
||||
print("修复内容:")
|
||||
print("1. 添加ADB错误码4294967295的特殊处理")
|
||||
print("2. 改进错误消息,提供更详细的诊断信息")
|
||||
print("3. 在douyin_fudai.py中添加截图重试机制")
|
||||
print("4. 当截图失败时会重试3次,而不是直接放弃")
|
||||
@@ -1,92 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
测试OCR期间截图文件等待逻辑
|
||||
"""
|
||||
|
||||
import time
|
||||
import sys
|
||||
import os
|
||||
|
||||
# 添加项目根目录到路径
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
def test_screenshot_file_wait():
|
||||
"""测试截图文件等待逻辑"""
|
||||
|
||||
print("测试OCR期间截图文件等待逻辑...")
|
||||
|
||||
# 1. 模拟截图文件不存在的情况
|
||||
print("\n1. 模拟截图文件等待...")
|
||||
|
||||
try:
|
||||
import path_config
|
||||
pic_path = path_config.get_screenshot_path()
|
||||
except Exception:
|
||||
pic_path = os.path.join(os.path.dirname(__file__), "pic", "screenshot.png")
|
||||
|
||||
print(f"截图文件路径: {pic_path}")
|
||||
|
||||
# 检查文件是否存在
|
||||
if os.path.exists(pic_path):
|
||||
print("截图文件已存在")
|
||||
file_size = os.path.getsize(pic_path)
|
||||
print(f"文件大小: {file_size} bytes")
|
||||
else:
|
||||
print("截图文件不存在")
|
||||
|
||||
# 2. 模拟等待逻辑
|
||||
print("\n2. 测试等待逻辑...")
|
||||
|
||||
wait_start = time.time()
|
||||
max_wait = 3.0
|
||||
found = False
|
||||
|
||||
while time.time() - wait_start < max_wait:
|
||||
if os.path.exists(pic_path):
|
||||
file_size = os.path.getsize(pic_path)
|
||||
if file_size >= 1024: # 至少1KB
|
||||
found = True
|
||||
break
|
||||
time.sleep(0.1)
|
||||
|
||||
wait_time = time.time() - wait_start
|
||||
|
||||
if found:
|
||||
print(".2f")
|
||||
else:
|
||||
print(f"等待{max_wait}秒后仍未找到有效截图文件")
|
||||
|
||||
# 3. 分析问题
|
||||
print("\n3. 问题分析:")
|
||||
|
||||
issues = []
|
||||
|
||||
if not found:
|
||||
issues.append("[WARN] 等待期间未找到截图文件")
|
||||
else:
|
||||
issues.append("[OK] 成功等到截图文件")
|
||||
|
||||
# 检查文件完整性
|
||||
if os.path.exists(pic_path):
|
||||
try:
|
||||
with open(pic_path, 'rb') as f:
|
||||
header = f.read(4)
|
||||
if header == b'\x89PNG':
|
||||
issues.append("[OK] 截图文件格式正确 (PNG)")
|
||||
else:
|
||||
issues.append("[WARN] 截图文件格式异常")
|
||||
except Exception as e:
|
||||
issues.append(f"[ERROR] 读取截图文件失败: {e}")
|
||||
else:
|
||||
issues.append("[ERROR] 截图文件不存在")
|
||||
|
||||
for issue in issues:
|
||||
print(issue)
|
||||
|
||||
print(f"\n测试结果: {'PASS' if found else 'FAIL'}")
|
||||
return found
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = test_screenshot_file_wait()
|
||||
sys.exit(0 if success else 1)
|
||||
@@ -1,127 +0,0 @@
|
||||
#!/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()
|
||||
@@ -1,129 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
测试点击福袋后阶段识别与OCR共用截图的优化
|
||||
"""
|
||||
|
||||
import time
|
||||
import sys
|
||||
import os
|
||||
|
||||
# 添加项目根目录到路径
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
def test_screenshot_reuse_optimization():
|
||||
"""测试截图复用优化"""
|
||||
|
||||
print("测试点击福袋后阶段识别与OCR共用截图优化...")
|
||||
print("=" * 60)
|
||||
|
||||
# 1. 模拟阶段诊断检测到任务数量 > 0
|
||||
print("\n1. 模拟阶段诊断检测到任务数量 > 0")
|
||||
|
||||
class MockAnalyser:
|
||||
def __init__(self):
|
||||
self._fudai_detail_task_count_from_stage = 2
|
||||
self._screenshot_ready_for_ocr = False
|
||||
|
||||
def _log_kv(self, key, value, **kwargs):
|
||||
timestamp = time.strftime("%H:%M:%S", time.localtime())
|
||||
print(f"[{timestamp}] [{key}] {value}")
|
||||
|
||||
analyser = MockAnalyser()
|
||||
|
||||
# 模拟阶段诊断设置标志
|
||||
analyser._fudai_detail_task_count_from_stage = 2
|
||||
analyser._screenshot_ready_for_ocr = True
|
||||
|
||||
print(f"任务数量: {analyser._fudai_detail_task_count_from_stage}")
|
||||
print(f"截图复用标志: {analyser._screenshot_ready_for_ocr}")
|
||||
|
||||
# 2. 模拟OCR开始时的逻辑
|
||||
print("\n2. 模拟OCR开始时的截图复用检查")
|
||||
|
||||
# 检查是否可以复用阶段诊断时的截图
|
||||
screenshot_reused = False
|
||||
try:
|
||||
if getattr(analyser, '_screenshot_ready_for_ocr', False):
|
||||
analyser._screenshot_ready_for_ocr = False # 清除标志,避免重复使用
|
||||
screenshot_reused = True
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"福袋详情",
|
||||
"read_fudai_detail_once:复用阶段诊断截图",
|
||||
reason="stage_diagnosed_task_count_gt_zero"
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
print(f"截图是否复用: {screenshot_reused}")
|
||||
print(f"清除后的复用标志: {analyser._screenshot_ready_for_ocr}")
|
||||
|
||||
# 3. 验证优化效果
|
||||
print("\n3. 验证优化效果")
|
||||
|
||||
if screenshot_reused:
|
||||
print("[OK] 成功复用阶段诊断时的截图,避免重复截图")
|
||||
print("[OK] OCR跳过了get_screenshot调用")
|
||||
print("[OK] 减少了一次不必要的截图操作")
|
||||
success = True
|
||||
else:
|
||||
print("[FAIL] 截图复用失败,仍会进行新的截图")
|
||||
success = False
|
||||
|
||||
# 4. 模拟任务数量为0的情况
|
||||
print("\n4. 模拟任务数量为0的情况")
|
||||
|
||||
analyser2 = MockAnalyser()
|
||||
analyser2._fudai_detail_task_count_from_stage = 0
|
||||
analyser2._screenshot_ready_for_ocr = False # 任务数量为0时不应该设置复用标志
|
||||
|
||||
screenshot_reused_2 = False
|
||||
try:
|
||||
if getattr(analyser2, '_screenshot_ready_for_ocr', False):
|
||||
analyser2._screenshot_ready_for_ocr = False
|
||||
screenshot_reused_2 = True
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
print(f"任务数量: {analyser2._fudai_detail_task_count_from_stage}")
|
||||
print(f"截图复用标志: {analyser2._screenshot_ready_for_ocr}")
|
||||
print(f"截图是否复用: {screenshot_reused_2}")
|
||||
|
||||
if not screenshot_reused_2:
|
||||
print("[OK] 任务数量为0时正确不复用截图")
|
||||
else:
|
||||
print("[FAIL] 任务数量为0时错误复用截图")
|
||||
success = False
|
||||
|
||||
# 5. 分析性能提升
|
||||
print("\n5. 性能提升分析")
|
||||
print("-" * 40)
|
||||
|
||||
print("优化前:")
|
||||
print("- 阶段诊断: 1次截图")
|
||||
print("- OCR识别: 1次截图")
|
||||
print("- 总计: 2次截图")
|
||||
|
||||
print("\n优化后:")
|
||||
print("- 阶段诊断: 1次截图")
|
||||
print("- OCR识别: 复用阶段诊断截图")
|
||||
print("- 总计: 1次截图")
|
||||
|
||||
print("\n性能提升:")
|
||||
print("- 减少50%的截图操作")
|
||||
print("- 减少截图等待时间")
|
||||
print("- 降低系统负载")
|
||||
|
||||
print(f"\n{'='*60}")
|
||||
print(f"测试结果: {'PASS' if success else 'FAIL'}")
|
||||
print("优化目标: 当阶段诊断发现任务数量>0时,OCR复用同一张截图")
|
||||
print(f"{'='*60}")
|
||||
|
||||
return success
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = test_screenshot_reuse_optimization()
|
||||
sys.exit(0 if success else 1)
|
||||
@@ -1,59 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
测试截图服务修复效果
|
||||
"""
|
||||
|
||||
import time
|
||||
import sys
|
||||
import os
|
||||
|
||||
# 添加项目根目录到路径
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
def test_service_startup():
|
||||
"""测试截图服务启动"""
|
||||
print("测试截图服务启动修复...")
|
||||
|
||||
# 模拟启动过程
|
||||
from douyin.core.douyin_fudai import fudai_analyse
|
||||
|
||||
# 创建分析器实例
|
||||
analyser_instance = fudai_analyse()
|
||||
|
||||
print("启动fudai_choujiang(只运行几秒钟测试服务启动)...")
|
||||
|
||||
try:
|
||||
# 设置设备ID(需要根据实际情况修改)
|
||||
device_id = "emulator-5554" # 或者其他设备ID
|
||||
|
||||
# 启动服务(只运行5秒)
|
||||
analyser_instance.fudai_choujiang(
|
||||
device_id=device_id,
|
||||
wait_minutes=0, # 不等待,直接测试启动
|
||||
)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("测试被中断")
|
||||
except Exception as e:
|
||||
print(f"测试过程中出现异常: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
# 检查服务状态
|
||||
print("\n检查服务状态:")
|
||||
from operations.screenshot_service import ScreenshotService
|
||||
service = ScreenshotService.get_instance()
|
||||
info = service.get_cache_info()
|
||||
|
||||
print(f"服务运行中: {info['running']}")
|
||||
print(f"设备ID: {info['device_id']}")
|
||||
print(f"后台线程: {'运行中' if hasattr(service, '_thread') and service._thread and service._thread.is_alive() else '未运行'}")
|
||||
|
||||
# 停止服务
|
||||
if service.is_running():
|
||||
service.stop()
|
||||
print("服务已停止")
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_service_startup()
|
||||
@@ -1,91 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
测试OCR时停止后台截图服务的优化效果
|
||||
"""
|
||||
|
||||
import time
|
||||
import sys
|
||||
import os
|
||||
|
||||
# 添加项目根目录到路径
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
def test_screenshot_service_ocr_integration():
|
||||
"""测试截图服务与OCR集成的优化"""
|
||||
print("=" * 80)
|
||||
print("测试OCR时停止后台截图服务的优化")
|
||||
print("=" * 80)
|
||||
|
||||
# 1. 启动截图服务
|
||||
print("\n1. 启动后台截图服务...")
|
||||
from operations.screenshot_service import ScreenshotService
|
||||
service = ScreenshotService.get_instance()
|
||||
|
||||
# 模拟设备ID(实际测试时需要替换为真实设备ID)
|
||||
device_id = "emulator-5554"
|
||||
|
||||
if service.is_running():
|
||||
print("服务已在运行")
|
||||
else:
|
||||
service.start(device_id, interval=1.5)
|
||||
time.sleep(0.5) # 等待服务启动
|
||||
|
||||
if service.is_running():
|
||||
print("[OK] 后台截图服务启动成功")
|
||||
else:
|
||||
print("[FAIL] 后台截图服务启动失败")
|
||||
return
|
||||
|
||||
# 2. 模拟OCR密集操作
|
||||
print("\n2. 模拟OCR密集操作(调用read_fudai_detail_once)...")
|
||||
|
||||
# 创建一个模拟的analyser对象
|
||||
class MockAnalyser:
|
||||
def __init__(self):
|
||||
self.device_id = device_id
|
||||
self._fudai_detail_task_count_from_stage = 2
|
||||
|
||||
def _log_kv(self, key, value, **kwargs):
|
||||
timestamp = time.strftime("%H:%M:%S", time.localtime())
|
||||
print(f"[{timestamp}] [{key}] {value}")
|
||||
|
||||
analyser = MockAnalyser()
|
||||
|
||||
# 调用read_fudai_detail_once,这应该会停止后台服务
|
||||
start_time = time.time()
|
||||
try:
|
||||
from fudai_detail_modules.fudai_detail_reader import read_fudai_detail_once
|
||||
result = read_fudai_detail_once(analyser)
|
||||
elapsed = time.time() - start_time
|
||||
print(".2f")
|
||||
print(f" 返回状态: {result[0]}")
|
||||
except Exception as e:
|
||||
elapsed = time.time() - start_time
|
||||
print(".2f")
|
||||
print(f" 错误: {e}")
|
||||
|
||||
# 3. 检查服务是否重新启动
|
||||
print("\n3. 检查后台截图服务是否重新启动...")
|
||||
time.sleep(0.5) # 等待服务重新启动
|
||||
|
||||
if service.is_running():
|
||||
print("[OK] 后台截图服务已自动重新启动")
|
||||
info = service.get_cache_info()
|
||||
print(f" 设备ID: {info['device_id']}")
|
||||
print(f" 截图间隔: {info['interval']}秒")
|
||||
print(f" 缓存有效期: {info['cache_valid_duration']}秒")
|
||||
else:
|
||||
print("[FAIL] 后台截图服务未重新启动")
|
||||
|
||||
# 4. 清理
|
||||
if service.is_running():
|
||||
service.stop()
|
||||
print("\n4. 测试完成,停止后台截图服务")
|
||||
|
||||
print("\n" + "=" * 80)
|
||||
print("测试完成")
|
||||
print("=" * 80)
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_screenshot_service_ocr_integration()
|
||||
@@ -1,84 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
简单测试:验证OCR时停止后台截图服务的代码修改
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# 添加项目根目录到路径
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
def test_code_modification():
|
||||
"""测试代码修改是否正确"""
|
||||
print("测试OCR时停止后台截图服务的代码修改...")
|
||||
|
||||
# 1. 检查文件是否存在
|
||||
file_path = "fudai_detail_modules/fudai_detail_reader.py"
|
||||
if not os.path.exists(file_path):
|
||||
print(f"[FAIL] 文件不存在: {file_path}")
|
||||
return False
|
||||
|
||||
# 2. 检查代码中是否包含停止服务的逻辑
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
# 检查关键代码片段
|
||||
checks = [
|
||||
("停止服务代码", "service.stop()" in content),
|
||||
("启动服务代码", "service.start(" in content),
|
||||
("screenshot_service_stopped变量", "screenshot_service_stopped" in content),
|
||||
("finally块", "finally:" in content),
|
||||
("ScreenshotService导入", "from operations.screenshot_service import ScreenshotService" in content),
|
||||
]
|
||||
|
||||
all_passed = True
|
||||
for check_name, result in checks:
|
||||
if result:
|
||||
print(f"[OK] {check_name}")
|
||||
else:
|
||||
print(f"[FAIL] {check_name}")
|
||||
all_passed = False
|
||||
|
||||
# 3. 检查语法是否正确
|
||||
try:
|
||||
compile(content, file_path, 'exec')
|
||||
print("[OK] 语法检查通过")
|
||||
except SyntaxError as e:
|
||||
print(f"[FAIL] 语法错误: {e}")
|
||||
all_passed = False
|
||||
|
||||
# 4. 检查导入是否工作
|
||||
read_fudai_detail_once = None # 初始化变量
|
||||
try:
|
||||
from fudai_detail_modules.fudai_detail_reader import read_fudai_detail_once
|
||||
print("[OK] 模块导入成功")
|
||||
except ImportError as e:
|
||||
print(f"[FAIL] 模块导入失败: {e}")
|
||||
all_passed = False
|
||||
|
||||
# 5. 检查函数签名
|
||||
import inspect
|
||||
try:
|
||||
if read_fudai_detail_once is not None:
|
||||
sig = inspect.signature(read_fudai_detail_once)
|
||||
print(f"[OK] 函数签名: {sig}")
|
||||
else:
|
||||
print("[SKIP] 函数未导入,跳过签名检查")
|
||||
except Exception as e:
|
||||
print(f"[FAIL] 函数签名检查失败: {e}")
|
||||
all_passed = False
|
||||
|
||||
print()
|
||||
if all_passed:
|
||||
print("[SUCCESS] 代码修改验证通过!")
|
||||
print("OCR密集操作时会自动停止后台截图服务,避免资源冲突。")
|
||||
else:
|
||||
print("[FAIL] 代码修改存在问题,请检查!")
|
||||
|
||||
return all_passed
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = test_code_modification()
|
||||
sys.exit(0 if success else 1)
|
||||
@@ -1,113 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
测试OCR时停止后台截图服务的功能
|
||||
"""
|
||||
|
||||
import time
|
||||
import sys
|
||||
import os
|
||||
|
||||
# 添加项目根目录到路径
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
def test_screenshot_service_stop_during_ocr():
|
||||
"""测试OCR过程中后台截图服务是否被停止"""
|
||||
|
||||
print("测试OCR时停止后台截图服务功能...")
|
||||
|
||||
# 1. 启动截图服务
|
||||
print("\n1. 启动后台截图服务...")
|
||||
from operations.screenshot_service import ScreenshotService
|
||||
service = ScreenshotService.get_instance()
|
||||
|
||||
# 模拟设备ID
|
||||
device_id = "emulator-5554"
|
||||
|
||||
if not service.is_running():
|
||||
service.start(device_id, interval=1.5)
|
||||
time.sleep(0.5)
|
||||
|
||||
if service.is_running():
|
||||
print("[OK] 后台截图服务已启动")
|
||||
else:
|
||||
print("[FAIL] 后台截图服务启动失败")
|
||||
return False
|
||||
|
||||
# 记录初始状态
|
||||
initial_cache_duration = service._cache_valid_duration
|
||||
print(f"初始缓存有效期: {initial_cache_duration}秒")
|
||||
|
||||
# 2. 模拟OCR操作
|
||||
print("\n2. 模拟OCR操作...")
|
||||
|
||||
# 创建模拟的analyser
|
||||
class MockAnalyser:
|
||||
def __init__(self):
|
||||
self.device_id = device_id
|
||||
self._fudai_detail_task_count_from_stage = 2
|
||||
|
||||
def _log_kv(self, key, value, **kwargs):
|
||||
timestamp = time.strftime("%H:%M:%S", time.localtime())
|
||||
print(f"[{timestamp}] [{key}] {value}")
|
||||
|
||||
analyser = MockAnalyser()
|
||||
|
||||
# 记录开始时的服务状态
|
||||
service_running_before = service.is_running()
|
||||
print(f"OCR开始前服务运行状态: {service_running_before}")
|
||||
|
||||
start_time = time.time() # 初始化开始时间
|
||||
try:
|
||||
# 调用read_fudai_detail_once - 这应该会停止后台服务
|
||||
from fudai_detail_modules.fudai_detail_reader import read_fudai_detail_once
|
||||
|
||||
start_time = time.time()
|
||||
result = read_fudai_detail_once(analyser)
|
||||
elapsed = time.time() - start_time
|
||||
|
||||
print(".2f")
|
||||
print(f" 返回状态: {result[0]}")
|
||||
|
||||
except Exception as e:
|
||||
elapsed = time.time() - start_time
|
||||
print(".2f")
|
||||
print(f" 错误: {e}")
|
||||
|
||||
# 3. 检查OCR后的服务状态
|
||||
print("\n3. 检查OCR后的服务状态...")
|
||||
time.sleep(0.5) # 等待服务状态稳定
|
||||
|
||||
service_running_after = service.is_running()
|
||||
final_cache_duration = service._cache_valid_duration
|
||||
|
||||
print(f"OCR结束后服务运行状态: {service_running_after}")
|
||||
print(f"最终缓存有效期: {final_cache_duration}秒")
|
||||
|
||||
# 4. 验证结果
|
||||
success = True
|
||||
|
||||
if service_running_before and not service_running_after:
|
||||
print("[OK] 后台截图服务在OCR期间被正确停止")
|
||||
elif not service_running_before:
|
||||
print("[INFO] 后台截图服务在OCR开始前就已经停止")
|
||||
else:
|
||||
print("[FAIL] 后台截图服务在OCR期间没有被停止")
|
||||
success = False
|
||||
|
||||
if final_cache_duration == initial_cache_duration:
|
||||
print("[OK] 缓存有效期在OCR结束后被正确恢复")
|
||||
else:
|
||||
print(f"[WARNING] 缓存有效期未恢复: 期望{initial_cache_duration}, 实际{final_cache_duration}")
|
||||
|
||||
# 5. 清理
|
||||
if service.is_running():
|
||||
service.stop()
|
||||
print("\n5. 测试完成,停止后台截图服务")
|
||||
|
||||
print(f"\n测试结果: {'PASS' if success else 'FAIL'}")
|
||||
return success
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = test_screenshot_service_stop_during_ocr()
|
||||
sys.exit(0 if success else 1)
|
||||
@@ -1,44 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
测试time导入修复
|
||||
"""
|
||||
|
||||
import time
|
||||
|
||||
def test_time_import():
|
||||
"""测试time模块导入和使用"""
|
||||
|
||||
print("测试time模块导入和使用...")
|
||||
|
||||
# 模拟read_fudai_detail_once函数的开始部分
|
||||
try:
|
||||
start_time = time.time()
|
||||
print(".3f")
|
||||
except Exception as e:
|
||||
print(f"[ERROR] time.time()调用失败: {e}")
|
||||
return False
|
||||
|
||||
# 模拟函数内部的逻辑
|
||||
try:
|
||||
task_count = 0
|
||||
# 清除标志
|
||||
# 模拟等待逻辑(之前有问题的部分)
|
||||
wait_start = time.time()
|
||||
print(".3f")
|
||||
# 模拟循环
|
||||
while time.time() - wait_start < 0.1: # 很短的时间
|
||||
pass
|
||||
|
||||
elapsed = time.time() - start_time
|
||||
print(".3f")
|
||||
except Exception as e:
|
||||
print(f"[ERROR] 函数内部time使用失败: {e}")
|
||||
return False
|
||||
|
||||
print("[OK] time模块导入和使用正常")
|
||||
return True
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = test_time_import()
|
||||
print(f"\n测试结果: {'PASS' if success else 'FAIL'}")
|
||||
Reference in new issue
Block a user