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

Middleware significantly degrades performance #549

Open
aldem opened this issue Jul 6, 2023 · 3 comments
Open

Middleware significantly degrades performance #549

aldem opened this issue Jul 6, 2023 · 3 comments

Comments

@aldem
Copy link

aldem commented Jul 6, 2023

Just tried robyn and in general I am impressed - it looks very promising, thank you! 👍

However, after first quick test for framework overhead (empty response) I noticed serious degradation in performance after adding an empty middleware.

My test code is dead simple:

from robyn import Robyn, Request, Response

app = Robyn(__file__)

@app.before_request()
async def middleware_1(request: Request):
    return request

@app.before_request()
async def middleware_2(request: Request):
    return request

@app.get("/")
async def root(request: Request):
    return Response(200, {}, "OK!\n")

app.start(port=8000, url="0.0.0.0", )

I run it on Python 3.11.3 with robyn 0.36.0 (default config, single process), and test with httpit -d60s -c8 'http://10.200.0.210:8000/' (8 concurrent connections) and results are as follows:

rps latency performance
no middleware ~23600 0.32 ms 100%
middlware 1 ~17500 0.46 ms 74% (-26%)
middleware 1&2 ~13700 0.58 ms 58% (-42%)

Both the server and the client are running on idling systems and network so other factors could be excluded, and results are consistent when tests are repeated.

What is interesting is that fastapi delivers similar results - rps are lower of course but empty middleware also introduces ~30% degradation, while blacksheep has almost no overhead.

Now the question is - what could be the possible reason for this degradation?

@sansyrox
Copy link
Member

sansyrox commented Jul 6, 2023

Hey @aldem 👋

Firstly, thank you for raising this issue! This is a very interesting observation and I was not aware of such a pattern. Thank you for pointing it out. 😄

We execute the middlewares serially but I will have a look at how Blacksheep handles the middlewares. Thanks again!

@sansyrox
Copy link
Member

Hey @aldem 👋

I was going through your issue and turns out we can chain middlewares in Robyn 😅 This is unintentional but I really wanted to implement this feature anyway. LOL

@sansyrox
Copy link
Member

Still need to find a proper way to speed it up though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants