- Removed excessive blank lines from multiple Python scripts and configuration files to improve readability and maintainability. - Ensured consistent formatting across the project, contributing to a more organized codebase. - Added new files for testing and analysis, including `test_screenshot_error_handling.py` and `test_screenshot_reuse_optimization.py`, to improve testing coverage and functionality.
329 lines
10 KiB
Python
329 lines
10 KiB
Python
#!/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()
|
|
|
|
|
|
|