-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
90 lines (65 loc) · 2.42 KB
/
Copy pathbenchmark.py
File metadata and controls
90 lines (65 loc) · 2.42 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
"""Quick performance benchmark for ylmz vs raw ASGI."""
import time
import asyncio
from ylmz import Ylmz, BaseModel
app = Ylmz()
class Item(BaseModel):
name: str
price: float
in_stock: bool = True
@app.get("/")
async def root():
return {"hello": "world"}
@app.get("/items/{item_id:int}")
async def get_item(item_id: int):
return {"id": item_id}
@app.get("/search")
async def search(q: str = ""):
return {"q": q}
@app.post("/items")
async def create_item(item: Item):
return {"created": item.model_dump()}
# Setup 100 routes to test trie scaling
for i in range(100):
@app.get(f"/api/v{i}")
async def handler():
return {"v": "ok"}
async def run_benchmark():
import httpx
transport = httpx.ASGITransport(app=app)
async with httpx.AsyncClient(transport=transport, base_url="http://test") as client:
# Warmup
for _ in range(50):
await client.get("/")
# Benchmark: simple route
n = 2000
start = time.perf_counter()
for _ in range(n):
await client.get("/")
elapsed = time.perf_counter() - start
print(f"GET / : {n/elapsed:.0f} req/s ({elapsed*1000/n:.2f} ms/req)")
# Benchmark: path param
start = time.perf_counter()
for _ in range(n):
await client.get("/items/42")
elapsed = time.perf_counter() - start
print(f"GET /items/42 : {n/elapsed:.0f} req/s ({elapsed*1000/n:.2f} ms/req)")
# Benchmark: query param
start = time.perf_counter()
for _ in range(n):
await client.get("/search?q=hello")
elapsed = time.perf_counter() - start
print(f"GET /search?q=hello : {n/elapsed:.0f} req/s ({elapsed*1000/n:.2f} ms/req)")
# Benchmark: POST with body
start = time.perf_counter()
for _ in range(n // 2):
await client.post("/items", json={"name": "test", "price": 9.99})
elapsed = time.perf_counter() - start
print(f"POST /items (with model) : {(n//2)/elapsed:.0f} req/s ({elapsed*1000/(n//2):.2f} ms/req)")
# Benchmark: deep route (trie perf test)
start = time.perf_counter()
for _ in range(n):
await client.get("/api/v50")
elapsed = time.perf_counter() - start
print(f"GET /api/v50 (100 routes) : {n/elapsed:.0f} req/s ({elapsed*1000/n:.2f} ms/req)")
asyncio.run(run_benchmark())