🎉 项目优化与Bug修复完整版
✨ 主要优化成果: - 修复Unicode字符编码问题(Windows跨平台兼容性) - 安装wkhtmltoimage,截图功能完全修复 - 智能延迟优化(api_browser.py) - 线程池资源泄漏修复(tasks.py) - HTML解析缓存机制 - 二分搜索算法优化(kdocs_uploader.py) - 自适应资源配置(browser_pool_worker.py) 🐛 Bug修复: - 解决截图失败问题 - 修复管理员密码设置 - 解决应用启动编码错误 📚 新增文档: - BUG_REPORT.md - 完整bug分析报告 - PERFORMANCE_ANALYSIS_REPORT.md - 性能优化分析 - LINUX_DEPLOYMENT_ANALYSIS.md - Linux部署指南 - SCREENSHOT_FIX_SUCCESS.md - 截图功能修复记录 - INSTALL_WKHTMLTOIMAGE.md - 安装指南 - OPTIMIZATION_FIXES_SUMMARY.md - 优化总结 🚀 功能验证: - Flask应用正常运行(51233端口) - 数据库、截图线程池、API预热正常 - 管理员登录:admin/admin123 - 健康检查API:http://127.0.0.1:51233/health 💡 技术改进: - 智能延迟算法(自适应调整) - LRU缓存策略 - 线程池资源管理优化 - 二分搜索算法(O(log n) vs O(n)) - 自适应资源管理 🎯 项目现在稳定运行,可部署到Linux环境
This commit is contained in:
183
test_screenshot_functionality.py
Normal file
183
test_screenshot_functionality.py
Normal file
@@ -0,0 +1,183 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
测试截图功能的脚本
|
||||
验证wkhtmltoimage安装和截图API功能
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import requests
|
||||
import time
|
||||
|
||||
|
||||
def test_wkhtmltoimage():
|
||||
"""测试wkhtmltoimage命令行工具"""
|
||||
print("--- 测试wkhtmltoimage命令行工具 ---")
|
||||
|
||||
try:
|
||||
import subprocess
|
||||
|
||||
result = subprocess.run(["wkhtmltoimage", "--version"], capture_output=True, text=True, timeout=5)
|
||||
if result.returncode == 0:
|
||||
print(f"[OK] wkhtmltoimage已安装: {result.stdout.strip()}")
|
||||
return True
|
||||
else:
|
||||
print("[FAIL] wkhtmltoimage命令执行失败")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"[FAIL] 测试wkhtmltoimage失败: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def test_direct_screenshot():
|
||||
"""测试直接截图功能"""
|
||||
print("\n--- 测试直接截图功能 ---")
|
||||
|
||||
try:
|
||||
import subprocess
|
||||
|
||||
# 创建截图目录
|
||||
os.makedirs("screenshots", exist_ok=True)
|
||||
|
||||
# 截图本地应用
|
||||
cmd = [
|
||||
"wkhtmltoimage",
|
||||
"--width",
|
||||
"1920",
|
||||
"--height",
|
||||
"1080",
|
||||
"--quality",
|
||||
"95",
|
||||
"--js-delay",
|
||||
"3000",
|
||||
"http://127.0.0.1:51233",
|
||||
"screenshots/test_direct.png",
|
||||
]
|
||||
|
||||
result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
|
||||
|
||||
if result.returncode == 0:
|
||||
if os.path.exists("screenshots/test_direct.png"):
|
||||
file_size = os.path.getsize("screenshots/test_direct.png")
|
||||
print(f"[OK] 直接截图成功: screenshots/test_direct.png ({file_size} bytes)")
|
||||
return True
|
||||
else:
|
||||
print("[FAIL] 截图文件未生成")
|
||||
return False
|
||||
else:
|
||||
print(f"[FAIL] 直接截图失败: {result.stderr}")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"[FAIL] 直接截图测试失败: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def test_api_screenshot():
|
||||
"""测试API截图功能"""
|
||||
print("\n--- 测试API截图功能 ---")
|
||||
|
||||
# 检查应用是否运行
|
||||
try:
|
||||
response = requests.get("http://127.0.0.1:51233/health", timeout=5)
|
||||
if response.status_code == 200:
|
||||
print("[OK] 应用正在运行")
|
||||
else:
|
||||
print(f"[FAIL] 应用响应异常: {response.status_code}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"[FAIL] 应用连接失败: {e}")
|
||||
return False
|
||||
|
||||
# 尝试访问截图相关的API
|
||||
api_endpoints = ["/api/screenshots", "/yuyx/api/browser_pool/stats", "/yuyx/api/screenshots"]
|
||||
|
||||
for endpoint in api_endpoints:
|
||||
try:
|
||||
response = requests.get(f"http://127.0.0.1:51233{endpoint}", timeout=5)
|
||||
print(f"API {endpoint}: {response.status_code}")
|
||||
|
||||
if response.status_code == 401:
|
||||
print(f" [WARN] 需要认证 - 这是正常的")
|
||||
elif response.status_code == 404:
|
||||
print(f" [WARN] 端点不存在 - 需要检查路由配置")
|
||||
elif response.status_code == 200:
|
||||
print(f" [OK] API正常工作")
|
||||
|
||||
except Exception as e:
|
||||
print(f" [FAIL] API调用失败: {e}")
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def check_logs():
|
||||
"""检查应用日志中的截图相关信息"""
|
||||
print("\n--- 检查应用日志 ---")
|
||||
|
||||
log_file = "app_new.log"
|
||||
if os.path.exists(log_file):
|
||||
print(f"[OK] 发现应用日志: {log_file}")
|
||||
|
||||
try:
|
||||
with open(log_file, "r", encoding="utf-8", errors="ignore") as f:
|
||||
lines = f.readlines()
|
||||
|
||||
# 查找截图相关的日志
|
||||
screenshot_lines = []
|
||||
for i, line in enumerate(lines[-20:]): # 最后20行
|
||||
if any(keyword in line.lower() for keyword in ["截图", "screenshot", "wkhtmltoimage"]):
|
||||
screenshot_lines.append(f"第{len(lines) - 20 + i + 1}行: {line.strip()}")
|
||||
|
||||
if screenshot_lines:
|
||||
print("发现截图相关日志:")
|
||||
for line in screenshot_lines:
|
||||
print(f" {line}")
|
||||
else:
|
||||
print("未发现截图相关日志")
|
||||
|
||||
except Exception as e:
|
||||
print(f"读取日志失败: {e}")
|
||||
else:
|
||||
print(f"[FAIL] 未找到应用日志: {log_file}")
|
||||
|
||||
|
||||
def main():
|
||||
print("[TEST] 截图功能测试工具")
|
||||
print("=" * 50)
|
||||
|
||||
# 测试wkhtmltoimage
|
||||
wkhtmltoimage_ok = test_wkhtmltoimage()
|
||||
|
||||
# 测试直接截图
|
||||
if wkhtmltoimage_ok:
|
||||
direct_ok = test_direct_screenshot()
|
||||
else:
|
||||
direct_ok = False
|
||||
|
||||
# 测试API
|
||||
api_ok = test_api_screenshot()
|
||||
|
||||
# 检查日志
|
||||
check_logs()
|
||||
|
||||
# 总结
|
||||
print("\n" + "=" * 50)
|
||||
print("[STATS] 测试结果总结:")
|
||||
print(f" wkhtmltoimage: {'[OK]' if wkhtmltoimage_ok else '[FAIL]'}")
|
||||
print(f" 直接截图: {'[OK]' if direct_ok else '[FAIL]'}")
|
||||
print(f" API连接: {'[OK]' if api_ok else '[FAIL]'}")
|
||||
|
||||
if wkhtmltoimage_ok and direct_ok:
|
||||
print("\n[SUCCESS] 截图功能基础测试通过!")
|
||||
print("现在可以测试Web界面的截图功能了。")
|
||||
print("\n下一步:")
|
||||
print("1. 访问 http://127.0.0.1:51233/yuyx 登录管理员后台")
|
||||
print("2. 使用admin/admin123登录")
|
||||
print("3. 找到截图功能进行测试")
|
||||
else:
|
||||
print("\n[WARN] 截图功能存在问题,需要进一步调试")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user