Skip to content

can't reproduce benchmark, get only 10% of RPS using bench/socketify_plaintext.py #215

@raceychan

Description

@raceychan

Describe the bug
A clear and concise description of what the bug is.

To Reproduce
Steps to reproduce the behavior:

  1. copy socketify_plaintext.py file from the main branch
  2. uv add socketify
  3. uv run python sock.py

Expected behavior
on benchmark it shows that even the slowest way to run socketify through python3, should reach 500K RPS, however, I only get 73K RPS.

import os

from socketify import App

def run_app():
    app = App(request_response_factory_max_items=200_000)
    router = app.router()

    @router.get("/")
    async def home(res, req):
        res.send(b"Hello, World!")

    app.listen(
        8000,
        lambda config: print(
            "PID %d Listening on port http://localhost:%d now\n"
            % (os.getpid(), config.port)
        ),
    )
    app.run()


def create_fork():
    n = os.fork()
    # n greater than 0 means parent process
    if not n > 0:
        run_app()


# fork limiting the cpu count - 1
# for i in range(1, multiprocessing.cpu_count()):
#     create_fork()
if __name__ == "__main__":
    run_app()  # run app on the main process too :)
wrk -t4 -c64 http://localhost:8000

Running 10s test @ http://localhost:8000
  4 threads and 64 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     0.87ms  221.20us  11.43ms   96.59%
    Req/Sec    18.56k     1.36k   32.00k    79.75%
  738552 requests in 10.06s, 97.90MB read
Requests/sec:  73406.05
Transfer/sec:      9.73MB

compared to a naive asgi

from starlette.responses import Response

async def naive(scope, receive, send):
    content =  "hello, world".encode()
    await Response(content)(scope, receive, send)
wrk -t4 -c64 http://localhost:8000
Running 10s test @ http://localhost:8000
  4 threads and 64 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     0.91ms  840.99us  35.45ms   99.39%
    Req/Sec    18.10k     0.95k   29.63k    93.03%
  724043 requests in 10.10s, 35.22MB read
Requests/sec:  71691.43
Transfer/sec:      3.49MB

Desktop (please complete the following information):
Distributor ID: Ubuntu
Description: Ubuntu 20.04.6 LTS
Release: 20.04
Codename: focal

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions