Repository navigation
Expand file tree
/
Copy pathtest.py
More file actions
127 lines (90 loc) · 2.48 KB
/
Copy pathtest.py
File metadata and controls
127 lines (90 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import asyncio
import aiohttp
import time
import random
URL = "http://172.22.5.107:30041/silver/v1/chat/completions"
API_KEY = "sk-NVl2tuQtGeHO86QixxOlWdXKukHKYbAhKUKmxeOUBosKVMuG"
CONCURRENCY = 50 # 并发数
TOTAL_REQUESTS = 200 # 总请求数
async def request(session, index):
payload = {
"model": "DeepSeek V4 Pro",
"messages": [
{
"role": "user",
"content": f"""
这是第 {index} 次测试请求。
随机编号:
{random.randint(100000,999999)}
请简单回答:
你是谁?
"""
}
],
"stream": False,
"enable_thinking": True,
"enable_search": True
}
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
start = time.time()
try:
async with session.post(
URL,
json=payload,
headers=headers,
timeout=120
) as resp:
text = await resp.text()
cost = time.time() - start
print(
f"[{index}] "
f"status={resp.status} "
f"time={cost:.2f}s "
f"len={len(text)}"
)
return {
"ok": resp.status == 200,
"time": cost
}
except Exception as e:
cost = time.time() - start
print(
f"[{index}] ERROR {e} "
f"time={cost:.2f}s"
)
return {
"ok": False,
"time": cost
}
async def worker():
sem = asyncio.Semaphore(CONCURRENCY)
async with aiohttp.ClientSession() as session:
async def run(i):
async with sem:
return await request(session, i)
tasks = [
run(i)
for i in range(TOTAL_REQUESTS)
]
return await asyncio.gather(*tasks)
if __name__ == "__main__":
start = time.time()
results = asyncio.run(worker())
total = time.time()-start
success = sum(
1 for r in results
if r["ok"]
)
avg = sum(
r["time"]
for r in results
)/len(results)
print("\n==========")
print("总请求:", TOTAL_REQUESTS)
print("成功:", success)
print("失败:", TOTAL_REQUESTS-success)
print("总耗时:", round(total, 2), "秒")
print("平均响应:", round(avg, 2), "秒")