forked from tanrax/python-api-frameworks-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlitestar_app.py
More file actions
96 lines (72 loc) · 2.6 KB
/
litestar_app.py
File metadata and controls
96 lines (72 loc) · 2.6 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
"""
Litestar Benchmark Application
4 endpoints:
1. GET /json-1k - Returns ~1KB JSON response
2. GET /json-10k - Returns ~10KB JSON response
3. GET /db - 10 reads from SQLite database
4. GET /slow - Mock API that takes 2 seconds to respond
"""
from __future__ import annotations
import asyncio
from typing import Any
from litestar import Litestar, get
from litestar.di import Provide
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
import test_data
from shared.models import Base, User, UserResponse
# Database setup
DATABASE_URL = "sqlite+aiosqlite:///./benchmark.db"
engine = create_async_engine(DATABASE_URL, echo=False)
async_session = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
async def get_db() -> AsyncSession:
"""Dependency to get database session."""
async with async_session() as session:
yield session
async def seed_database():
"""Seed database with 10 users if empty."""
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
async with async_session() as session:
result = await session.execute(select(User).limit(1))
if result.scalar_one_or_none() is None:
users = [
User(
username=f"user{i:02d}",
email=f"user{i:02d}@example.com",
first_name=f"First{i}",
last_name=f"Last{i}",
)
for i in range(10)
]
session.add_all(users)
await session.commit()
print("[litestar] Seeded 10 users")
async def on_startup():
"""Startup hook - seed database."""
await seed_database()
@get("/json-1k")
async def json_1k() -> list[dict[str, Any]]:
"""Return ~1KB JSON response."""
return test_data.JSON_1K
@get("/json-10k")
async def json_10k() -> list[dict[str, Any]]:
"""Return ~10KB JSON response."""
return test_data.JSON_10K
@get("/db", dependencies={"db": Provide(get_db)})
async def db_read(db: AsyncSession) -> list[UserResponse]:
"""Read 10 users from database."""
stmt = select(User).limit(10)
result = await db.execute(stmt)
users = result.scalars().all()
return [UserResponse.model_validate(u) for u in users]
@get("/slow")
async def slow() -> dict[str, Any]:
"""Mock slow API - 2 second delay."""
await asyncio.sleep(2)
return {"status": "ok", "delay_seconds": 2}
app = Litestar(
route_handlers=[json_1k, json_10k, db_read, slow],
on_startup=[on_startup],
openapi_config=None,
)