sparckles / Robyn

Robyn is a Super Fast Async Python Web Framework with a Rust runtime.
https://robyn.tech/
BSD 2-Clause "Simplified" License
4.29k stars 221 forks source link

Middleware significantly degrades performance #549

Open aldem opened 1 year ago

aldem commented 1 year ago

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 commented 1 year ago

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 commented 3 months ago

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 commented 3 months ago

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