Files
douyin_guaji-main/tools/analyze_optimization_effect.py
T
Your Name 478ae69052 chore: Clean up blank lines and enhance code organization
- Removed excessive blank lines from multiple Python scripts and configuration files to improve readability and maintainability.
- Ensured consistent formatting across the project, contributing to a more organized codebase.
- Added new files for testing and analysis, including `test_screenshot_error_handling.py` and `test_screenshot_reuse_optimization.py`, to improve testing coverage and functionality.
2026-01-01 10:04:32 +08:00

149 lines
5.2 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 python
# -*- coding: utf-8 -*-
"""分析优化效果"""
import sys
import os
import re
from datetime import datetime
if sys.platform == 'win32':
try:
# 尝试使用reconfigure方法(Python 3.7+)
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
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
def main():
log_file = "logs/unified_2025-12-31.log"
if not os.path.exists(log_file):
print("日志文件不存在")
return
with open(log_file, 'r', encoding='utf-8', errors='replace') as f:
lines = f.readlines()
print("=" * 60)
print("优化效果分析")
print("=" * 60)
print()
# 1. 检查服务启动
service_started = False
for line in lines:
if '截图服务' in line and '已启动' in line:
service_started = True
print("✅ 服务已启动")
# 提取时间
time_match = re.search(r'(\d{2}:\d{2}:\d{2})', line)
if time_match:
print(f" 启动时间: {time_match.group(1)}")
break
if not service_started:
print("❌ 服务未启动")
return
# 2. 分析服务启动后的截图性能
service_start_time = None
screenshot_times_after_service = []
for i, line in enumerate(lines):
if '截图服务' in line and '已启动' in line:
time_match = re.search(r'(\d{2}:\d{2}:\d{2})', line)
if time_match:
h, m, s = map(int, time_match.group(1).split(':'))
service_start_time = h * 3600 + m * 60 + s
print(f" 服务启动时间戳: {service_start_time}")
break
if service_start_time:
# 分析服务启动后的截图耗时
for line in lines:
if 'screenshot_elapsed_seconds' in line:
time_match = re.search(r'(\d{2}:\d{2}:\d{2})', line)
if time_match:
h, m, s = map(int, time_match.group(1).split(':'))
log_time = h * 3600 + m * 60 + s
if log_time >= service_start_time:
elapsed_match = re.search(r'screenshot_elapsed_seconds=([\d.]+)', line)
if elapsed_match:
elapsed = float(elapsed_match.group(1))
screenshot_times_after_service.append(elapsed)
if screenshot_times_after_service:
print()
print("服务启动后的截图性能:")
print(f" 次数: {len(screenshot_times_after_service)}")
print(f" 平均耗时: {sum(screenshot_times_after_service) / len(screenshot_times_after_service):.2f}秒")
print(f" 最短耗时: {min(screenshot_times_after_service):.2f}秒")
print(f" 最长耗时: {max(screenshot_times_after_service):.2f}秒")
# 分析快速响应(可能是缓存命中)
fast_responses = [t for t in screenshot_times_after_service if t < 0.5]
if fast_responses:
print(f"\n ✅ 发现 {len(fast_responses)} 次快速响应(< 0.5秒)")
print(f" 说明缓存可能生效")
else:
print(f"\n ⚠️ 未发现快速响应(< 0.5秒)")
print(f" 可能原因:缓存未命中或服务未正常工作")
# 3. 检查代码集成
print()
print("=" * 60)
print("代码集成检查")
print("=" * 60)
# 检查 Underlying_Operations.py
if os.path.exists("Underlying_Operations.py"):
with open("Underlying_Operations.py", 'r', encoding='utf-8') as f:
content = f.read()
if 'use_service' in content and 'ScreenshotService' in content:
print("✅ Underlying_Operations.py 已支持服务模式")
else:
print("❌ Underlying_Operations.py 未支持服务模式")
print()
print("=" * 60)
print("结论")
print("=" * 60)
if service_started:
if screenshot_times_after_service:
avg = sum(screenshot_times_after_service) / len(screenshot_times_after_service)
if avg < 1.0:
print("✅ 优化生效!平均耗时 < 1秒")
elif avg < 2.0:
print("⚠️ 优化部分生效,平均耗时 1-2秒")
print(" 可能原因:缓存未完全生效")
else:
print("❌ 优化未生效,平均耗时 >= 2秒")
print(" 可能原因:")
print(" 1. 缓存未命中")
print(" 2. 服务未正常工作")
print(" 3. 某些调用未使用服务模式")
else:
print("⚠️ 服务已启动,但未找到截图耗时数据")
else:
print("❌ 服务未启动,需要重新运行程序")
if __name__ == '__main__':
main()