()
| 88 | |
| 89 | # ============ 主流程 ============ |
| 90 | async def run_load_test(): |
| 91 | prompts = await load_data() |
| 92 | queue = asyncio.Queue(maxsize=MAX_CONCURRENCY * 5) # 限制队列大小,降低内存占用 |
| 93 | counter = Counter() |
| 94 | latencies = [] |
| 95 | |
| 96 | connector = aiohttp.TCPConnector(limit=MAX_CONCURRENCY * 2) # 限制TCP连接 |
| 97 | timeout = aiohttp.ClientTimeout(total=TIMEOUT) |
| 98 | |
| 99 | async with aiohttp.ClientSession(connector=connector, timeout=timeout) as session: |
| 100 | with tqdm(total=TOTAL_REQUESTS, desc="压测进度") as pbar: |
| 101 | # 启动 Worker |
| 102 | workers = [ |
| 103 | asyncio.create_task(worker(f"W{i}", session, prompts, counter, latencies, pbar, queue)) |
| 104 | for i in range(MAX_CONCURRENCY) |
| 105 | ] |
| 106 | |
| 107 | # 边生产边消费 |
| 108 | for i in range(TOTAL_REQUESTS): |
| 109 | await queue.put(i) |
| 110 | |
| 111 | # 发送毒丸让 worker 退出 |
| 112 | for _ in workers: |
| 113 | await queue.put(None) |
| 114 | |
| 115 | await queue.join() |
| 116 | await asyncio.gather(*workers) |
| 117 | |
| 118 | generate_report(counter, latencies) |
| 119 | |
| 120 | |
| 121 | # ============ 报告输出 ============ |
no test coverage detected