4 · API JSON Endpoint (api_json)#
← All benchmarks · Related chapter: Chapter 18 · The Booking Desk API
Query 2,000 rows and return a verified UTF-8 JSON byte payload ready for an HTTP response. We measure both pgdorm's zero-hydration pool.json() (Rust encodes PostgreSQL binary wire tuples straight into UTF-8 JSON bytes) and pgdorm hydrating __slots__ models + Pydantic TypeAdapter.dump_json() so you can compare apples-to-apples hydration alongside the zero-hydration path.
Chart & metrics#
| ORM / Path | Instr (Client + PG) | Peak Py | RTTs | Server ms | ms (med) |
|---|---|---|---|---|---|
pgdorm (pool.json()) |
19.5M (6.3M + 13.3M) | 260.3 KiB | 1 | 1.58 ms | 2.67 ms |
pgdorm (pool.json(mode="server")) |
32.4M (941k + 31.5M) | 268.1 KiB | 1 | 3.30 ms | 4.59 ms |
pgdorm (pool.all() + Pydantic) |
69.2M (57.0M + 12.2M) | 978.9 KiB | 1 | 2.62 ms | 7.55 ms |
| SQLDataclass | 91.1M (74.2M + 16.8M) | 941.7 KiB | 3 | 1.21 ms | 7.33 ms |
| Django ORM | 97.0M (81.3M + 15.7M) | 1.78 MiB | 1 | 1.18 ms | 6.85 ms |
| Piccolo ORM | 108.1M (93.9M + 14.2M) | 1.73 MiB | 2 | 1.56 ms | 7.76 ms |
| Peewee | 112.8M (89.6M + 23.2M) | 1.56 MiB | 1 | 1.22 ms | 7.55 ms |
| Tortoise ORM | 124.5M (110.2M + 14.4M) | 1.71 MiB | 2 | 1.04 ms | 8.85 ms |
| SQLAlchemy 2 | 134.9M (120.9M + 13.9M) | 3.96 MiB | 3 | 2.46 ms | 12.76 ms |
| SQLModel | 160.0M (146.8M + 13.2M) | 4.42 MiB | 3 | 0.71 ms | 13.13 ms |
Ferris' basement: Skipping Python object allocation for read-only HTTP responses
Read-only REST endpoints spend most of their CPU hydrating ORM objects only to serialize them to JSON and immediately throw every Python object away. pool.json() keeps the exact same typed pg.select(...) query AST and binary wire decode, but writes UTF-8 JSON directly inside Rust (260.3 KiB peak Python heap).
Contender implementations#
Every tab below is AST-extracted directly from bench/adapters/ — zero hand-copied snippets.
pgdorm 0.10.0 (pgdorm-wire (Rust)) · 1 RTT · 6.3M instr (pool.json()) / 57.0M instr (OLTP+Pydantic) · pgdorm_adapter.py
_TA_EVENTS = TypeAdapter(list[BenchEvent])
async def api_json_events(self, limit: int) -> bytes:
assert self._pool is not None
return await self._pool.json(
pg.select(BenchEvent).order_by(BenchEvent.bench_event_id.asc()).limit(limit)
)
async def api_json_server(self, limit: int) -> bytes:
assert self._pool is not None
return await self._pool.json(
pg.select(BenchEvent).order_by(BenchEvent.bench_event_id.asc()).limit(limit),
mode=pg.enums.JsonMode.SERVER,
)
async def api_json_events_pydantic(self, limit: int) -> bytes:
assert self._pool is not None
rows = await self._pool.all(
pg.select(BenchEvent).order_by(BenchEvent.bench_event_id.asc()).limit(limit)
)
return _TA_EVENTS.dump_json(rows)
SQLAlchemy 2 2.1.9 (asyncpg) · 3 RTTs · 120.9M instr · 12.76 ms · sqlalchemy_adapter.py
SQLModel 0.0.50 (asyncpg) · 3 RTTs · 146.8M instr · 13.13 ms · sqlmodel_adapter.py
async def api_json_events(self, limit: int) -> bytes:
assert self._session_factory is not None
async with self._session_factory() as s:
rows = (
await s.exec(select(_SMEvent).order_by(col(_SMEvent.bench_event_id)).limit(limit))
).all()
return _TA_SM_EVENTS.dump_json(rows)
SQLDataclass 0.4.3 (psycopg3 (sync)) · 3 RTTs · 74.2M instr · 7.33 ms · sqldataclass_adapter.py
async def api_json_events(self, limit: int) -> bytes:
assert self._engine is not None
with self._engine.connect() as conn:
rows = load_all(
conn,
select(SdEvent.__table__).order_by(SdEvent.__table__.c.bench_event_id).limit(limit),
SdEvent,
)
return _EVENT_LIST_ADAPTER.dump_json(rows)
Django ORM 6.1.3 (psycopg3) · 1 RTT · 81.3M instr · 6.85 ms · django_adapter.py
async def api_json_events(self, limit: int) -> bytes:
assert self._Event is not None
rows = list(
self._Event.objects.order_by("bench_event_id").values(
"bench_event_id", "category", "code", "amount", "active", "recorded_at"
)[:limit]
)
return json.dumps(rows, default=json_default).encode("utf-8")
Tortoise ORM 1.1.9 (asyncpg) · 2 RTTs · 110.2M instr · 8.85 ms · tortoise_adapter.py
async def api_json_events(self, limit: int) -> bytes:
rows = (
await _TEvent.all()
.order_by("bench_event_id")
.limit(limit)
.values("bench_event_id", "category", "code", "amount", "active", "recorded_at")
)
return json.dumps(rows, default=json_default).encode("utf-8")
Piccolo ORM 1.39.0 (asyncpg) · 2 RTTs · 93.9M instr · 7.76 ms · piccolo_adapter.py
async def api_json_events(self, limit: int) -> bytes:
rows = (
await self._Event.select(
self._Event.bench_event_id,
self._Event.category,
self._Event.code,
self._Event.amount,
self._Event.active,
self._Event.recorded_at,
)
.order_by(self._Event.bench_event_id)
.limit(limit)
)
return json.dumps(rows, default=json_default).encode("utf-8")
Peewee 4.5.3 (psycopg3) · 1 RTT · 89.6M instr · 7.55 ms · peewee_adapter.py