测试并发性能
()
| 97 | } |
| 98 | |
| 99 | def test_concurrent_performance(): |
| 100 | """测试并发性能""" |
| 101 | # 动态获取可用源 |
| 102 | test_sources = get_available_sources() |
| 103 | |
| 104 | if not test_sources: |
| 105 | print("❌ 没有找到可用的测试源") |
| 106 | return |
| 107 | |
| 108 | print("🚀 开始API密钥管理器性能测试") |
| 109 | print(f"📊 配置: {CONCURRENT_REQUESTS}并发, 总共{TOTAL_REQUESTS}次请求") |
| 110 | print(f"🎯 可用源: {', '.join(test_sources)}") |
| 111 | print("-" * 60) |
| 112 | |
| 113 | # 生成测试任务 |
| 114 | tasks = [] |
| 115 | for i in range(TOTAL_REQUESTS): |
| 116 | source_name = test_sources[i % len(test_sources)] |
| 117 | tasks.append((source_name, i)) |
| 118 | |
| 119 | # 并发执行 |
| 120 | start_time = time.time() |
| 121 | results = [] |
| 122 | |
| 123 | with ThreadPoolExecutor(max_workers=CONCURRENT_REQUESTS) as executor: |
| 124 | future_to_task = { |
| 125 | executor.submit(test_get_api_key, source_name, request_id): (source_name, request_id) |
| 126 | for source_name, request_id in tasks |
| 127 | } |
| 128 | |
| 129 | for future in as_completed(future_to_task): |
| 130 | result = future.result() |
| 131 | results.append(result) |
| 132 | |
| 133 | # 实时显示进度 |
| 134 | if len(results) % 10 == 0: |
| 135 | print(f"⏳ 已完成: {len(results)}/{TOTAL_REQUESTS}") |
| 136 | |
| 137 | end_time = time.time() |
| 138 | total_time = end_time - start_time |
| 139 | |
| 140 | # 分析结果 |
| 141 | analyze_results(results, total_time) |
| 142 | |
| 143 | def analyze_results(results: list, total_time: float): |
| 144 | """分析测试结果""" |
no test coverage detected