Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve fastapi perfs #391

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions lessons/232/python-app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import orjson
from asyncpg import PostgresError
from fastapi import FastAPI, HTTPException
from fastapi.responses import ORJSONResponse, PlainTextResponse
from fastapi.responses import ORJSONResponse, PlainTextResponse, Response
from prometheus_client import make_asgi_app
from pydantic import BaseModel

Expand All @@ -25,12 +25,12 @@


@app.get("/healthz", response_class=PlainTextResponse)
def health():
return "OK"
async def health():
return PlainTextResponse("OK")


@app.get("/api/devices", response_class=ORJSONResponse)
def get_devices():
async def get_devices():
devices = (
{
"id": 1,
Expand Down Expand Up @@ -58,15 +58,15 @@ def get_devices():
},
)

return devices
return ORJSONResponse(devices)


class DeviceRequest(BaseModel):
mac: str
firmware: str


@app.post("/api/devices", status_code=201, response_class=ORJSONResponse)
@app.post("/api/devices", status_code=201, response_class=Response)
async def create_device(
device: DeviceRequest, conn: PostgresDep, cache_client: MemcachedDep
):
Expand Down Expand Up @@ -102,18 +102,20 @@ async def create_device(
"updated_at": now,
}

device_json = orjson.dumps(device_dict)

# Measure cache operation
start_time = time.perf_counter()

await cache_client.set(
device_uuid.hex.encode(),
orjson.dumps(device_dict),
device_json,
exptime=20,
)

H.labels(op="set", db="memcache").observe(time.perf_counter() - start_time)

return device_dict
return Response(device_json, media_type="application/json")

except PostgresError:
logger.exception("Postgres error")
Expand Down Expand Up @@ -142,14 +144,14 @@ async def get_device_stats(cache_client: MemcachedDep):
stats = await cache_client.stats()
# H.labels(op="stats", db="memcache").observe(time.perf_counter() - start_time)

return {
return ORJSONResponse({
"curr_items": stats.get(b"curr_items", 0),
"total_items": stats.get(b"total_items", 0),
"bytes": stats.get(b"bytes", 0),
"curr_connections": stats.get(b"curr_connections", 0),
"get_hits": stats.get(b"get_hits", 0),
"get_misses": stats.get(b"get_misses", 0),
}
})
except aiomcache.exceptions.ClientException:
logger.exception("Memcached error")
raise HTTPException(
Expand Down