| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- #!/usr/bin/env python3
- """
- 简化版性能基准测试
- 模拟性能测试,用于验证测试框架本身。
- 实际性能测试需要运行应用服务器。
- """
- import time
- import statistics
- from typing import List, Dict, Any
- import json
- from datetime import datetime
- def simulate_api_call(endpoint: str, complexity: str = "simple") -> float:
- """模拟 API 调用,返回响应时间(秒)"""
- # 根据端点复杂度模拟不同的响应时间
- base_times = {
- "simple": 0.01, # 10ms - 健康检查等
- "moderate": 0.05, # 50ms - 简单查询
- "complex": 0.15, # 150ms - 复杂搜索
- "heavy": 0.3 # 300ms - 大量计算
- }
-
- base_time = base_times.get(complexity, 0.05)
- # 添加一些随机变化(±20%)
- import random
- variation = random.uniform(0.8, 1.2)
-
- time.sleep(base_time * variation * 0.01) # 实际只睡眠很短时间,用于模拟
- return base_time * variation
- def benchmark_endpoint(endpoint: str, complexity: str, iterations: int = 100) -> Dict[str, Any]:
- """对端点进行基准测试"""
- response_times = []
-
- print(f" 测试 {endpoint} ({iterations} 次请求)...", end=" ")
-
- for _ in range(iterations):
- response_time = simulate_api_call(endpoint, complexity)
- response_times.append(response_time)
-
- # 计算统计数据
- stats = {
- "endpoint": endpoint,
- "requests": len(response_times),
- "response_time": {
- "min": min(response_times) * 1000,
- "max": max(response_times) * 1000,
- "mean": statistics.mean(response_times) * 1000,
- "median": statistics.median(response_times) * 1000,
- "p95": statistics.quantiles(response_times, n=20)[18] * 1000 if len(response_times) > 1 else response_times[0] * 1000,
- "p99": statistics.quantiles(response_times, n=100)[98] * 1000 if len(response_times) > 1 else response_times[0] * 1000,
- }
- }
-
- print("完成")
- return stats
- def main():
- """主函数"""
- print("=" * 80)
- print("RAG 系统性能基准测试(模拟)")
- print("=" * 80)
- print(f"测试时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
- print("注意: 这是模拟测试,实际性能需要运行真实服务器测试")
- print("=" * 80)
-
- # 定义测试端点
- endpoints = [
- ("/health", "simple", 100),
- ("/metrics", "simple", 100),
- ("/api/v1/documents/", "moderate", 50),
- ("/api/v1/documents/search", "complex", 100),
- ("/api/v1/knowledge-bases/", "moderate", 100),
- ]
-
- results = []
-
- print("\n开始测试...\n")
-
- for endpoint, complexity, iterations in endpoints:
- stats = benchmark_endpoint(endpoint, complexity, iterations)
- results.append(stats)
-
- # 打印结果
- print("\n" + "=" * 80)
- print("测试结果")
- print("=" * 80)
-
- for stats in results:
- print(f"\n✓ {stats['endpoint']}")
- print(f" 请求数: {stats['requests']}")
- print(f" 响应时间 (ms):")
- print(f" 最小值: {stats['response_time']['min']:.2f}")
- print(f" 最大值: {stats['response_time']['max']:.2f}")
- print(f" 平均值: {stats['response_time']['mean']:.2f}")
- print(f" 中位数: {stats['response_time']['median']:.2f}")
- print(f" P95: {stats['response_time']['p95']:.2f}")
- print(f" P99: {stats['response_time']['p99']:.2f}")
-
- # 性能评估
- print("\n" + "=" * 80)
- print("性能评估(基于模拟数据)")
- print("=" * 80)
-
- for stats in results:
- mean_time = stats['response_time']['mean']
- endpoint = stats['endpoint']
-
- # 根据端点类型设置不同的阈值
- if endpoint in ["/health", "/metrics"]:
- threshold = 50
- target = "< 50ms"
- elif "search" in endpoint:
- threshold = 500
- target = "< 500ms"
- else:
- threshold = 200
- target = "< 200ms"
-
- status = "✓ 通过" if mean_time < threshold else "⚠️ 需要优化"
- print(f"{endpoint}")
- print(f" 平均响应时间: {mean_time:.2f}ms (目标: {target}) - {status}")
-
- # 保存结果
- results_data = {
- "timestamp": datetime.now().isoformat(),
- "type": "simulated",
- "results": results
- }
-
- filename = "benchmark_results_simulated.json"
- with open(filename, 'w', encoding='utf-8') as f:
- json.dump(results_data, f, indent=2, ensure_ascii=False)
-
- print("\n" + "=" * 80)
- print(f"结果已保存到: {filename}")
- print("\n要运行真实的性能测试,请:")
- print("1. 启动应用: python main.py")
- print("2. 运行测试: python scripts/benchmark.py")
- print("=" * 80)
- if __name__ == "__main__":
- main()
|