-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Description
First Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn't find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google "How to X in FastAPI" and didn't find any information.
- I already read and followed all the tutorial in the docs and didn't find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
from datetime import datetime
from typing import List, Optional
from fastapi import APIRouter, Depends, HTTPException
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
from sqlalchemy.orm import Session
from sqlalchemy import exc
from commons import schemas
from commons.dependencies import get_db
from services.events import crud
router = APIRouter(
tags=['Events']
)
@router.get('/v1/passages')
def get_passages(search_start_datetime: datetime,
search_end_datetime: datetime,
offset: int,
limit: int,
db: Session = Depends(get_db)):
start = datetime.now()
passages = crud.get_passages(db, search_start_datetime, search_end_datetime, offset, limit)
print('CRUD took: ', datetime.now() - start)
return passages
***
CRUD
***
def get_passages(
db: Session,
search_start_datetime: datetime,
search_end_datetime: datetime,
offset: int,
limit: int
):
passages = _get_common_passages_query(db) \
.filter((models.Passage.event_datetime >= search_start_datetime) & (models.Passage.event_datetime <= search_end_datetime)) \
.order_by(models.Passage.event_datetime) \
.offset(offset) \
.limit(limit) \
.all()
return _build_passages(passages) # auxiliary fn where I build Pydantic objects from ORMDescription
Hello everyone!
At the moment, for historical reasons, I am rewriting the project from Rust to Python (FastAPI, obviously). The project is an API layer in front of the PostgreSQL database. Unfortunately, I gain significantly lower performance than on Rust (in some endpoints 30-40 times slower), but I understand that it is incorrect to compare a framework and another language. So in the process of optimization, I first of all decided that performance issues lies in the CRUD operations on my part. But in the process of researching, I came to the following results (I will provide them with respect to certain input parameters):
- Input: about 10,000 objects from ORM when using the code above and Postman for API requests, testing locally on one PC
- About 3 seconds for an application in a Docker container
- About 2 seconds without a container
- About 1 second of the time above is taken by my CRUD manipulations and creating Pydantic objects (for some reason I have to create them manually). So yes, there is many things that I can improve
By the way, I also found out from other issues that even when the already created Pydantic objects are returned from router, the validation occurs again (so I removed the response_model param from the router)
Based on this, I understand that I can optimize my work with CRUD and Pydantic (I also use the orjson library to speed up serialization, but do not see any significant difference with the default one), but I do not understand why a one second of the request time added as overhead from the Docker container
It is also not quite obvious to me why FastAPI itself takes another second of the request time. In my opinion, this is subjectively too much, given that Pydantic objects are return from router without validation, just serialization with orjson
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.68.1
Python Version
Python 3.8.10
Additional Context
No response