🤖 自动格式化代码 [skip ci]
This commit is contained in:
@@ -35,67 +35,42 @@ def test_performance_logger():
|
|||||||
"cycle_id": 1,
|
"cycle_id": 1,
|
||||||
"action_type": "reply",
|
"action_type": "reply",
|
||||||
"total_time": 2.5,
|
"total_time": 2.5,
|
||||||
"step_times": {
|
"step_times": {"观察": 0.1, "并行调整动作、处理": 1.2, "规划器": 0.8, "执行动作": 0.4},
|
||||||
"观察": 0.1,
|
|
||||||
"并行调整动作、处理": 1.2,
|
|
||||||
"规划器": 0.8,
|
|
||||||
"执行动作": 0.4
|
|
||||||
},
|
|
||||||
"reasoning": "用户询问天气,需要回复",
|
"reasoning": "用户询问天气,需要回复",
|
||||||
"success": True
|
"success": True,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cycle_id": 2,
|
"cycle_id": 2,
|
||||||
"action_type": "no_reply",
|
"action_type": "no_reply",
|
||||||
"total_time": 1.8,
|
"total_time": 1.8,
|
||||||
"step_times": {
|
"step_times": {"观察": 0.08, "并行调整动作、处理": 0.9, "规划器": 0.6, "执行动作": 0.22},
|
||||||
"观察": 0.08,
|
|
||||||
"并行调整动作、处理": 0.9,
|
|
||||||
"规划器": 0.6,
|
|
||||||
"执行动作": 0.22
|
|
||||||
},
|
|
||||||
"reasoning": "无需回复的日常对话",
|
"reasoning": "无需回复的日常对话",
|
||||||
"success": True
|
"success": True,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cycle_id": 3,
|
"cycle_id": 3,
|
||||||
"action_type": "reply",
|
"action_type": "reply",
|
||||||
"total_time": 3.2,
|
"total_time": 3.2,
|
||||||
"step_times": {
|
"step_times": {"观察": 0.12, "并行调整动作、处理": 1.5, "规划器": 1.1, "执行动作": 0.48},
|
||||||
"观察": 0.12,
|
|
||||||
"并行调整动作、处理": 1.5,
|
|
||||||
"规划器": 1.1,
|
|
||||||
"执行动作": 0.48
|
|
||||||
},
|
|
||||||
"reasoning": "用户提出复杂问题,需要详细回复",
|
"reasoning": "用户提出复杂问题,需要详细回复",
|
||||||
"success": True
|
"success": True,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cycle_id": 4,
|
"cycle_id": 4,
|
||||||
"action_type": "no_reply",
|
"action_type": "no_reply",
|
||||||
"total_time": 1.5,
|
"total_time": 1.5,
|
||||||
"step_times": {
|
"step_times": {"观察": 0.07, "并行调整动作、处理": 0.8, "规划器": 0.5, "执行动作": 0.13},
|
||||||
"观察": 0.07,
|
|
||||||
"并行调整动作、处理": 0.8,
|
|
||||||
"规划器": 0.5,
|
|
||||||
"执行动作": 0.13
|
|
||||||
},
|
|
||||||
"reasoning": "群聊中的无关对话",
|
"reasoning": "群聊中的无关对话",
|
||||||
"success": True
|
"success": True,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cycle_id": 5,
|
"cycle_id": 5,
|
||||||
"action_type": "error",
|
"action_type": "error",
|
||||||
"total_time": 0.5,
|
"total_time": 0.5,
|
||||||
"step_times": {
|
"step_times": {"观察": 0.05, "并行调整动作、处理": 0.2, "规划器": 0.15, "执行动作": 0.1},
|
||||||
"观察": 0.05,
|
|
||||||
"并行调整动作、处理": 0.2,
|
|
||||||
"规划器": 0.15,
|
|
||||||
"执行动作": 0.1
|
|
||||||
},
|
|
||||||
"reasoning": "处理过程中出现错误",
|
"reasoning": "处理过程中出现错误",
|
||||||
"success": False
|
"success": False,
|
||||||
}
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
# 记录测试数据
|
# 记录测试数据
|
||||||
@@ -110,7 +85,7 @@ def test_performance_logger():
|
|||||||
|
|
||||||
# 完成会话
|
# 完成会话
|
||||||
logger.finalize_session()
|
logger.finalize_session()
|
||||||
print(f"\n=== 会话已完成 ===")
|
print("\n=== 会话已完成 ===")
|
||||||
print(f"日志文件: {logger.session_file}")
|
print(f"日志文件: {logger.session_file}")
|
||||||
print(f"统计文件: {logger.stats_file}")
|
print(f"统计文件: {logger.stats_file}")
|
||||||
|
|
||||||
@@ -122,10 +97,10 @@ def test_performance_logger():
|
|||||||
print(f"统计文件大小: {logger.stats_file.stat().st_size} 字节")
|
print(f"统计文件大小: {logger.stats_file.stat().st_size} 字节")
|
||||||
|
|
||||||
# 读取并显示统计数据
|
# 读取并显示统计数据
|
||||||
with open(logger.stats_file, 'r', encoding='utf-8') as f:
|
with open(logger.stats_file, "r", encoding="utf-8") as f:
|
||||||
stats_data = json.load(f)
|
stats_data = json.load(f)
|
||||||
|
|
||||||
print(f"\n=== 最终统计数据 ===")
|
print("\n=== 最终统计数据 ===")
|
||||||
if test_chat_id in stats_data:
|
if test_chat_id in stats_data:
|
||||||
chat_stats = stats_data[test_chat_id]
|
chat_stats = stats_data[test_chat_id]
|
||||||
print(f"Chat ID: {test_chat_id}")
|
print(f"Chat ID: {test_chat_id}")
|
||||||
@@ -133,13 +108,15 @@ def test_performance_logger():
|
|||||||
print(f"总记录数: {chat_stats['overall']['total_records']}")
|
print(f"总记录数: {chat_stats['overall']['total_records']}")
|
||||||
print(f"平均总时间: {chat_stats['overall']['avg_total_time']:.2f}秒")
|
print(f"平均总时间: {chat_stats['overall']['avg_total_time']:.2f}秒")
|
||||||
|
|
||||||
print(f"\n各步骤平均时间:")
|
print("\n各步骤平均时间:")
|
||||||
for step, avg_time in chat_stats['overall']['avg_step_times'].items():
|
for step, avg_time in chat_stats["overall"]["avg_step_times"].items():
|
||||||
print(f" {step}: {avg_time:.3f}秒")
|
print(f" {step}: {avg_time:.3f}秒")
|
||||||
|
|
||||||
print(f"\n按动作类型统计:")
|
print("\n按动作类型统计:")
|
||||||
for action, action_stats in chat_stats['by_action'].items():
|
for action, action_stats in chat_stats["by_action"].items():
|
||||||
print(f" {action}: {action_stats['count']}次 ({action_stats['percentage']:.1f}%), 平均{action_stats['avg_total_time']:.2f}秒")
|
print(
|
||||||
|
f" {action}: {action_stats['count']}次 ({action_stats['percentage']:.1f}%), 平均{action_stats['avg_total_time']:.2f}秒"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_version_manager():
|
def test_version_manager():
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import sys
|
|||||||
import json
|
import json
|
||||||
import argparse
|
import argparse
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from datetime import datetime
|
|
||||||
from typing import Dict, Any
|
from typing import Dict, Any
|
||||||
|
|
||||||
# 添加项目根目录到Python路径
|
# 添加项目根目录到Python路径
|
||||||
@@ -28,29 +27,29 @@ def display_chat_stats(chat_id: str, stats: Dict[str, Any]):
|
|||||||
print(f"版本: {stats.get('version', 'unknown')}")
|
print(f"版本: {stats.get('version', 'unknown')}")
|
||||||
print(f"最后更新: {stats['last_updated']}")
|
print(f"最后更新: {stats['last_updated']}")
|
||||||
|
|
||||||
overall = stats['overall']
|
overall = stats["overall"]
|
||||||
print(f"\n📊 总体统计:")
|
print("\n📊 总体统计:")
|
||||||
print(f" 总记录数: {overall['total_records']}")
|
print(f" 总记录数: {overall['total_records']}")
|
||||||
print(f" 平均总时间: {format_time(overall['avg_total_time'])}")
|
print(f" 平均总时间: {format_time(overall['avg_total_time'])}")
|
||||||
|
|
||||||
print(f"\n⏱️ 各步骤平均时间:")
|
print("\n⏱️ 各步骤平均时间:")
|
||||||
for step, avg_time in overall['avg_step_times'].items():
|
for step, avg_time in overall["avg_step_times"].items():
|
||||||
print(f" {step}: {format_time(avg_time)}")
|
print(f" {step}: {format_time(avg_time)}")
|
||||||
|
|
||||||
print(f"\n🎯 按动作类型统计:")
|
print("\n🎯 按动作类型统计:")
|
||||||
by_action = stats['by_action']
|
by_action = stats["by_action"]
|
||||||
|
|
||||||
# 按比例排序
|
# 按比例排序
|
||||||
sorted_actions = sorted(by_action.items(), key=lambda x: x[1]['percentage'], reverse=True)
|
sorted_actions = sorted(by_action.items(), key=lambda x: x[1]["percentage"], reverse=True)
|
||||||
|
|
||||||
for action, action_stats in sorted_actions:
|
for action, action_stats in sorted_actions:
|
||||||
print(f" 📌 {action}:")
|
print(f" 📌 {action}:")
|
||||||
print(f" 次数: {action_stats['count']} ({action_stats['percentage']:.1f}%)")
|
print(f" 次数: {action_stats['count']} ({action_stats['percentage']:.1f}%)")
|
||||||
print(f" 平均总时间: {format_time(action_stats['avg_total_time'])}")
|
print(f" 平均总时间: {format_time(action_stats['avg_total_time'])}")
|
||||||
|
|
||||||
if action_stats['avg_step_times']:
|
if action_stats["avg_step_times"]:
|
||||||
print(f" 步骤时间:")
|
print(" 步骤时间:")
|
||||||
for step, step_time in action_stats['avg_step_times'].items():
|
for step, step_time in action_stats["avg_step_times"].items():
|
||||||
print(f" {step}: {format_time(step_time)}")
|
print(f" {step}: {format_time(step_time)}")
|
||||||
|
|
||||||
|
|
||||||
@@ -59,26 +58,28 @@ def display_comparison(stats_data: Dict[str, Dict[str, Any]]):
|
|||||||
if len(stats_data) < 2:
|
if len(stats_data) < 2:
|
||||||
return
|
return
|
||||||
|
|
||||||
print(f"\n=== 多聊天对比 ===")
|
print("\n=== 多聊天对比 ===")
|
||||||
|
|
||||||
# 创建对比表格
|
# 创建对比表格
|
||||||
chat_ids = list(stats_data.keys())
|
chat_ids = list(stats_data.keys())
|
||||||
|
|
||||||
print(f"\n📊 总体对比:")
|
print("\n📊 总体对比:")
|
||||||
print(f"{'Chat ID':<20} {'版本':<12} {'记录数':<8} {'平均时间':<12} {'最常见动作':<15}")
|
print(f"{'Chat ID':<20} {'版本':<12} {'记录数':<8} {'平均时间':<12} {'最常见动作':<15}")
|
||||||
print("-" * 70)
|
print("-" * 70)
|
||||||
|
|
||||||
for chat_id in chat_ids:
|
for chat_id in chat_ids:
|
||||||
stats = stats_data[chat_id]
|
stats = stats_data[chat_id]
|
||||||
overall = stats['overall']
|
overall = stats["overall"]
|
||||||
|
|
||||||
# 找到最常见的动作
|
# 找到最常见的动作
|
||||||
most_common_action = max(stats['by_action'].items(), key=lambda x: x[1]['count'])
|
most_common_action = max(stats["by_action"].items(), key=lambda x: x[1]["count"])
|
||||||
most_common_name = most_common_action[0]
|
most_common_name = most_common_action[0]
|
||||||
most_common_pct = most_common_action[1]['percentage']
|
most_common_pct = most_common_action[1]["percentage"]
|
||||||
|
|
||||||
version = stats.get('version', 'unknown')
|
version = stats.get("version", "unknown")
|
||||||
print(f"{chat_id:<20} {version:<12} {overall['total_records']:<8} {format_time(overall['avg_total_time']):<12} {most_common_name}({most_common_pct:.0f}%)")
|
print(
|
||||||
|
f"{chat_id:<20} {version:<12} {overall['total_records']:<8} {format_time(overall['avg_total_time']):<12} {most_common_name}({most_common_pct:.0f}%)"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def view_session_logs(chat_id: str = None, latest: bool = False):
|
def view_session_logs(chat_id: str = None, latest: bool = False):
|
||||||
@@ -108,7 +109,7 @@ def view_session_logs(chat_id: str = None, latest: bool = False):
|
|||||||
print(f"\n=== 会话日志: {log_file.name} ===")
|
print(f"\n=== 会话日志: {log_file.name} ===")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(log_file, 'r', encoding='utf-8') as f:
|
with open(log_file, "r", encoding="utf-8") as f:
|
||||||
records = json.load(f)
|
records = json.load(f)
|
||||||
|
|
||||||
if not records:
|
if not records:
|
||||||
@@ -123,9 +124,9 @@ def view_session_logs(chat_id: str = None, latest: bool = False):
|
|||||||
total_time = 0
|
total_time = 0
|
||||||
|
|
||||||
for record in records:
|
for record in records:
|
||||||
action = record['action_type']
|
action = record["action_type"]
|
||||||
action_counts[action] = action_counts.get(action, 0) + 1
|
action_counts[action] = action_counts.get(action, 0) + 1
|
||||||
total_time += record['total_time']
|
total_time += record["total_time"]
|
||||||
|
|
||||||
print(f" 总耗时: {format_time(total_time)}")
|
print(f" 总耗时: {format_time(total_time)}")
|
||||||
print(f" 平均耗时: {format_time(total_time / len(records))}")
|
print(f" 平均耗时: {format_time(total_time / len(records))}")
|
||||||
@@ -155,7 +156,7 @@ def main():
|
|||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(stats_file, 'r', encoding='utf-8') as f:
|
with open(stats_file, "r", encoding="utf-8") as f:
|
||||||
stats_data = json.load(f)
|
stats_data = json.load(f)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"❌ 读取统计数据失败: {e}")
|
print(f"❌ 读取统计数据失败: {e}")
|
||||||
|
|||||||
@@ -410,14 +410,14 @@ class HeartFChatting:
|
|||||||
|
|
||||||
# 记录性能数据
|
# 记录性能数据
|
||||||
try:
|
try:
|
||||||
action_result = self._current_cycle_detail.loop_plan_info.get('action_result', {})
|
action_result = self._current_cycle_detail.loop_plan_info.get("action_result", {})
|
||||||
cycle_performance_data = {
|
cycle_performance_data = {
|
||||||
"cycle_id": self._current_cycle_detail.cycle_id,
|
"cycle_id": self._current_cycle_detail.cycle_id,
|
||||||
"action_type": action_result.get('action_type', 'unknown'),
|
"action_type": action_result.get("action_type", "unknown"),
|
||||||
"total_time": self._current_cycle_detail.end_time - self._current_cycle_detail.start_time,
|
"total_time": self._current_cycle_detail.end_time - self._current_cycle_detail.start_time,
|
||||||
"step_times": cycle_timers.copy(),
|
"step_times": cycle_timers.copy(),
|
||||||
"reasoning": action_result.get('reasoning', ''),
|
"reasoning": action_result.get("reasoning", ""),
|
||||||
"success": self._current_cycle_detail.loop_action_info.get('action_taken', False),
|
"success": self._current_cycle_detail.loop_action_info.get("action_taken", False),
|
||||||
}
|
}
|
||||||
self.performance_logger.record_cycle(cycle_performance_data)
|
self.performance_logger.record_cycle(cycle_performance_data)
|
||||||
except Exception as perf_e:
|
except Exception as perf_e:
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
import os
|
|
||||||
import time
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Dict, List, Any, Optional
|
from typing import Dict, List, Any
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from src.common.logger import get_logger
|
from src.common.logger import get_logger
|
||||||
|
|
||||||
@@ -28,7 +26,9 @@ class HFCPerformanceLogger:
|
|||||||
|
|
||||||
# 当前会话的日志文件,包含版本号
|
# 当前会话的日志文件,包含版本号
|
||||||
version_suffix = self.version.replace(".", "_")
|
version_suffix = self.version.replace(".", "_")
|
||||||
self.session_file = self.log_dir / f"{chat_id}_{version_suffix}_{self.session_start_time.strftime('%Y%m%d_%H%M%S')}.json"
|
self.session_file = (
|
||||||
|
self.log_dir / f"{chat_id}_{version_suffix}_{self.session_start_time.strftime('%Y%m%d_%H%M%S')}.json"
|
||||||
|
)
|
||||||
self.current_session_data = []
|
self.current_session_data = []
|
||||||
|
|
||||||
# 统计数据文件
|
# 统计数据文件
|
||||||
@@ -50,7 +50,7 @@ class HFCPerformanceLogger:
|
|||||||
"total_time": cycle_data.get("total_time", 0),
|
"total_time": cycle_data.get("total_time", 0),
|
||||||
"step_times": cycle_data.get("step_times", {}),
|
"step_times": cycle_data.get("step_times", {}),
|
||||||
"reasoning": cycle_data.get("reasoning", ""),
|
"reasoning": cycle_data.get("reasoning", ""),
|
||||||
"success": cycle_data.get("success", False)
|
"success": cycle_data.get("success", False),
|
||||||
}
|
}
|
||||||
|
|
||||||
# 添加到当前会话数据
|
# 添加到当前会话数据
|
||||||
@@ -59,7 +59,9 @@ class HFCPerformanceLogger:
|
|||||||
# 立即写入文件(防止数据丢失)
|
# 立即写入文件(防止数据丢失)
|
||||||
self._write_session_data()
|
self._write_session_data()
|
||||||
|
|
||||||
logger.debug(f"记录HFC循环数据: cycle_id={record['cycle_id']}, action={record['action_type']}, time={record['total_time']:.2f}s")
|
logger.debug(
|
||||||
|
f"记录HFC循环数据: cycle_id={record['cycle_id']}, action={record['action_type']}, time={record['total_time']:.2f}s"
|
||||||
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"记录HFC循环数据失败: {e}")
|
logger.error(f"记录HFC循环数据失败: {e}")
|
||||||
@@ -67,7 +69,7 @@ class HFCPerformanceLogger:
|
|||||||
def _write_session_data(self):
|
def _write_session_data(self):
|
||||||
"""写入当前会话数据到文件"""
|
"""写入当前会话数据到文件"""
|
||||||
try:
|
try:
|
||||||
with open(self.session_file, 'w', encoding='utf-8') as f:
|
with open(self.session_file, "w", encoding="utf-8") as f:
|
||||||
json.dump(self.current_session_data, f, ensure_ascii=False, indent=2)
|
json.dump(self.current_session_data, f, ensure_ascii=False, indent=2)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"写入会话数据失败: {e}")
|
logger.error(f"写入会话数据失败: {e}")
|
||||||
@@ -84,7 +86,7 @@ class HFCPerformanceLogger:
|
|||||||
continue # 跳过当前会话文件
|
continue # 跳过当前会话文件
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(file_path, 'r', encoding='utf-8') as f:
|
with open(file_path, "r", encoding="utf-8") as f:
|
||||||
records = json.load(f)
|
records = json.load(f)
|
||||||
if isinstance(records, list):
|
if isinstance(records, list):
|
||||||
all_records.extend(records)
|
all_records.extend(records)
|
||||||
@@ -122,11 +124,7 @@ class HFCPerformanceLogger:
|
|||||||
step_times = record.get("step_times", {})
|
step_times = record.get("step_times", {})
|
||||||
|
|
||||||
if action_type not in action_groups:
|
if action_type not in action_groups:
|
||||||
action_groups[action_type] = {
|
action_groups[action_type] = {"count": 0, "total_times": [], "step_times": {}}
|
||||||
"count": 0,
|
|
||||||
"total_times": [],
|
|
||||||
"step_times": {}
|
|
||||||
}
|
|
||||||
|
|
||||||
action_groups[action_type]["count"] += 1
|
action_groups[action_type]["count"] += 1
|
||||||
action_groups[action_type]["total_times"].append(total_time)
|
action_groups[action_type]["total_times"].append(total_time)
|
||||||
@@ -149,7 +147,7 @@ class HFCPerformanceLogger:
|
|||||||
overall_stats = {
|
overall_stats = {
|
||||||
"total_records": total_records,
|
"total_records": total_records,
|
||||||
"avg_total_time": sum(total_times) / len(total_times) if total_times else 0,
|
"avg_total_time": sum(total_times) / len(total_times) if total_times else 0,
|
||||||
"avg_step_times": {}
|
"avg_step_times": {},
|
||||||
}
|
}
|
||||||
|
|
||||||
# 各步骤平均时间
|
# 各步骤平均时间
|
||||||
@@ -163,7 +161,7 @@ class HFCPerformanceLogger:
|
|||||||
"count": data["count"],
|
"count": data["count"],
|
||||||
"percentage": (data["count"] / total_records) * 100,
|
"percentage": (data["count"] / total_records) * 100,
|
||||||
"avg_total_time": sum(data["total_times"]) / len(data["total_times"]) if data["total_times"] else 0,
|
"avg_total_time": sum(data["total_times"]) / len(data["total_times"]) if data["total_times"] else 0,
|
||||||
"avg_step_times": {}
|
"avg_step_times": {},
|
||||||
}
|
}
|
||||||
|
|
||||||
# 该动作各步骤平均时间
|
# 该动作各步骤平均时间
|
||||||
@@ -175,7 +173,7 @@ class HFCPerformanceLogger:
|
|||||||
"version": self.version,
|
"version": self.version,
|
||||||
"last_updated": datetime.now().isoformat(),
|
"last_updated": datetime.now().isoformat(),
|
||||||
"overall": overall_stats,
|
"overall": overall_stats,
|
||||||
"by_action": action_stats
|
"by_action": action_stats,
|
||||||
}
|
}
|
||||||
|
|
||||||
def _update_stats_file(self, new_stats: Dict[str, Any]):
|
def _update_stats_file(self, new_stats: Dict[str, Any]):
|
||||||
@@ -184,7 +182,7 @@ class HFCPerformanceLogger:
|
|||||||
# 读取现有统计数据
|
# 读取现有统计数据
|
||||||
existing_stats = {}
|
existing_stats = {}
|
||||||
if self.stats_file.exists():
|
if self.stats_file.exists():
|
||||||
with open(self.stats_file, 'r', encoding='utf-8') as f:
|
with open(self.stats_file, "r", encoding="utf-8") as f:
|
||||||
existing_stats = json.load(f)
|
existing_stats = json.load(f)
|
||||||
|
|
||||||
# 更新当前chat_id和版本的统计数据
|
# 更新当前chat_id和版本的统计数据
|
||||||
@@ -192,7 +190,7 @@ class HFCPerformanceLogger:
|
|||||||
existing_stats[stats_key] = new_stats
|
existing_stats[stats_key] = new_stats
|
||||||
|
|
||||||
# 写回文件
|
# 写回文件
|
||||||
with open(self.stats_file, 'w', encoding='utf-8') as f:
|
with open(self.stats_file, "w", encoding="utf-8") as f:
|
||||||
json.dump(existing_stats, f, ensure_ascii=False, indent=2)
|
json.dump(existing_stats, f, ensure_ascii=False, indent=2)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -210,7 +208,7 @@ class HFCPerformanceLogger:
|
|||||||
try:
|
try:
|
||||||
if self.current_session_data:
|
if self.current_session_data:
|
||||||
# 计算当前会话统计数据
|
# 计算当前会话统计数据
|
||||||
current_stats = self._calculate_stats(self.current_session_data)
|
self._calculate_stats(self.current_session_data)
|
||||||
|
|
||||||
# 合并历史数据重新计算总体统计
|
# 合并历史数据重新计算总体统计
|
||||||
all_records = self.current_session_data[:]
|
all_records = self.current_session_data[:]
|
||||||
@@ -221,7 +219,7 @@ class HFCPerformanceLogger:
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(file_path, 'r', encoding='utf-8') as f:
|
with open(file_path, "r", encoding="utf-8") as f:
|
||||||
records = json.load(f)
|
records = json.load(f)
|
||||||
if isinstance(records, list):
|
if isinstance(records, list):
|
||||||
all_records.extend(records)
|
all_records.extend(records)
|
||||||
@@ -232,7 +230,9 @@ class HFCPerformanceLogger:
|
|||||||
total_stats = self._calculate_stats(all_records)
|
total_stats = self._calculate_stats(all_records)
|
||||||
self._update_stats_file(total_stats)
|
self._update_stats_file(total_stats)
|
||||||
|
|
||||||
logger.info(f"完成会话统计,当前会话 {len(self.current_session_data)} 条记录,总共 {len(all_records)} 条记录")
|
logger.info(
|
||||||
|
f"完成会话统计,当前会话 {len(self.current_session_data)} 条记录,总共 {len(all_records)} 条记录"
|
||||||
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"结束会话统计失败: {e}")
|
logger.error(f"结束会话统计失败: {e}")
|
||||||
@@ -84,7 +84,7 @@ class HFCVersionManager:
|
|||||||
base_version = cls.DEFAULT_VERSION
|
base_version = cls.DEFAULT_VERSION
|
||||||
|
|
||||||
# 提取基础版本号的主要部分
|
# 提取基础版本号的主要部分
|
||||||
base_match = re.match(r'v?(\d+\.\d+)', base_version)
|
base_match = re.match(r"v?(\d+\.\d+)", base_version)
|
||||||
if base_match:
|
if base_match:
|
||||||
base_part = base_match.group(1)
|
base_part = base_match.group(1)
|
||||||
else:
|
else:
|
||||||
@@ -118,18 +118,18 @@ class HFCVersionManager:
|
|||||||
# 支持的格式:
|
# 支持的格式:
|
||||||
# v1.0.0, 1.0.0, v1.0, 1.0, v1.0.0.20241222_1530 等
|
# v1.0.0, 1.0.0, v1.0, 1.0, v1.0.0.20241222_1530 等
|
||||||
patterns = [
|
patterns = [
|
||||||
r'^v?(\d+\.\d+\.\d+)$', # v1.0.0 或 1.0.0
|
r"^v?(\d+\.\d+\.\d+)$", # v1.0.0 或 1.0.0
|
||||||
r'^v?(\d+\.\d+)$', # v1.0 或 1.0
|
r"^v?(\d+\.\d+)$", # v1.0 或 1.0
|
||||||
r'^v?(\d+\.\d+\.\d+\.\w+)$', # v1.0.0.build 或 1.0.0.build
|
r"^v?(\d+\.\d+\.\d+\.\w+)$", # v1.0.0.build 或 1.0.0.build
|
||||||
r'^v?(\d+\.\d+\.\w+)$', # v1.0.build 或 1.0.build
|
r"^v?(\d+\.\d+\.\w+)$", # v1.0.build 或 1.0.build
|
||||||
]
|
]
|
||||||
|
|
||||||
for pattern in patterns:
|
for pattern in patterns:
|
||||||
match = re.match(pattern, version)
|
match = re.match(pattern, version)
|
||||||
if match:
|
if match:
|
||||||
# 确保版本号以v开头
|
# 确保版本号以v开头
|
||||||
if not version.startswith('v'):
|
if not version.startswith("v"):
|
||||||
version = 'v' + version
|
version = "v" + version
|
||||||
return version
|
return version
|
||||||
|
|
||||||
return None
|
return None
|
||||||
@@ -154,7 +154,7 @@ class HFCVersionManager:
|
|||||||
"default_version": cls.DEFAULT_VERSION,
|
"default_version": cls.DEFAULT_VERSION,
|
||||||
"is_custom": current != cls.DEFAULT_VERSION,
|
"is_custom": current != cls.DEFAULT_VERSION,
|
||||||
"env_version": os.getenv("HFC_PERFORMANCE_VERSION"),
|
"env_version": os.getenv("HFC_PERFORMANCE_VERSION"),
|
||||||
"timestamp": datetime.now().isoformat()
|
"timestamp": datetime.now().isoformat(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -345,6 +345,7 @@ class ChatManager:
|
|||||||
async def load_all_streams(self):
|
async def load_all_streams(self):
|
||||||
"""从数据库加载所有聊天流"""
|
"""从数据库加载所有聊天流"""
|
||||||
logger.info("正在从数据库加载所有聊天流")
|
logger.info("正在从数据库加载所有聊天流")
|
||||||
|
|
||||||
def _db_load_all_streams_sync():
|
def _db_load_all_streams_sync():
|
||||||
loaded_streams_data = []
|
loaded_streams_data = []
|
||||||
for model_instance in ChatStreams.select():
|
for model_instance in ChatStreams.select():
|
||||||
|
|||||||
@@ -198,21 +198,15 @@ class StatisticOutputTask(AsyncTask):
|
|||||||
logger.info("正在收集统计数据...")
|
logger.info("正在收集统计数据...")
|
||||||
|
|
||||||
# 数据收集任务
|
# 数据收集任务
|
||||||
collect_task = loop.run_in_executor(
|
collect_task = loop.run_in_executor(executor, self._collect_all_statistics, now)
|
||||||
executor, self._collect_all_statistics, now
|
|
||||||
)
|
|
||||||
|
|
||||||
# 等待数据收集完成
|
# 等待数据收集完成
|
||||||
stats = await collect_task
|
stats = await collect_task
|
||||||
logger.info("统计数据收集完成")
|
logger.info("统计数据收集完成")
|
||||||
|
|
||||||
# 并行执行控制台输出和HTML报告生成
|
# 并行执行控制台输出和HTML报告生成
|
||||||
console_task = loop.run_in_executor(
|
console_task = loop.run_in_executor(executor, self._statistic_console_output, stats, now)
|
||||||
executor, self._statistic_console_output, stats, now
|
html_task = loop.run_in_executor(executor, self._generate_html_report, stats, now)
|
||||||
)
|
|
||||||
html_task = loop.run_in_executor(
|
|
||||||
executor, self._generate_html_report, stats, now
|
|
||||||
)
|
|
||||||
|
|
||||||
# 等待两个输出任务完成
|
# 等待两个输出任务完成
|
||||||
await asyncio.gather(console_task, html_task)
|
await asyncio.gather(console_task, html_task)
|
||||||
@@ -226,6 +220,7 @@ class StatisticOutputTask(AsyncTask):
|
|||||||
备选方案:完全异步后台运行统计输出
|
备选方案:完全异步后台运行统计输出
|
||||||
使用此方法可以让统计任务完全非阻塞
|
使用此方法可以让统计任务完全非阻塞
|
||||||
"""
|
"""
|
||||||
|
|
||||||
async def _async_collect_and_output():
|
async def _async_collect_and_output():
|
||||||
try:
|
try:
|
||||||
import concurrent.futures
|
import concurrent.futures
|
||||||
@@ -246,12 +241,8 @@ class StatisticOutputTask(AsyncTask):
|
|||||||
|
|
||||||
# 创建并发的输出任务
|
# 创建并发的输出任务
|
||||||
output_tasks = [
|
output_tasks = [
|
||||||
asyncio.create_task(
|
asyncio.create_task(loop.run_in_executor(executor, self._statistic_console_output, stats, now)),
|
||||||
loop.run_in_executor(executor, self._statistic_console_output, stats, now)
|
asyncio.create_task(loop.run_in_executor(executor, self._generate_html_report, stats, now)),
|
||||||
),
|
|
||||||
asyncio.create_task(
|
|
||||||
loop.run_in_executor(executor, self._generate_html_report, stats, now)
|
|
||||||
)
|
|
||||||
]
|
]
|
||||||
|
|
||||||
# 等待所有输出任务完成
|
# 等待所有输出任务完成
|
||||||
@@ -1232,6 +1223,7 @@ class AsyncStatisticOutputTask(AsyncTask):
|
|||||||
|
|
||||||
async def run(self):
|
async def run(self):
|
||||||
"""完全异步执行统计任务"""
|
"""完全异步执行统计任务"""
|
||||||
|
|
||||||
async def _async_collect_and_output():
|
async def _async_collect_and_output():
|
||||||
try:
|
try:
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
@@ -1250,12 +1242,8 @@ class AsyncStatisticOutputTask(AsyncTask):
|
|||||||
|
|
||||||
# 创建并发的输出任务
|
# 创建并发的输出任务
|
||||||
output_tasks = [
|
output_tasks = [
|
||||||
asyncio.create_task(
|
asyncio.create_task(loop.run_in_executor(executor, self._statistic_console_output, stats, now)),
|
||||||
loop.run_in_executor(executor, self._statistic_console_output, stats, now)
|
asyncio.create_task(loop.run_in_executor(executor, self._generate_html_report, stats, now)),
|
||||||
),
|
|
||||||
asyncio.create_task(
|
|
||||||
loop.run_in_executor(executor, self._generate_html_report, stats, now)
|
|
||||||
)
|
|
||||||
]
|
]
|
||||||
|
|
||||||
# 等待所有输出任务完成
|
# 等待所有输出任务完成
|
||||||
|
|||||||
@@ -72,11 +72,11 @@ os.makedirs(_DB_DIR, exist_ok=True)
|
|||||||
db = SqliteDatabase(
|
db = SqliteDatabase(
|
||||||
_DB_FILE,
|
_DB_FILE,
|
||||||
pragmas={
|
pragmas={
|
||||||
'journal_mode': 'wal', # WAL模式提高并发性能
|
"journal_mode": "wal", # WAL模式提高并发性能
|
||||||
'cache_size': -64 * 1000, # 64MB缓存
|
"cache_size": -64 * 1000, # 64MB缓存
|
||||||
'foreign_keys': 1,
|
"foreign_keys": 1,
|
||||||
'ignore_check_constraints': 0,
|
"ignore_check_constraints": 0,
|
||||||
'synchronous': 0, # 异步写入提高性能
|
"synchronous": 0, # 异步写入提高性能
|
||||||
'busy_timeout': 1000, # 1秒超时而不是3秒
|
"busy_timeout": 1000, # 1秒超时而不是3秒
|
||||||
}
|
},
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -49,7 +49,6 @@ class Individuality:
|
|||||||
self.bot_person_id = person_info_manager.get_person_id("system", "bot_id")
|
self.bot_person_id = person_info_manager.get_person_id("system", "bot_id")
|
||||||
self.name = bot_nickname
|
self.name = bot_nickname
|
||||||
|
|
||||||
|
|
||||||
# 检查配置变化,如果变化则清空
|
# 检查配置变化,如果变化则清空
|
||||||
await self._check_config_and_clear_if_changed(
|
await self._check_config_and_clear_if_changed(
|
||||||
bot_nickname, personality_core, personality_sides, identity_detail
|
bot_nickname, personality_core, personality_sides, identity_detail
|
||||||
@@ -63,7 +62,6 @@ class Individuality:
|
|||||||
# 初始化身份
|
# 初始化身份
|
||||||
self.identity = Identity(identity_detail=identity_detail)
|
self.identity = Identity(identity_detail=identity_detail)
|
||||||
|
|
||||||
|
|
||||||
logger.info("正在将所有人设写入impression")
|
logger.info("正在将所有人设写入impression")
|
||||||
# 将所有人设写入impression
|
# 将所有人设写入impression
|
||||||
impression_parts = []
|
impression_parts = []
|
||||||
|
|||||||
@@ -62,11 +62,11 @@ class PersonInfoManager:
|
|||||||
try:
|
try:
|
||||||
db.connect(reuse_if_open=True)
|
db.connect(reuse_if_open=True)
|
||||||
# 设置连接池参数
|
# 设置连接池参数
|
||||||
if hasattr(db, 'execute_sql'):
|
if hasattr(db, "execute_sql"):
|
||||||
# 设置SQLite优化参数
|
# 设置SQLite优化参数
|
||||||
db.execute_sql('PRAGMA cache_size = -64000') # 64MB缓存
|
db.execute_sql("PRAGMA cache_size = -64000") # 64MB缓存
|
||||||
db.execute_sql('PRAGMA temp_store = memory') # 临时存储在内存中
|
db.execute_sql("PRAGMA temp_store = memory") # 临时存储在内存中
|
||||||
db.execute_sql('PRAGMA mmap_size = 268435456') # 256MB内存映射
|
db.execute_sql("PRAGMA mmap_size = 268435456") # 256MB内存映射
|
||||||
db.create_tables([PersonInfo], safe=True)
|
db.create_tables([PersonInfo], safe=True)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"数据库连接或 PersonInfo 表创建失败: {e}")
|
logger.error(f"数据库连接或 PersonInfo 表创建失败: {e}")
|
||||||
@@ -165,7 +165,6 @@ class PersonInfoManager:
|
|||||||
async def update_one_field(self, person_id: str, field_name: str, value, data: dict = None):
|
async def update_one_field(self, person_id: str, field_name: str, value, data: dict = None):
|
||||||
"""更新某一个字段,会补全"""
|
"""更新某一个字段,会补全"""
|
||||||
if field_name not in PersonInfo._meta.fields:
|
if field_name not in PersonInfo._meta.fields:
|
||||||
|
|
||||||
logger.debug(f"更新'{field_name}'失败,未在 PersonInfo Peewee 模型中定义的字段。")
|
logger.debug(f"更新'{field_name}'失败,未在 PersonInfo Peewee 模型中定义的字段。")
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -178,6 +177,7 @@ class PersonInfoManager:
|
|||||||
|
|
||||||
def _db_update_sync(p_id: str, f_name: str, val_to_set):
|
def _db_update_sync(p_id: str, f_name: str, val_to_set):
|
||||||
import time
|
import time
|
||||||
|
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
try:
|
try:
|
||||||
record = PersonInfo.get_or_none(PersonInfo.person_id == p_id)
|
record = PersonInfo.get_or_none(PersonInfo.person_id == p_id)
|
||||||
@@ -190,7 +190,9 @@ class PersonInfoManager:
|
|||||||
|
|
||||||
total_time = save_time - start_time
|
total_time = save_time - start_time
|
||||||
if total_time > 0.5: # 如果超过500ms就记录日志
|
if total_time > 0.5: # 如果超过500ms就记录日志
|
||||||
logger.warning(f"数据库更新操作耗时 {total_time:.3f}秒 (查询: {query_time-start_time:.3f}s, 保存: {save_time-query_time:.3f}s) person_id={p_id}, field={f_name}")
|
logger.warning(
|
||||||
|
f"数据库更新操作耗时 {total_time:.3f}秒 (查询: {query_time - start_time:.3f}s, 保存: {save_time - query_time:.3f}s) person_id={p_id}, field={f_name}"
|
||||||
|
)
|
||||||
|
|
||||||
return True, False # Found and updated, no creation needed
|
return True, False # Found and updated, no creation needed
|
||||||
else:
|
else:
|
||||||
@@ -203,7 +205,6 @@ class PersonInfoManager:
|
|||||||
logger.error(f"数据库操作异常,耗时 {total_time:.3f}秒: {e}")
|
logger.error(f"数据库操作异常,耗时 {total_time:.3f}秒: {e}")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
found, needs_creation = await asyncio.to_thread(_db_update_sync, person_id, field_name, processed_value)
|
found, needs_creation = await asyncio.to_thread(_db_update_sync, person_id, field_name, processed_value)
|
||||||
|
|
||||||
if needs_creation:
|
if needs_creation:
|
||||||
|
|||||||
Reference in New Issue
Block a user