feat: 优化整体项目

This commit is contained in:
2025-12-05 18:06:13 +08:00
parent b4df26f61d
commit 7d3ef70093
13 changed files with 2661 additions and 305 deletions

View File

@@ -46,7 +46,7 @@ class PerformanceMonitor(PluginBase):
if sender_wxid not in admins:
return
if content in ["/性能", "/stats", "/状态"]:
if content in ["/性能", "/stats", "/状态", "/性能报告"]:
stats_msg = await self._get_performance_stats(bot)
await bot.send_text(from_wxid, stats_msg)
return False # 阻止其他插件处理
@@ -66,11 +66,21 @@ class PerformanceMonitor(PluginBase):
async def _get_performance_stats(self, bot) -> str:
"""获取性能统计信息"""
try:
# 尝试使用新的性能监控器
try:
from utils.bot_utils import get_performance_monitor
monitor = get_performance_monitor()
if monitor and monitor.message_received > 0:
return self._format_new_stats(monitor)
except ImportError:
pass
# 降级到旧的统计方式
stats = await self._get_performance_data()
# 格式化统计信息
uptime_hours = (time.time() - self.start_time) / 3600
msg = f"""📊 系统性能统计
🕐 运行时间: {uptime_hours:.1f} 小时
@@ -94,11 +104,57 @@ class PerformanceMonitor(PluginBase):
• 过滤模式: {stats['ignore_mode']}"""
return msg
except Exception as e:
logger.error(f"获取性能统计失败: {e}")
return f"❌ 获取性能统计失败: {str(e)}"
def _format_new_stats(self, monitor) -> str:
"""格式化新性能监控器的统计信息"""
stats = monitor.get_stats()
# 基础信息
msg = f"""📊 系统性能报告
🕐 运行时间: {stats['uptime_formatted']}
📨 消息统计:
• 收到: {stats['messages']['received']}
• 处理: {stats['messages']['processed']}
• 失败: {stats['messages']['failed']}
• 丢弃: {stats['messages']['dropped']}
• 成功率: {stats['messages']['success_rate']}
• 处理速率: {stats['messages']['processing_rate']}
⚡ 处理性能:
• 平均耗时: {stats['processing_time']['average_ms']}ms
• 最大耗时: {stats['processing_time']['max_ms']}ms
• 最小耗时: {stats['processing_time']['min_ms']}ms
📦 队列状态:
• 当前大小: {stats['queue']['current_size']}
• 历史最大: {stats['queue']['max_size']}"""
# 熔断器状态
cb = stats.get('circuit_breaker', {})
if cb:
state_icon = {'closed': '🟢', 'open': '🔴', 'half_open': '🟡'}.get(cb.get('state', ''), '')
msg += f"""
🔌 熔断器:
• 状态: {state_icon} {cb.get('state', 'N/A')}
• 失败计数: {cb.get('failure_count', 0)}
• 恢复时间: {cb.get('current_recovery_time', 0):.0f}s"""
# 插件耗时排行
plugins = stats.get('plugins', [])
if plugins:
msg += "\n\n🔧 插件耗时排行:"
for i, p in enumerate(plugins[:5], 1):
msg += f"\n {i}. {p['name']}: {p['avg_time_ms']}ms ({p['calls']}次)"
return msg
async def _get_performance_data(self) -> dict:
"""获取性能数据"""
# 系统资源简化版本不依赖psutil