Add shopping red packet detection tests and sample image

- Implemented a test script for shopping red packet detection using pixel analysis and OCR.
- Created a sample image to simulate a shopping red packet for testing purposes.
- Added functions to test pixel detection, coordinate definitions, and OCR keyword matching.
- Included detailed print statements for test results and summaries.
This commit is contained in:
Your Name committed 2026-01-08 20:37:49 +08:00
1 parent b3011dd9ce
commit fb0c07c91a
30 files changed
+1269 -319

No files matched your search

+54 -38
View File
@@ -8,18 +8,22 @@ import re
from collections import defaultdict
from datetime import datetime
if sys.platform == 'win32':
if sys.platform == "win32":
try:
reconfigure_func = getattr(sys.stdout, 'reconfigure', None)
reconfigure_func = getattr(sys.stdout, "reconfigure", None)
if reconfigure_func:
reconfigure_func(encoding='utf-8')
reconfigure_func(encoding="utf-8")
except Exception:
try:
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
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):
@@ -33,7 +37,7 @@ def analyze_elapsed_times(log_path):
# 读取日志文件
try:
with open(log_path, 'r', encoding='utf-8', errors='replace') as f:
with open(log_path, "r", encoding="utf-8", errors="replace") as f:
lines = f.readlines()
except Exception as e:
print(f"读取日志文件失败: {e}")
@@ -45,55 +49,55 @@ def analyze_elapsed_times(log_path):
for line in lines:
# 查找包含 elapsed_seconds 的行
if 'elapsed_seconds=' in line:
if "elapsed_seconds=" in line:
# 提取耗时
elapsed_match = re.search(r'elapsed_seconds=([\d.]+)', line)
elapsed_match = re.search(r"elapsed_seconds=([\d.]+)", line)
if elapsed_match:
elapsed = float(elapsed_match.group(1))
# 提取步骤名称(从日志标签中提取)
step_name = "未知步骤"
if '[截图]' in line:
if "[截图]" in line:
step_name = "截图"
elif '[流程]' in line and 'run_once_basic' in line:
elif "[流程]" in line and "run_once_basic" in line:
step_name = "run_once_basic总耗时"
elif '[预约]' in line and 'process_reservation' in line:
elif "[预约]" in line and "process_reservation" in line:
step_name = "预约处理"
elif '[参与]' in line:
elif "[参与]" in line:
step_name = "参与抽奖"
elif '[弹窗处理]' in line:
elif "[弹窗处理]" in line:
step_name = "弹窗处理"
elif '[福袋详情]' in line:
elif "[福袋详情]" in line:
step_name = "福袋详情读取"
elif '[阶段诊断]' in line:
elif "[阶段诊断]" in line:
step_name = "阶段诊断"
elif '[OCR]' in line:
elif "[OCR]" in line:
step_name = "OCR识别"
elif '[滑动]' in line:
elif "[滑动]" in line:
step_name = "滑动操作"
elif '[行为]' in line:
elif "[行为]" in line:
step_name = "行为操作"
elif '[统计]' in line:
elif "[统计]" in line:
step_name = "统计记录"
elif '[自救]' in line:
elif "[自救]" in line:
step_name = "自救处理"
elif '[超时]' in line:
elif "[超时]" in line:
step_name = "超时处理"
elif 'elapsed_seconds=' in line:
elif "elapsed_seconds=" in line:
# 从消息中提取更具体的步骤
if '预约' in line:
if "预约" in line:
step_name = "预约相关"
elif 'OCR' in line or '识别' in line:
elif "OCR" in line or "识别" in line:
step_name = "OCR识别"
elif '截图' in line:
elif "截图" in line:
step_name = "截图"
elif '滑动' in line:
elif "滑动" in line:
step_name = "滑动操作"
elif '详情' in line:
elif "详情" in line:
step_name = "详情处理"
else:
# 从消息中提取
msg_match = re.search(r'\] (.+?):', line)
msg_match = re.search(r"\] (.+?):", line)
if msg_match:
step_name = msg_match.group(1).strip()
else:
@@ -128,11 +132,15 @@ def analyze_elapsed_times(log_path):
# 按平均耗时降序排序
step_stats.sort(key=lambda x: x[1], reverse=True)
print(f"{'步骤名称':<20} {'平均耗时(秒)':<12} {'最大耗时':<10} {'最小耗时':<10} {'次数':<6}")
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(
f"{step:<20} {avg_time:<12.2f} {max_time:<10.2f} {min_time:<10.2f} {count:<6}"
)
print()
print("=" * 80)
@@ -149,11 +157,11 @@ def analyze_elapsed_times(log_path):
# 找到对应的日志行
log_snippet = ""
for line in lines:
if f'elapsed_seconds={elapsed}' in line and step in line:
if f"elapsed_seconds={elapsed}" in line and step in line:
# 提取有意义的部分
parts = line.split('] ')
parts = line.split("] ")
if len(parts) > 1:
log_snippet = parts[1].split(' elapsed_seconds')[0][:50]
log_snippet = parts[1].split(" elapsed_seconds")[0][:50]
break
print(f"{step:<20} {elapsed:<10.2f} {log_snippet:<50}")
@@ -166,23 +174,30 @@ def analyze_elapsed_times(log_path):
if step_stats:
slowest_step = step_stats[0]
if slowest_step[1] > 5.0:
print(f"⚠️ 最耗时的步骤 '{slowest_step[0]}' 平均耗时 {slowest_step[1]:.2f}秒,建议优化")
print(
f"⚠️ 最耗时的步骤 '{slowest_step[0]}' 平均耗时 {slowest_step[1]:.2f}秒,建议优化"
)
elif slowest_step[1] > 2.0:
print(f"⚠️ 最耗时的步骤 '{slowest_step[0]}' 平均耗时 {slowest_step[1]:.2f}秒,需要关注")
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秒),可能存在性能问题")
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"
f"logs/unified_{datetime.now().strftime('%Y-%m-%d')}.log",
]
log_path = None
@@ -202,5 +217,6 @@ def main():
print(f"分析日志文件: {log_path}")
analyze_elapsed_times(log_path)
if __name__ == '__main__':
main()
if __name__ == "__main__":
main()