LinkUp initial import
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
"""多 worker 多接口压测:python tools/bench.py [并发数] [每场景秒数]"""
|
||||
import asyncio, json, sys, time
|
||||
import aiohttp
|
||||
|
||||
BASE = "http://127.0.0.1:34173"
|
||||
CONC = int(sys.argv[1]) if len(sys.argv) > 1 else 100
|
||||
DURATION = float(sys.argv[2]) if len(sys.argv) > 2 else 10
|
||||
|
||||
SCENARIOS = [
|
||||
("任务·公开 /api/tasks", "/api/tasks", False),
|
||||
("任务·登录态 /api/tasks", "/api/tasks", True),
|
||||
("会话 /api/auth/me", "/api/auth/me", True),
|
||||
("通知 /api/notifications", "/api/notifications", True),
|
||||
("积分流水 /api/points/entries", "/api/points/entries", True),
|
||||
("信用详情 /api/credit/summary", "/api/credit/summary", True),
|
||||
]
|
||||
|
||||
TOKEN = None
|
||||
|
||||
|
||||
async def login(session):
|
||||
global TOKEN
|
||||
if TOKEN:
|
||||
return {"Authorization": f"Bearer {TOKEN}"}
|
||||
async with session.post(f"{BASE}/api/auth/login",
|
||||
json={"email": "localuser@linkup.test", "password": "LinkUp2026!"}) as r:
|
||||
data = await r.json()
|
||||
assert data.get("ok"), data
|
||||
TOKEN = data["token"]
|
||||
return {"Authorization": f"Bearer {TOKEN}"}
|
||||
|
||||
|
||||
async def worker(session, path, headers, stop_at, latencies, errors):
|
||||
while time.monotonic() < stop_at:
|
||||
t0 = time.perf_counter()
|
||||
try:
|
||||
async with session.get(f"{BASE}{path}", headers=headers) as r:
|
||||
await r.read()
|
||||
if r.status != 200:
|
||||
errors[0] += 1
|
||||
except Exception:
|
||||
errors[0] += 1
|
||||
continue
|
||||
latencies.append((time.perf_counter() - t0) * 1000)
|
||||
|
||||
|
||||
async def run_scenario(session, label, path, authed):
|
||||
headers = await login(session) if authed else {}
|
||||
latencies, errors = [], [0]
|
||||
stop_at = time.monotonic() + DURATION
|
||||
await asyncio.gather(*[worker(session, path, headers, stop_at, latencies, errors) for _ in range(CONC)])
|
||||
latencies.sort()
|
||||
total = len(latencies) + errors[0]
|
||||
rps = total / DURATION
|
||||
p = lambda q: latencies[min(len(latencies) - 1, int(len(latencies) * q))]
|
||||
print(f"{label:<30} {rps:>7.0f} RPS P50 {p(0.5):>6.0f}ms P95 {p(0.95):>6.0f}ms 失败 {errors[0]}")
|
||||
|
||||
|
||||
async def main():
|
||||
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(limit=0),
|
||||
timeout=aiohttp.ClientTimeout(total=30)) as session:
|
||||
for label, path, authed in SCENARIOS:
|
||||
await run_scenario(session, label, path, authed)
|
||||
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user