Files
douyin_guaji-main/test_ocr_multithread.py
T
Your Name 07c387751d chore: Clean up blank lines across multiple files to enhance readability
- Removed excessive blank lines in various Python scripts, markdown files, and configuration files to improve overall code clarity and maintainability.
- Ensured consistent formatting throughout the project, contributing to a more organized codebase.
2025-12-31 17:00:58 +08:00

103 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/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()