Files
douyin_guaji-main/tools/analyze_step_times.py
T
Your Name b3011dd9ce feat: Implement high-value room revisit feature and enhance fudai tracking
- Added tracking for previously seen fudai in Runner and ProcRunner classes to avoid duplicate recognition.
- Extracted room title during fudai recognition for better tracking.
- Updated high_value_rooms.json with new room entries and adjusted total count.
- Introduced new GUI elements for configuring icon coordinates in webui.
- Created comprehensive documentation for the high-value room revisit functionality.
- Added tests for delay settings and icon detection logic to ensure functionality.
- Implemented a tool for analyzing step times in logs to identify performance bottlenecks.
2026-01-08 14:27:38 +08:00

206 lines
7.0 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""分析日志中各步骤的耗时排序"""
import sys
import os
import re
from collections import defaultdict
from datetime import datetime
if sys.platform == 'win32':
try:
reconfigure_func = getattr(sys.stdout, 'reconfigure', None)
if reconfigure_func:
reconfigure_func(encoding='utf-8')
except Exception:
try:
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
except Exception:
pass
def analyze_elapsed_times(log_path):
"""分析日志中的耗时数据"""
if not os.path.exists(log_path):
print(f"日志文件不存在: {log_path}")
return
print("=" * 80)
print("步骤耗时分析")
print("=" * 80)
print()
# 读取日志文件
try:
with open(log_path, 'r', encoding='utf-8', errors='replace') as f:
lines = f.readlines()
except Exception as e:
print(f"读取日志文件失败: {e}")
return
# 收集耗时数据
step_times = defaultdict(list)
total_elapsed_times = []
for line in lines:
# 查找包含 elapsed_seconds 的行
if 'elapsed_seconds=' in line:
# 提取耗时
elapsed_match = re.search(r'elapsed_seconds=([\d.]+)', line)
if elapsed_match:
elapsed = float(elapsed_match.group(1))
# 提取步骤名称(从日志标签中提取)
step_name = "未知步骤"
if '[截图]' in line:
step_name = "截图"
elif '[流程]' in line and 'run_once_basic' in line:
step_name = "run_once_basic总耗时"
elif '[预约]' in line and 'process_reservation' in line:
step_name = "预约处理"
elif '[参与]' in line:
step_name = "参与抽奖"
elif '[弹窗处理]' in line:
step_name = "弹窗处理"
elif '[福袋详情]' in line:
step_name = "福袋详情读取"
elif '[阶段诊断]' in line:
step_name = "阶段诊断"
elif '[OCR]' in line:
step_name = "OCR识别"
elif '[滑动]' in line:
step_name = "滑动操作"
elif '[行为]' in line:
step_name = "行为操作"
elif '[统计]' in line:
step_name = "统计记录"
elif '[自救]' in line:
step_name = "自救处理"
elif '[超时]' in line:
step_name = "超时处理"
elif 'elapsed_seconds=' in line:
# 从消息中提取更具体的步骤
if '预约' in line:
step_name = "预约相关"
elif 'OCR' in line or '识别' in line:
step_name = "OCR识别"
elif '截图' in line:
step_name = "截图"
elif '滑动' in line:
step_name = "滑动操作"
elif '详情' in line:
step_name = "详情处理"
else:
# 从消息中提取
msg_match = re.search(r'\] (.+?):', line)
if msg_match:
step_name = msg_match.group(1).strip()
else:
# 跳过不重要的记录
continue
step_times[step_name].append(elapsed)
total_elapsed_times.append((step_name, elapsed))
# 分析结果
if not step_times:
print("未找到耗时数据")
return
print(f"总共分析了 {len(total_elapsed_times)} 条耗时记录")
print(f"涉及 {len(step_times)} 个不同步骤")
print()
# 按步骤分组统计
print("各步骤耗时统计(按平均耗时排序):")
print("-" * 80)
step_stats = []
for step, times in step_times.items():
if times:
avg_time = sum(times) / len(times)
max_time = max(times)
min_time = min(times)
count = len(times)
step_stats.append((step, avg_time, max_time, min_time, count))
# 按平均耗时降序排序
step_stats.sort(key=lambda x: x[1], reverse=True)
print(f"{'步骤名称':<20} {'平均耗时(秒)':<12} {'最大耗时':<10} {'最小耗时':<10} {'次数':<6}")
print("-" * 80)
for step, avg_time, max_time, min_time, count in step_stats:
print(f"{step:<20} {avg_time:<12.2f} {max_time:<10.2f} {min_time:<10.2f} {count:<6}")
print()
print("=" * 80)
print("耗时最长的前10个记录:")
print("-" * 80)
# 按耗时降序排序所有记录
total_elapsed_times.sort(key=lambda x: x[1], reverse=True)
print(f"{'步骤名称':<20} {'耗时(秒)':<10} {'日志片段':<50}")
print("-" * 80)
for step, elapsed in total_elapsed_times[:10]:
# 找到对应的日志行
log_snippet = ""
for line in lines:
if f'elapsed_seconds={elapsed}' in line and step in line:
# 提取有意义的部分
parts = line.split('] ')
if len(parts) > 1:
log_snippet = parts[1].split(' elapsed_seconds')[0][:50]
break
print(f"{step:<20} {elapsed:<10.2f} {log_snippet:<50}")
print()
print("=" * 80)
print("性能建议:")
print("-" * 80)
# 分析耗时最长的步骤
if step_stats:
slowest_step = step_stats[0]
if slowest_step[1] > 5.0:
print(f"⚠️ 最耗时的步骤 '{slowest_step[0]}' 平均耗时 {slowest_step[1]:.2f}秒,建议优化")
elif slowest_step[1] > 2.0:
print(f"⚠️ 最耗时的步骤 '{slowest_step[0]}' 平均耗时 {slowest_step[1]:.2f}秒,需要关注")
else:
print("✅ 各步骤耗时都在合理范围内")
# 检查是否有异常耗时的记录
abnormal_records = [t for t in total_elapsed_times if t[1] > 10.0]
if abnormal_records:
print(f"⚠️ 发现 {len(abnormal_records)} 条异常耗时记录(>10秒),可能存在性能问题")
def main():
# 默认日志文件路径
log_files = [
"logs/unified.log",
"logs/unified_2025-12-31.log",
f"logs/unified_{datetime.now().strftime('%Y-%m-%d')}.log"
]
log_path = None
for lf in log_files:
if os.path.exists(lf):
log_path = lf
break
if not log_path:
print("未找到日志文件,请指定日志文件路径")
print("使用方法: python analyze_step_times.py [日志文件路径]")
return
if len(sys.argv) > 1:
log_path = sys.argv[1]
print(f"分析日志文件: {log_path}")
analyze_elapsed_times(log_path)
if __name__ == '__main__':
main()