2.4 ReAct的性能评估与调优(上) 读者读完这节,能掌握ReAct智能体的核心性能评估方法,学会系统性的监控策略 学习目标 理解ReAct智能体的核心性能评估指标 掌握性能监控框架的搭建方法 学会使用基础性能分析工具 能够建立简单的性能基准测试 了解常见的性能瓶颈识别方法 核心概念 ReAct智能体的性能评估主要关注以下几个核心维度: 推理质量:智能体推理的准确性、完整性和逻辑性 执行效率:工具调用速度、响应时间和资源消耗 系统稳定性:错误处理能力、容错性和可靠性 资源利用:内存占用、计算效率和扩展性 ReAct性能评估框架:核心指标体系 环境准备 / 前置知识 必需依赖 推荐开发环境 性能分析工具:cProfile, timeit 监控工具:psutil
读者读完这节,能掌握ReAct智能体的核心性能评估方法,学会系统性的监控策略
ReAct智能体的性能评估主要关注以下几个核心维度:
# 性能评估基础依赖 pytest>=7.0.0 timeit>=0.9.0 psutil>=5.9.0 matplotlib>=3.5.0
import time import psutil from dataclasses import dataclass from typing import List, Dict, Any, Optional from datetime import datetime import statistics @dataclass class CorePerformanceMetrics: """核心性能指标数据结构""" timestamp: datetime task_name: str total_time: float reasoning_time: float action_time: float observation_time: float memory_usage: float cpu_usage: float iterations: int success_rate: float tool_call_count: int error_count: int class CorePerformanceEvaluator: """核心性能评估器""" def __init__(self): self.metrics_history: List[CorePerformanceMetrics] = [] self.current_metrics = None self.start_time = None self.resource_monitor = ResourceMonitor() def start_evaluation(self, task_name: str): """开始性能评估""" self.start_time = time.time() self.current_metrics = CorePerformanceMetrics( timestamp=datetime.now(), task_name=task_name, total_time=0.0, reasoning_time=0.0, action_time=0.0, observation_time=0.0, memory_usage=0.0, cpu_usage=0.0, iterations=0, success_rate=0.0, tool_call_count=0, error_count=0 ) self.resource_monitor.start_monitoring() def record_reasoning_time(self, duration: float): """记录推理时间""" self.current_metrics.reasoning_time += duration def record_action_time(self, duration: float): """记录行动时间""" self.current_metrics.action_time += duration self.current_metrics.tool_call_count += 1 def record_observation_time(self, duration: float): """记录观察时间""" self.current_metrics.observation_time += duration def record_iteration(self): """记录迭代次数""" self.current_metrics.iterations += 1 def record_error(self): """记录错误""" self.current_metrics.error_count += 1 def end_evaluation(self, success: bool, accuracy_score: float = 0.0): """结束性能评估""" if self.current_metrics and self.start_time: self.current_metrics.total_time = time.time() - self.start_time self.current_metrics.memory_usage = self.resource_monitor.get_max_memory() self.current_metrics.cpu_usage = self.resource_monitor.get_avg_cpu() self.current_metrics.success_rate = 1.0 if success else 0.0 self.current_metrics.accuracy_score = accuracy_score self.metrics_history.append(self.current_metrics) self.resource_monitor.stop_monitoring() def get_statistics(self) -> Dict[str, Any]: """获取性能统计信息""" if not self.metrics_history: return {} total_metrics = len(self.metrics_history) success_tasks = sum(1 for m in self.metrics_history if m.success_rate == 1.0) return { "total_tasks": total_metrics, "success_rate": success_tasks / total_metrics, "avg_total_time": statistics.mean(m.total_time for m in self.metrics_history), "avg_iterations": statistics.mean(m.iterations for m in self.metrics_history), "avg_memory_usage": statistics.mean(m.memory_usage for m in self.metrics_history), "avg_tool_calls": statistics.mean(m.tool_call_count for m in self.metrics_history), "error_rate": sum(m.error_count for m in self.metrics_history) / total_metrics } class ResourceMonitor: """资源监控器""" def __init__(self): self.process = psutil.Process() self.start_time = None self.monitoring = False self.memory_samples = [] self.cpu_samples = [] def start_monitoring(self): """开始监控""" self.start_time = time.time() self.monitoring = True self.memory_samples = [] self.cpu_samples = [] def stop_monitoring(self): """停止监控""" self.monitoring = False def record_sample(self): """记录采样""" if not self.monitoring: return try: memory_info = self.process.memory_info() cpu_percent = self.process.cpu_percent() self.memory_samples.append(memory_info.rss / 1024 / 1024) # MB self.cpu_samples.append(cpu_percent) except Exception: pass def get_max_memory(self) -> float: """获取最大内存使用量(MB)""" return max(self.memory_samples) if self.memory_samples else 0.0 def get_avg_cpu(self) -> float: """获取平均CPU使用率""" return statistics.mean(self.cpu_samples) if self.cpu_samples else 0.0
import unittest from unittest.mock import Mock import threading import time from concurrent.futures import ThreadPoolExecutor class CorePerformanceTest(unittest.TestCase): """基础性能测试框架""" def setUp(self): self.evaluator = CorePerformanceEvaluator() self.test_tasks = [ "简单计算任务", "中等复杂度推理任务", "复杂多步骤任务" ] def test_simple_task_performance(self): """测试简单任务性能""" task_name = "简单计算任务" self.evaluator.start_evaluation(task_name) # 模拟执行过程 time.sleep(0.1) # 推理时间 self.evaluator.record_reasoning_time(0.1) time.sleep(0.05) # 行动时间 self.evaluator.record_action_time(0.05) time.sleep(0.02) # 观察时间 self.evaluator.record_observation_time(0.02) self.evaluator.record_iteration() self.evaluator.end_evaluation(True, 0.95) # 验证结果 stats = self.evaluator.get_statistics() self.assertGreater(stats["success_rate"], 0.8) self.assertLess(stats["avg_total_time"], 1.0) def test_complex_task_performance(self): """测试复杂任务性能""" task_name = "复杂多步骤任务" self.evaluator.start_evaluation(task_name) # 模拟复杂执行过程 for i in range(5): time.sleep(0.2) # 推理时间 self.evaluator.record_reasoning_time(0.2) time.sleep(0.1) # 行动时间 self.evaluator.record_action_time(0.1) time.sleep(0.05) # 观察时间 self.evaluator.record_observation_time(0.05) self.evaluator.record_iteration() time.sleep(0.1) # 最终推理 self.evaluator.record_reasoning_time(0.1) self.evaluator.end_evaluation(True, 0.85) # 验证结果 stats = self.evaluator.get_statistics() self.assertGreater(stats["avg_iterations"], 3) self.assertLess(stats["avg_total_time"], 2.0) class StressTestFramework: """压力测试框架""" def __init__(self, agent_class, max_concurrent: int = 10): self.agent_class = agent_class self.max_concurrent = max_concurrent self.results = [] def run_concurrent_test(self, task: str, iterations: int = 100): """运行并发测试""" results = [] def execute_task(task_id): agent = self.agent_class() start_time = time.time() result = agent.run(task) end_time = time.time() return { "task_id": task_id, "duration": end_time - start_time, "success": result["success"], "iterations": result["iterations"] } with ThreadPoolExecutor(max_workers=self.max_concurrent) as executor: futures = [executor.submit(execute_task, i) for i in range(iterations)] for future in futures: result = future.result() results.append(result) self.results.extend(results) return results def get_stress_test_results(self) -> Dict[str, Any]: """获取压力测试结果""" if not self.results: return {} total_tasks = len(self.results) success_tasks = sum(1 for r in self.results if r["success"]) durations = [r["duration"] for r in self.results] iterations = [r["iterations"] for r in self.results] return { "total_tasks": total_tasks, "success_rate": success_tasks / total_tasks, "avg_duration": statistics.mean(durations), "max_duration": max(durations), "min_duration": min(durations), "avg_iterations": statistics.mean(iterations), "concurrent_limit": self.max_concurrent }
import cProfile import pstats import io class CoreProfilerManager: """基础性能分析器管理器""" def __init__(self): self.profiler = cProfile.Profile() self.results = {} def profile_function(self, func, *args, **kwargs): """分析函数性能""" # CPU性能分析 self.profiler.enable() result = func(*args, **kwargs) self.profiler.disable() # 获取分析结果 stats = pstats.Stats(self.profiler) stats.sort_stats('cumulative') # 保存结果 result_key = f"{func.__name__}_{time.time()}" self.results[result_key] = { 'cpu_stats': stats, 'result': result } return result, stats def get_function_stats(self, func_name: str) -> Dict[str, Any]: """获取特定函数的性能统计""" if func_name not in self.results: return {} stats = self.results[func_name]['cpu_stats'] # 获取调用次数和总执行时间 call_count = stats.stats.get(func_name, {}).get('ncalls', 0) total_time = stats.stats.get(func_name, {}).get('tottime', 0) return { 'function_name': func_name, 'call_count': call_count, 'total_time': total_time, 'average_time': total_time / call_count if call_count > 0 else 0 } class TimeProfiler: """时间分析器""" def __init__(self): self.timings = {} def time_function(self, func): """装饰器:分析函数执行时间""" def wrapper(*args, **kwargs): start_time = time.time() result = func(*args, **kwargs) end_time = time.time() duration = end_time - start_time func_name = func.__name__ if func_name not in self.timings: self.timings[func_name] = [] self.timings[func_name].append(duration) return result return wrapper def get_timing_stats(self, func_name: str) -> Dict[str, Any]: """获取函数时间统计""" if func_name not in self.timings: return {} timings = self.timings[func_name] return { 'function_name': func_name, 'call_count': len(timings), 'total_time': sum(timings), 'average_time': statistics.mean(timings), 'min_time': min(timings), 'max_time': max(timings), 'median_time': statistics.median(timings) }
import matplotlib.pyplot as plt import seaborn as sns import pandas as pd from datetime import datetime, timedelta class CorePerformanceVisualizer: """基础性能可视化工具""" def __init__(self, evaluator: CorePerformanceEvaluator): self.evaluator = evaluator plt.style.use('seaborn-v0_8') sns.set_palette("husl") def generate_performance_report(self, save_path: str = None): """生成基础性能报告""" # 创建图表 fig, axes = plt.subplots(2, 2, figsize=(15, 10)) fig.suptitle('ReAct智能体性能评估报告', fontsize=16, fontweight='bold') # 1. 执行时间分布 self._plot_execution_time(axes[0, 0]) # 2. 迭代次数分布 self._plot_iterations_distribution(axes[0, 1]) # 3. 内存使用趋势 self._plot_memory_trend(axes[1, 0]) # 4. 成功率分析 self._plot_success_rate(axes[1, 1]) plt.tight_layout() if save_path: plt.savefig(save_path, dpi=300, bbox_inches='tight') plt.show() def _plot_execution_time(self, ax): """绘制执行时间分布""" metrics = self.evaluator.metrics_history if not metrics: return times = [m.total_time for m in metrics] task_names = [m.task_name for m in metrics] bars = ax.bar(task_names, times, color='skyblue', alpha=0.7) ax.set_title('任务执行时间分布', fontweight='bold') ax.set_ylabel('执行时间 (秒)') ax.set_xlabel('任务类型') # 添加数值标签 for bar, time_val in zip(bars, times): height = bar.get_height() ax.text(bar.get_x() + bar.get_width()/2., height, f'{time_val:.2f}s', ha='center', va='bottom') def _plot_iterations_distribution(self, ax): """绘制迭代次数分布""" metrics = self.evaluator.metrics_history if not metrics: return iterations = [m.iterations for m in metrics] task_names = [m.task_name for m in metrics] bars = ax.bar(task_names, iterations, color='lightgreen', alpha=0.7) ax.set_title('迭代次数分布', fontweight='bold') ax.set_ylabel('迭代次数') ax.set_xlabel('任务类型') # 添加数值标签 for bar, iter_count in zip(bars, iterations): height = bar.get_height() ax.text(bar.get_x() + bar.get_width()/2., height, f'{iter_count}', ha='center', va='bottom') def _plot_memory_trend(self, ax): """绘制内存使用趋势""" metrics = self.evaluator.metrics_history if not metrics: return memory_usage = [m.memory_usage for m in metrics] timestamps = [m.timestamp for m in metrics] ax.plot(timestamps, memory_usage, 'o-', color='red', linewidth=2, markersize=6) ax.set_title('内存使用趋势', fontweight='bold') ax.set_ylabel('内存使用量 (MB)') ax.set_xlabel('时间') # 格式化时间轴 ax.tick_params(axis='x', rotation=45) def _plot_success_rate(self, ax): """绘制成功率分析""" metrics = self.evaluator.metrics_history if not metrics: return success_rates = [m.success_rate for m in metrics] task_names = [m.task_name for m in metrics] colors = ['green' if rate > 0.8 else 'orange' if rate > 0.5 else 'red' for rate in success_rates] bars = ax.bar(task_names, success_rates, color=colors, alpha=0.7) ax.set_title('任务成功率', fontweight='bold') ax.set_ylabel('成功率') ax.set_xlabel('任务类型') ax.set_ylim(0, 1) # 添加数值标签 for bar, rate in zip(bars, success_rates): height = bar.get_height() ax.text(bar.get_x() + bar.get_width()/2., height, f'{rate:.2%}', ha='center', va='bottom')
# 完整的基础性能评估示例 def main(): """主函数演示基础性能评估流程""" # 1. 初始化评估器 evaluator = CorePerformanceEvaluator() visualizer = CorePerformanceVisualizer(evaluator) # 2. 测试不同复杂度的任务 test_tasks = [ ("简单计算任务", "1+1等于多少"), ("中等推理任务", "搜索Python的最新特性"), ("复杂多步骤任务", "分析人工智能发展趋势") ] print("开始基础性能评估测试...") print("=" * 50) for task_name, task_query in test_tasks: print(f"\n测试任务: {task_name}") # 模拟ReAct智能体执行 evaluator.start_evaluation(task_name) # 模拟执行过程 for i in range(3): # 推理阶段 time.sleep(0.5) evaluator.record_reasoning_time(0.5) # 行动阶段 time.sleep(0.3) evaluator.record_action_time(0.3) evaluator.record_iteration() # 观察阶段 time.sleep(0.2) evaluator.record_observation_time(0.2) # 完成评估 evaluator.end_evaluation(True, 0.9) # 输出当前任务统计 current_stats = evaluator.get_statistics() print(f"执行时间: {current_stats['avg_total_time']:.2f}秒") print(f"迭代次数: {current_stats['avg_iterations']}") print(f"内存使用: {current_stats['avg_memory_usage']:.2f}MB") # 3. 生成详细统计报告 print("\n性能统计报告:") print("=" * 30) stats = evaluator.get_statistics() for key, value in stats.items(): if isinstance(value, float): print(f"{key}: {value:.4f}") else: print(f"{key}: {value}") # 4. 生成可视化报告 print("\n生成性能可视化报告...") visualizer.generate_performance_report("/tmp/core_performance_report.png") # 5. 演示压力测试 print("\n开始压力测试...") stress_test = StressTestFramework(MockAgent, max_concurrent=5) stress_results = stress_test.run_concurrent_test("搜索天气", iterations=20) print("\n压力测试结果:") stress_stats = stress_test.get_stress_test_results() for key, value in stress_stats.items(): if isinstance(value, float): print(f"{key}: {value:.4f}") else: print(f"{key}: {value}") if __name__ == "__main__": main()
A:选择性能指标时需要考虑以下因素:
A:性能监控频率应该根据以下原则设置:
A:识别性能瓶颈的方法包括:
本节介绍了ReAct智能体性能评估的基础框架,包括:
通过这些基础工具和方法,你可以开始评估ReAct智能体的性能表现,为后续的深度调优奠定基础。
在下一节中,我们将深入探讨更高级的性能调优策略和优化技巧。
关键词:Agent智能体开发实战,ReAct性能,基础评估,监控框架,性能测试
难度:进阶
预计阅读:25分钟