DEV Community

Ciro Spaciari
Ciro Spaciari

Posted on • Edited on

This is how i got more performance in WebSockets than Node or Bun using Python

Bun is changing out the scenario for Node.js, with much more performance in HTTP and WebSockets, but how FAST it is compared with Python?

In the Bun website https://bun.sh/, the source code of the WebSocket benchmark is available, so... let's try to run Falcon + uvicorn on it!

Benchmark

I just simplified a little the benchmark, to remove a string concatenation, to measure more of the raw throughput with less impact other things, and runned using Falcon + uvicorn and socketify.py, when benchmarking i was talking with Vytautas Liuolia in the Falcon Gitter for tunning HTTP performance in other raw throughtput testing as well, and Falcon is not so much focused on WS performance, this also depends a lot of ASGI server too. For this test i choose uvicorn with gunicorn as ASGI server.

Messages per second with Linux x64:

image

Runtime versions: PyPy3 7.3.9, Python 3.10.7, node v16.17.0, bun v0.2.2

Framework versions: gunicorn 20.1.0 + uvicorn 0.19.0, socketify alpha, gunicorn 20.1.0 + falcon 3.1.0, robyn 0.18.3

WebSocket tested with Bun.sh bench chat-client check the source code in this link

Machine OS: Debian GNU/Linux bookworm/sid x86_64 Kernel: 6.0.0-2-amd64 CPU: Intel i7-7700HQ (8) @ 3.800GHz Memory: 32066MiB

Socketify.py got almost 900k messages/s with PyPy3 and 860k with Python3 the same performance as Bun,
with Falcon 35k messages/s and with Falcon with PyPy3 56k messages/s, node.js manages 192k.

Bun is REALLY fast! But why the performance of Bun is almost identicall to socketify.py? The answer is that it also uses uWebSockets as base, actually the same code of the PR i made to the C API is been used in Bun with some tweaks, and it endup with almost the same performance that socketify.py, showing that both have some DNA in common.

Follow @cirospaciari on twitter for more updates about socketify.py and if you have some time check out the github https://github.com/cirospaciari/socketify.py this project can be even better with some community love ;)

Top comments (0)