|
|
@ -1,4 +1,5 @@ |
|
|
import time |
|
|
import time |
|
|
|
|
|
|
|
|
from fastapi import APIRouter, FastAPI |
|
|
from fastapi import APIRouter, FastAPI |
|
|
|
|
|
|
|
|
# Setup Router |
|
|
# Setup Router |
|
|
@ -7,27 +8,35 @@ router = APIRouter() |
|
|
|
|
|
|
|
|
# 50 Static Routes |
|
|
# 50 Static Routes |
|
|
for i in range(50): |
|
|
for i in range(50): |
|
|
|
|
|
|
|
|
@router.get(f"/static-{i}") |
|
|
@router.get(f"/static-{i}") |
|
|
def static_route(i=i): |
|
|
def static_route(i=i): |
|
|
return {"route": f"static-{i}"} |
|
|
return {"route": f"static-{i}"} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 50 Dynamic Routes |
|
|
# 50 Dynamic Routes |
|
|
for i in range(50): |
|
|
for i in range(50): |
|
|
|
|
|
|
|
|
@router.get(f"/dynamic-{i}/{{item_id}}") |
|
|
@router.get(f"/dynamic-{i}/{{item_id}}") |
|
|
def dynamic_route(item_id: int, i=i): |
|
|
def dynamic_route(item_id: int, i=i): |
|
|
return {"route": f"dynamic-{i}", "item_id": item_id} |
|
|
return {"route": f"dynamic-{i}", "item_id": item_id} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.include_router(router) |
|
|
app.include_router(router) |
|
|
import anyio |
|
|
|
|
|
from contextlib import AsyncExitStack |
|
|
from contextlib import AsyncExitStack |
|
|
|
|
|
|
|
|
|
|
|
import anyio |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Mock ASGI stack |
|
|
# Mock ASGI stack |
|
|
async def mock_receive(): |
|
|
async def mock_receive(): |
|
|
return {"type": "http.request", "body": b"", "more_body": False} |
|
|
return {"type": "http.request", "body": b"", "more_body": False} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def mock_send(message): |
|
|
async def mock_send(message): |
|
|
pass |
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
scope_static = { |
|
|
scope_static = { |
|
|
"type": "http", |
|
|
"type": "http", |
|
|
"method": "GET", |
|
|
"method": "GET", |
|
|
@ -54,6 +63,7 @@ scope_dynamic = { |
|
|
"fastapi_function_astack": AsyncExitStack(), |
|
|
"fastapi_function_astack": AsyncExitStack(), |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def run_benchmark(): |
|
|
async def run_benchmark(): |
|
|
# Warmup |
|
|
# Warmup |
|
|
for _ in range(100): |
|
|
for _ in range(100): |
|
|
@ -82,7 +92,7 @@ async def run_benchmark(): |
|
|
print(f"Speedup: {cache_disabled_time / cache_enabled_time:.2f}x") |
|
|
print(f"Speedup: {cache_disabled_time / cache_enabled_time:.2f}x") |
|
|
|
|
|
|
|
|
print("\nRunning Dynamic Route Benchmark (10,000 iterations)...") |
|
|
print("\nRunning Dynamic Route Benchmark (10,000 iterations)...") |
|
|
|
|
|
|
|
|
# Caching has no effect on dynamic routes |
|
|
# Caching has no effect on dynamic routes |
|
|
start_time = time.perf_counter() |
|
|
start_time = time.perf_counter() |
|
|
for _ in range(10000): |
|
|
for _ in range(10000): |
|
|
@ -90,5 +100,6 @@ async def run_benchmark(): |
|
|
dynamic_time = time.perf_counter() - start_time |
|
|
dynamic_time = time.perf_counter() - start_time |
|
|
print(f"Dynamic Route time: {dynamic_time:.4f} seconds") |
|
|
print(f"Dynamic Route time: {dynamic_time:.4f} seconds") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
if __name__ == "__main__": |
|
|
anyio.run(run_benchmark) |
|
|
anyio.run(run_benchmark) |
|
|
|