From 78a75d48439636df626d08d98ee9a57785b5866d Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 19 Jun 2026 14:44:17 +0800 Subject: [PATCH] Tune fudai detail OCR strategy --- douyin/core/fudai_modules/init_helpers.py | 2 + douyin/fudai/douyin_fudai_contain_impl.py | 75 ++++++++++++++++++++++- douyin/utils/douyin_config.py | 10 +++ tests/test_fudai_detail_ui_v2.py | 61 ++++++++++++++++++ 4 files changed, 145 insertions(+), 3 deletions(-) diff --git a/douyin/core/fudai_modules/init_helpers.py b/douyin/core/fudai_modules/init_helpers.py index a1224d3..d94633c 100644 --- a/douyin/core/fudai_modules/init_helpers.py +++ b/douyin/core/fudai_modules/init_helpers.py @@ -84,6 +84,8 @@ def apply_fudai_defaults(self: Any) -> None: self.fudai_detail_ocr_live_title_enabled = False self.fudai_detail_task_ocr_in_critical_path_enabled = False self.fudai_detail_countdown_full_ocr_in_critical_path_enabled = False + self.fudai_detail_info_line_ocr_in_critical_path_enabled = False + self.fudai_detail_ocr_workers = 1 self.pixel_detect_fudai_icon_enabled = True self.pixel_detect_coupon_popup_enabled = True self.pixel_detect_shopping_redpacket_enabled = True diff --git a/douyin/fudai/douyin_fudai_contain_impl.py b/douyin/fudai/douyin_fudai_contain_impl.py index 424a7cd..ec57eb0 100644 --- a/douyin/fudai/douyin_fudai_contain_impl.py +++ b/douyin/fudai/douyin_fudai_contain_impl.py @@ -147,6 +147,45 @@ def _get_bool_setting(analyser: Any, key: str, default: bool = False) -> bool: return default +def _get_int_setting( + analyser: Any, + key: str, + default: int, + *, + min_value: int = 1, + max_value: int = 8, +) -> int: + try: + if hasattr(analyser, key): + value = int(getattr(analyser, key)) + return max(min_value, min(max_value, value)) + except Exception: + pass + try: + cfg_path = path_config.get_config_path() + if cfg_path and os.path.exists(cfg_path): + with open(cfg_path, "r", encoding="utf-8") as f: + cfg = json.load(f) + if isinstance(cfg, dict) and key in cfg: + value = int(cfg.get(key)) + return max(min_value, min(max_value, value)) + except Exception: + pass + return max(min_value, min(max_value, int(default))) + + +def _get_detail_ocr_workers(analyser: Any, fallback_workers: int) -> int: + """福袋详情 OCR 默认串行,避免多 Paddle 实例抢 CPU 导致尾延迟放大。""" + _ = fallback_workers + return _get_int_setting( + analyser, + "fudai_detail_ocr_workers", + 1, + min_value=1, + max_value=3, + ) + + def _should_schedule_task_list_ocr(analyser: Any, ocr_switches: Any) -> bool: """Keep the expensive task-list OCR out of the detail critical path by default.""" try: @@ -175,6 +214,19 @@ def _should_schedule_countdown_full_ocr(analyser: Any, ocr_switches: Any) -> boo ) +def _should_schedule_info_line_ocr(analyser: Any, ocr_switches: Any) -> bool: + """The one-task participants/countdown line is expensive; keep it opt-in.""" + try: + _ = bool(getattr(ocr_switches, "countdown", False)) + except Exception: + return False + return _get_bool_setting( + analyser, + "fudai_detail_info_line_ocr_in_critical_path_enabled", + False, + ) + + def _slowest_ocr_task(durations: Dict[str, Any]) -> str: if not durations: return "" @@ -619,6 +671,10 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo analyser, ocr_switches, ) + info_line_ocr_in_critical_path = _should_schedule_info_line_ocr( + analyser, + ocr_switches, + ) try: setattr(analyser, "fudai_detail_ui_version_active", ui_version) @@ -900,7 +956,10 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo # 使用动态线程池大小,根据CPU核心数优化,并复用线程池 from operations.screenshot_service import get_optimal_workers - optimal_workers = get_optimal_workers() + optimal_workers = _get_detail_ocr_workers( + analyser, + get_optimal_workers(), + ) executor = _get_fudai_ocr_executor(optimal_workers) task_funcs = {} @@ -924,11 +983,18 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo ) ) info_line_raw = None - if renwu == 1 and (ocr_switches.participants or ocr_switches.countdown): + if renwu == 1 and info_line_ocr_in_critical_path: info_line_box = tuple(profile.get("info_line") or ()) if len(info_line_box) == 4: analyser.cut_pic(*info_line_box, "", "福袋人数倒计时信息", analyser.y_pianyi) task_funcs["info_line"] = ocr_info_line + elif renwu == 1 and (ocr_switches.participants or ocr_switches.countdown): + analyser._log_kv( + "性能", + "跳过单任务人数倒计时整行OCR关键路径", + reason="info_line_ocr_not_in_critical_path", + enable_key="fudai_detail_info_line_ocr_in_critical_path_enabled", + ) # 并行补充倒计时额外候选(保持并行,不降级) for meta in countdown_candidate_meta: if meta.get("is_primary"): @@ -1324,7 +1390,10 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo # 使用动态线程池大小,并复用线程池 from operations.screenshot_service import get_optimal_workers - optimal_workers = get_optimal_workers() + optimal_workers = _get_detail_ocr_workers( + analyser, + get_optimal_workers(), + ) executor = _get_fudai_ocr_executor(optimal_workers) task_funcs = {} diff --git a/douyin/utils/douyin_config.py b/douyin/utils/douyin_config.py index 7264b0c..1a34de1 100644 --- a/douyin/utils/douyin_config.py +++ b/douyin/utils/douyin_config.py @@ -414,6 +414,7 @@ class ConfigManager: "fudai_detail_ocr_live_title_enabled": False, "fudai_detail_task_ocr_in_critical_path_enabled": False, "fudai_detail_countdown_full_ocr_in_critical_path_enabled": False, + "fudai_detail_info_line_ocr_in_critical_path_enabled": False, }.items(): if key in config: setattr(analyser, key, bool(config.get(key, default))) @@ -433,6 +434,15 @@ class ConfigManager: if "enable_whole_detail_ocr" in config: analyser.enable_whole_detail_ocr = bool(config["enable_whole_detail_ocr"]) + if "fudai_detail_ocr_workers" in config: + try: + analyser.fudai_detail_ocr_workers = max( + 1, + min(3, int(config.get("fudai_detail_ocr_workers") or 1)), + ) + except Exception: + analyser.fudai_detail_ocr_workers = 1 + # ===== 推荐页养号相关配置 ===== if "yanghao_enabled" in config: analyser.yanghao_enabled = bool(config["yanghao_enabled"]) diff --git a/tests/test_fudai_detail_ui_v2.py b/tests/test_fudai_detail_ui_v2.py index b9fe1ed..d36df30 100644 --- a/tests/test_fudai_detail_ui_v2.py +++ b/tests/test_fudai_detail_ui_v2.py @@ -350,6 +350,67 @@ def test_countdown_full_ocr_critical_path_requires_explicit_opt_in(monkeypatch): assert _should_schedule_countdown_full_ocr(analyser, disabled_switches) is False +def test_info_line_ocr_is_not_in_detail_critical_path_by_default(monkeypatch): + from douyin.fudai.douyin_fudai_contain_impl import ( + _should_schedule_info_line_ocr, + ) + from douyin.fudai.douyin_fudai_core_pkg.ocr_switches import ( + resolve_fudai_detail_ocr_switches, + ) + + monkeypatch.setattr( + "douyin.core.path_config.get_config_path", + lambda: "__missing__.json", + ) + switches = resolve_fudai_detail_ocr_switches( + {"fudai_detail_ocr_countdown_enabled": True} + ) + + assert _should_schedule_info_line_ocr(SimpleNamespace(), switches) is False + + +def test_info_line_ocr_critical_path_requires_explicit_opt_in(monkeypatch): + from douyin.fudai.douyin_fudai_contain_impl import ( + _should_schedule_info_line_ocr, + ) + from douyin.fudai.douyin_fudai_core_pkg.ocr_switches import ( + resolve_fudai_detail_ocr_switches, + ) + + monkeypatch.setattr( + "douyin.core.path_config.get_config_path", + lambda: "__missing__.json", + ) + switches = resolve_fudai_detail_ocr_switches( + {"fudai_detail_ocr_countdown_enabled": True} + ) + + analyser = SimpleNamespace( + fudai_detail_info_line_ocr_in_critical_path_enabled=True, + ) + + assert _should_schedule_info_line_ocr(analyser, switches) is True + + +def test_detail_ocr_workers_default_to_one_and_clamp(monkeypatch): + from douyin.fudai.douyin_fudai_contain_impl import _get_detail_ocr_workers + + monkeypatch.setattr( + "douyin.core.path_config.get_config_path", + lambda: "__missing__.json", + ) + + assert _get_detail_ocr_workers(SimpleNamespace(), 8) == 1 + assert _get_detail_ocr_workers( + SimpleNamespace(fudai_detail_ocr_workers=2), + 8, + ) == 2 + assert _get_detail_ocr_workers( + SimpleNamespace(fudai_detail_ocr_workers=99), + 8, + ) == 3 + + def test_config_manager_loads_fudai_detail_ui_and_ocr_switches(tmp_path): from douyin.utils.douyin_config import ConfigManager