benchmark_simple.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/env python3
  2. """
  3. 简化版性能基准测试
  4. 模拟性能测试,用于验证测试框架本身。
  5. 实际性能测试需要运行应用服务器。
  6. """
  7. import time
  8. import statistics
  9. from typing import List, Dict, Any
  10. import json
  11. from datetime import datetime
  12. def simulate_api_call(endpoint: str, complexity: str = "simple") -> float:
  13. """模拟 API 调用,返回响应时间(秒)"""
  14. # 根据端点复杂度模拟不同的响应时间
  15. base_times = {
  16. "simple": 0.01, # 10ms - 健康检查等
  17. "moderate": 0.05, # 50ms - 简单查询
  18. "complex": 0.15, # 150ms - 复杂搜索
  19. "heavy": 0.3 # 300ms - 大量计算
  20. }
  21. base_time = base_times.get(complexity, 0.05)
  22. # 添加一些随机变化(±20%)
  23. import random
  24. variation = random.uniform(0.8, 1.2)
  25. time.sleep(base_time * variation * 0.01) # 实际只睡眠很短时间,用于模拟
  26. return base_time * variation
  27. def benchmark_endpoint(endpoint: str, complexity: str, iterations: int = 100) -> Dict[str, Any]:
  28. """对端点进行基准测试"""
  29. response_times = []
  30. print(f" 测试 {endpoint} ({iterations} 次请求)...", end=" ")
  31. for _ in range(iterations):
  32. response_time = simulate_api_call(endpoint, complexity)
  33. response_times.append(response_time)
  34. # 计算统计数据
  35. stats = {
  36. "endpoint": endpoint,
  37. "requests": len(response_times),
  38. "response_time": {
  39. "min": min(response_times) * 1000,
  40. "max": max(response_times) * 1000,
  41. "mean": statistics.mean(response_times) * 1000,
  42. "median": statistics.median(response_times) * 1000,
  43. "p95": statistics.quantiles(response_times, n=20)[18] * 1000 if len(response_times) > 1 else response_times[0] * 1000,
  44. "p99": statistics.quantiles(response_times, n=100)[98] * 1000 if len(response_times) > 1 else response_times[0] * 1000,
  45. }
  46. }
  47. print("完成")
  48. return stats
  49. def main():
  50. """主函数"""
  51. print("=" * 80)
  52. print("RAG 系统性能基准测试(模拟)")
  53. print("=" * 80)
  54. print(f"测试时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
  55. print("注意: 这是模拟测试,实际性能需要运行真实服务器测试")
  56. print("=" * 80)
  57. # 定义测试端点
  58. endpoints = [
  59. ("/health", "simple", 100),
  60. ("/metrics", "simple", 100),
  61. ("/api/v1/documents/", "moderate", 50),
  62. ("/api/v1/documents/search", "complex", 100),
  63. ("/api/v1/knowledge-bases/", "moderate", 100),
  64. ]
  65. results = []
  66. print("\n开始测试...\n")
  67. for endpoint, complexity, iterations in endpoints:
  68. stats = benchmark_endpoint(endpoint, complexity, iterations)
  69. results.append(stats)
  70. # 打印结果
  71. print("\n" + "=" * 80)
  72. print("测试结果")
  73. print("=" * 80)
  74. for stats in results:
  75. print(f"\n✓ {stats['endpoint']}")
  76. print(f" 请求数: {stats['requests']}")
  77. print(f" 响应时间 (ms):")
  78. print(f" 最小值: {stats['response_time']['min']:.2f}")
  79. print(f" 最大值: {stats['response_time']['max']:.2f}")
  80. print(f" 平均值: {stats['response_time']['mean']:.2f}")
  81. print(f" 中位数: {stats['response_time']['median']:.2f}")
  82. print(f" P95: {stats['response_time']['p95']:.2f}")
  83. print(f" P99: {stats['response_time']['p99']:.2f}")
  84. # 性能评估
  85. print("\n" + "=" * 80)
  86. print("性能评估(基于模拟数据)")
  87. print("=" * 80)
  88. for stats in results:
  89. mean_time = stats['response_time']['mean']
  90. endpoint = stats['endpoint']
  91. # 根据端点类型设置不同的阈值
  92. if endpoint in ["/health", "/metrics"]:
  93. threshold = 50
  94. target = "< 50ms"
  95. elif "search" in endpoint:
  96. threshold = 500
  97. target = "< 500ms"
  98. else:
  99. threshold = 200
  100. target = "< 200ms"
  101. status = "✓ 通过" if mean_time < threshold else "⚠️ 需要优化"
  102. print(f"{endpoint}")
  103. print(f" 平均响应时间: {mean_time:.2f}ms (目标: {target}) - {status}")
  104. # 保存结果
  105. results_data = {
  106. "timestamp": datetime.now().isoformat(),
  107. "type": "simulated",
  108. "results": results
  109. }
  110. filename = "benchmark_results_simulated.json"
  111. with open(filename, 'w', encoding='utf-8') as f:
  112. json.dump(results_data, f, indent=2, ensure_ascii=False)
  113. print("\n" + "=" * 80)
  114. print(f"结果已保存到: {filename}")
  115. print("\n要运行真实的性能测试,请:")
  116. print("1. 启动应用: python main.py")
  117. print("2. 运行测试: python scripts/benchmark.py")
  118. print("=" * 80)
  119. if __name__ == "__main__":
  120. main()