From 735a04722dd05af928be38208d750ce458c49d11 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 12 Jan 2026 14:08:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E7=A6=8F=E8=A2=8B?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E8=BF=87=E6=BB=A4=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E5=87=8C=E6=99=A8=E6=97=B6=E6=AE=B5=E4=B8=8D?= =?UTF-8?q?=E8=BF=87=E6=BB=A4=E7=9A=84=E5=88=A4=E6=96=AD=EF=BC=9B=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E8=AF=A6=E7=BB=86=E6=97=B6=E9=97=B4=E5=88=86=E6=9E=90?= =?UTF-8?q?=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- douyin/fudai/core/fudai_filter.py | 20 +--- douyin/fudai/douyin_fudai_core.py | 20 +--- time_analysis_result.txt | 33 ++++++ tools/detailed_time_analysis.py | 173 ++++++++++++++++++++++++++++++ 4 files changed, 208 insertions(+), 38 deletions(-) create mode 100644 time_analysis_result.txt create mode 100644 tools/detailed_time_analysis.py diff --git a/douyin/fudai/core/fudai_filter.py b/douyin/fudai/core/fudai_filter.py index 4d511ee..f19c5c9 100644 --- a/douyin/fudai/core/fudai_filter.py +++ b/douyin/fudai/core/fudai_filter.py @@ -14,26 +14,8 @@ def check_contain(analyser, contains: str = "") -> bool: 返回值语义(调整后): - True : "建议丢弃"(内容命中黑名单 contains_not_want) - - False : "可以保留"(未命中黑名单,或凌晨时段不做内容过滤) + - False : "可以保留"(未命中黑名单) """ - try: - hour = int(analyser.operation.get_current_hour()) - except Exception: - hour = -1 - - # 凌晨:不对内容做要求(不过滤) - if 0 < hour < 7: - try: - analyser._log_kv( - "筛选", - "凌晨不过滤福袋内容", - hour=hour, - contains=contains[:50] if contains else "", - ) - except Exception: - pass - return False - try: for contain in getattr(analyser, "contains_not_want", []) or []: if contain and contain in (contains or ""): diff --git a/douyin/fudai/douyin_fudai_core.py b/douyin/fudai/douyin_fudai_core.py index 7d1260e..81edc28 100644 --- a/douyin/fudai/douyin_fudai_core.py +++ b/douyin/fudai/douyin_fudai_core.py @@ -2075,7 +2075,7 @@ def check_contain(analyser, contains: str = "") -> bool: 返回值语义(调整后): - True : “建议丢弃”(内容命中黑名单 contains_not_want) - - False : “可以保留”(未命中黑名单,或凌晨时段不做内容过滤) + - False : “可以保留”(未命中黑名单) """ # 以下抑制为静态检查方便添加: @@ -2083,24 +2083,6 @@ def check_contain(analyser, contains: str = "") -> bool: # - W0718: broad-exception-caught(代码中大量使用 broad except 用于鲁棒性) # 这些抑制只影响静态检查,不改变运行时行为。 # pylint: disable=W0212,W0718 - try: - hour = int(analyser.operation.get_current_hour()) - except Exception: - hour = -1 - - # 凌晨:不对内容做要求(不过滤) - if 0 < hour < 7: - try: - analyser._log_kv( - "筛选", - "凌晨不过滤福袋内容", - hour=hour, - contains=contains[:50] if contains else "", - ) - except Exception: - pass - return False - try: for contain in getattr(analyser, "contains_not_want", []) or []: if contain and contain in (contains or ""): diff --git a/time_analysis_result.txt b/time_analysis_result.txt new file mode 100644 index 0000000..d5062cd --- /dev/null +++ b/time_analysis_result.txt @@ -0,0 +1,33 @@ +Reading logs\actions.log... +Reading logs\unified_2026-01-12_003546.log... +Reading logs\unified_2026-01-12.log... +Reading logs\unified_2026-01-11_230527.log... +----------------------------------------------------------------------------------------------------------------------- +Operation | Count | Avg | P50 | P80 | P95 | Max +----------------------------------------------------------------------------------------------------------------------- +[流程] run_once_basic | 142 | 14831.2 | 16210.0 | 23148.0 | 33541.5 | 55170.0 +[福袋查找] 福袋查找 | 100 | 11896.8 | 9166.0 | 15917.6 | 25612.3 | 38443.0 +[流程] handle_live_room_has_or_not_fudai | 100 | 11895.5 | 9165.0 | 15920.0 | 25610.5 | 38440.0 +[流程] handle_in_fudai_detail | 39 | 8981.0 | 6660.0 | 10746.0 | 21919.0 | 30030.0 +[参与] basic_join_decision | 24 | 7505.0 | 7070.0 | 8216.0 | 20918.0 | 27350.0 +[预约] [预约] process_reservation_attend_window 返回结果 | 7 | 7118.6 | 4890.0 | 6434.0 | 17838.0 | 22620.0 +[流程] find_and_open_fudai_detail | 100 | 5762.4 | 5135.0 | 6878.0 | 8632.5 | 10650.0 +[流程] 检测到福袋图标 | 318 | 3632.8 | 4305.0 | 4870.0 | 5060.0 | 5730.0 +[ACTION] return to recommend and reenter live | 23 | 3525.2 | 19.9 | 32.9 | 15656.4 | 63200.0 +[福袋详情] read_fudai_detail_with_retries | 44 | 2899.8 | 2840.0 | 2944.0 | 3068.5 | 4870.0 +[福袋详情] read_fudai_detail_once | 44 | 2039.3 | 1980.0 | 2078.0 | 2208.5 | 4000.0 +[福袋详情] 完整识别结果 | 44 | 2013.4 | 1940.0 | 2070.0 | 2140.0 | 4000.0 +[ACTION] 上滑切换直播间 | 8797 | 1473.9 | 1700.0 | 2160.0 | 2460.0 | 73260.0 +[截图] 获取屏幕截图 | 3224 | 1383.2 | 1340.8 | 1619.9 | 1955.4 | 2472.6 +[操作] 上滑切换直播间 | 110 | 846.4 | 510.0 | 532.0 | 1503.0 | 11990.0 +[OCR] 等待开奖期间OCR检测 | 3 | 810.0 | 810.0 | 858.0 | 882.0 | 890.0 +[预约] 预约检查 | 119 | 550.0 | 16.0 | 22.4 | 3580.8 | 22630.0 +[ACTION] 下滑回退直播间 | 3068 | 531.6 | 490.0 | 510.0 | 860.0 | 11910.0 +[操作] 点击操作 | 56 | 212.1 | 210.0 | 230.0 | 240.0 | 250.0 +[筛选] should_skip_fudai_by_config | 53 | 25.8 | 30.0 | 40.0 | 50.0 | 60.0 +[预约] [预约] add_reservation_core_logic | 19 | 25.8 | 20.0 | 40.0 | 51.0 | 60.0 +[预约] [预约] cleanup_expired_reservations | 27 | 7.4 | 0.0 | 8.0 | 40.0 | 40.0 +[[福袋查找] 像素检测] 实际福袋区域命中 | 45 | 0.2 | 0.0 | 0.0 | 0.0 | 10.0 +[[福袋查找] 像素检测] 实际福袋区域未命中 | 473 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 +[预约] [预约] | 119 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 +----------------------------------------------------------------------------------------------------------------------- diff --git a/tools/detailed_time_analysis.py b/tools/detailed_time_analysis.py new file mode 100644 index 0000000..d4c831c --- /dev/null +++ b/tools/detailed_time_analysis.py @@ -0,0 +1,173 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +Detailed time analysis for logs. +Calculates Average, P80, Min, Max for various operations. +""" + +import os +import re +import sys +import numpy as np +from collections import defaultdict +from datetime import datetime + +# Ensure UTF-8 output on Windows +if sys.platform == "win32": + try: + if hasattr(sys.stdout, 'reconfigure'): + sys.stdout.reconfigure(encoding='utf-8') + except Exception: + pass + +def parse_logs(log_paths): + data = defaultdict(list) + + # 更通用的正则,适应 actions.log 和 unified log + # 匹配: [GUI] - Tag [OP] Msg ... 耗时:Val + gui_op_re = re.compile(r'\[GUI\]\s*-\s*(.*?)\s*\[OP\]\s*(.*?)\s*(?:elapsed_seconds|elapsed_ms|耗时[:=])\s*=?\s*([\d.]+)([ms]?)', re.I) + + # 匹配: [Tag] Msg ... 耗时:Val (非 [GUI] 开头的格式) + tag_op_re = re.compile(r'\[([^\]\d]{2,15})\]\s*(.*?)\s*(?:elapsed_seconds|elapsed_ms|耗时[:=])\s*=?\s*([\d.]+)([ms]?)', re.I) + + exclude_tags = {'INFO', 'DEBUG', 'ERROR', 'WARN', 'WARNING', 'TRACE', 'GUI', 'ACTIONLOGGER'} + + for log_path in log_paths: + if not os.path.exists(log_path): + continue + + print(f"Reading {log_path}...") + with open(log_path, 'r', encoding='utf-8', errors='replace') as f: + for line in f: + tag, msg, val_str, unit = None, None, None, None + + # 尝试匹配 [GUI] - Tag [OP] 格式 + m = gui_op_re.search(line) + if m: + tag, msg, val_str, unit = m.groups() + else: + # 尝试匹配 [Tag] 格式 + m = tag_op_re.search(line) + if m: + tag, msg, val_str, unit = m.groups() + + if tag and msg: + if tag.upper() in exclude_tags: + continue + + val = float(val_str) + is_ms = False + if unit.lower() in ['ms', 'm']: + is_ms = True + elif '耗时=' in line and val > 5: # The bug + is_ms = True + + value = val if is_ms else val * 1000 + + # 清理 Msg + # 避免使用 '.' 在正则中未转义的情况 + clean_msg = msg + # 1. 移除 (等待开奖中) 这种括号内容 + clean_msg = re.sub(r'(.*?等待开奖.*?)', '', clean_msg) + clean_msg = re.sub(r'\(.*?等待开奖.*?\)', '', clean_msg) + + # 2. 按照特定关键词拆分,取前面的部分作为操作名 + split_keywords = ['完成', 'elapsed', '耗时', '成功', '失败', '开始', '结束', '...', '---', '步骤'] + for kw in split_keywords: + if kw in clean_msg: + clean_msg = clean_msg.split(kw)[0] + + # 3. 统一特殊长内容 + if '完整识别结果' in clean_msg: + clean_msg = '完整识别结果' + if '等待开奖中' in clean_msg: + clean_msg = '等待开奖中' + if '未检测到福袋图标' in clean_msg: + clean_msg = '未检测到福袋图标' + if '检测到福袋图标' in clean_msg: + clean_msg = '检测到福袋图标' + + # 4. 移除括号 + clean_msg = re.sub(r'(.*?)', '', clean_msg) + clean_msg = re.sub(r'\(.*?\)', '', clean_msg) + + clean_msg = clean_msg.strip(' ,,|:-_') + + if not clean_msg: + continue + + category = f"[{tag}] {clean_msg}" + data[category].append(value) + continue + + # 特殊格式处理 (截图) + m = re.search(r'\[截图\].*?总耗时\(ms\)=([\d.]+)', line) + if m: + data["[截图] 获取屏幕截图"].append(float(m.group(1))) + continue + + return data + +def analyze(data): + results = [] + for category, values in data.items(): + if not values: + continue + arr = np.array(values) + results.append({ + 'category': category, + 'count': len(arr), + 'avg': np.mean(arr), + 'p50': np.percentile(arr, 50), + 'p80': np.percentile(arr, 80), + 'p95': np.percentile(arr, 95), + 'min': np.min(arr), + 'max': np.max(arr), + 'total': np.sum(arr) + }) + + # Sort by average duration descending + results.sort(key=lambda x: x['avg'], reverse=True) + return results + +def print_table(results): + header = f"{'Operation':<55} | {'Count':>6} | {'Avg':>8} | {'P50':>8} | {'P80':>8} | {'P95':>8} | {'Max':>8}" + sep = "-" * len(header) + print(sep) + print(header) + print(sep) + for r in results: + print(f"{r['category'][:55]:<55} | {r['count']:>6} | {r['avg']:>8.1f} | {r['p50']:>8.1f} | {r['p80']:>8.1f} | {r['p95']:>8.1f} | {r['max']:>8.1f}") + print(sep) + +if __name__ == "__main__": + logs_dir = "logs" + latest_unified = None + all_logs = [] + + # Get all logs from last 3 days or so + if os.path.exists(logs_dir): + files = os.listdir(logs_dir) + # Sort files to find the latest + files.sort(reverse=True) + + # Add actions.log + if "actions.log" in files: + all_logs.append(os.path.join(logs_dir, "actions.log")) + + # Add a few recent unified logs + unified_count = 0 + for f in files: + if f.startswith("unified_") and f.endswith(".log"): + all_logs.append(os.path.join(logs_dir, f)) + unified_count += 1 + if unified_count >= 3: + break + + if not all_logs: + print("No logs found.") + sys.exit(1) + + data = parse_logs(all_logs) + results = analyze(data) + print_table(results)