question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

application performance with response_model

See original GitHub issue

Issue

I found that when use response_model, application performance dropped dramatically.

no response_model is about 4 times than having response_model in efficiency

How should I solve this problem?

Is my way of using it incorrect?

relative information

py-version: python 3.7

FastAPI-version: lastest

screenshot:

no response_model

image

with response_model image

model code

class ResponseSchema(BaseModel):
    code: int
    message: str

# config skip_defaults: True
# NovelSchema field count about 10
# ChapterSchema field count about 7
class BookCatalogData(BaseSchema):
    novel: NovelSchema = SchemaValue(None)
    chapters: List[ChapterSchema] = SchemaValue(None)

class BookCatalogRsm(ResponseSchema):
    data: BookCatalogData

controller code

novel field contain 6 field

chapters field contain about 700 items

@app.get(
    '/v1/api/book/catalog',
    response_model=BookCatalogRsm,
    tags=[TAG]
)
async def book_catalog(novelId: int = -1):
    chapters = await services.chapter.get_catalog(novelId)
    novel = await services.novel.get_by_id(
        novelId,
        columns=[services.novel.table.c.id, services.novel.table.c.name]
    )
    return return_ok({
        'novel': novel,
        'chapters': chapters
    })

helping

Thank you for your help.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
dmontagucommented, Jan 22, 2020

Yeah, using a response model currently adds a lot of overhead in order to make sure extra fields don’t get serialized. The fix will be to more carefully walk the structure so that fields don’t have to be reparsed.

Fixing this is my top fastapi priority I just haven’t been able to find time to focus on it, but I’m hoping to soon.

1reaction
euri10commented, May 10, 2019

out of curiosity what was the reason ? for timing purposes you can use timing-asgi like this

from timing_asgi import TimingMiddleware, TimingClient
from timing_asgi.integrations import StarletteScopeToName

class PrintTimings(TimingClient):
    def timing(self, metric_name, timing, tags):
        logger.debug(f"{metric_name}, {timing}, {tags}")

app.add_middleware(
    TimingMiddleware,
    client=PrintTimings(),
    metric_namer=StarletteScopeToName(prefix="app", starlette_app=app)
)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Response Model - FastAPI
FastAPI framework, high performance, easy to learn, fast to code, ready for production.
Read more >
Application of the Graded Response Model to a Scale ... - NCBI
The analysis presented shows the importance of performing modeling with IRT, because it provides in depth information about the psychometric attributes of each ......
Read more >
A New LED Response Model and its Application to Pre ...
In this letter, we first propose a new LED response model taking the parasitic effects appearing at higher frequencies into account. The ...
Read more >
The Science of the TrainingPeaks Performance Manager
Dr. Andy Coggan explains the incredible science behind the Performance Manager ... Banister's impulse-response model: theory, applications, and limitations.
Read more >
Monitoring the Heart Rate Variability Responses to Training ...
Conclusion: The impulse-response model and data collected via a novel smartphone application can be used to model HRV responses to swimming ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found