Skip to content

FastAPI performance hit? #503

@mavwolverine

Description

@mavwolverine

core running on docker

Created 2 hello world FastAPI apps, one vanilla, other with supertokens.

from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware

from supertokens_python import (
    InputAppInfo,
    SupertokensConfig,
    init,
)
from supertokens_python.recipe import (
    dashboard,
    emailverification,
    multitenancy,
    session,
    usermetadata,
    userroles,
    emailpassword,
)
from supertokens_python import (
    get_all_cors_headers,
)
from supertokens_python.framework.fastapi import get_middleware


def create_app():
    app = FastAPI()
    app.add_middleware(
        CORSMiddleware,
        allow_origins=[
            "http://localhost:3000",
            "http://localhost:8000",
        ],
        allow_credentials=True,
        allow_methods=["GET", "PUT", "POST", "DELETE", "OPTIONS", "PATCH"],
    )

    @app.get("/")
    def read_root():
        return {"Hello": "World"}

    # TODO: start server

    return app


def create_app_supertokens():
    init(
        app_info=InputAppInfo(
            app_name="SuperTokens test",
            api_domain="http://localhost:8000/",
            website_domain="http://localhost:3000/",
            api_base_path="/auth",
            website_base_path="/auth",
        ),
        supertokens_config=SupertokensConfig(
            # https://try.supertokens.com is for demo purposes. Replace this with the
            # address of your core instance (sign up on supertokens.com), or self host
            # a core.
            # connection_uri="https://try.supertokens.com",
            connection_uri="http://localhost:3567",
            # api_key=<API_KEY(if configured)>
        ),
        framework="fastapi",
        recipe_list=[
            emailpassword.init(),
            session.init(),  # initializes session features
            dashboard.init(),
            emailverification.init(mode="REQUIRED"),
            userroles.init(),
            usermetadata.init(),
            multitenancy.init(),
        ],
        mode="asgi",  # use wsgi if you are running using gunicorn
    )

    app = FastAPI()
    app.add_middleware(get_middleware())

    app.add_middleware(
        CORSMiddleware,
        allow_origins=[
            "http://localhost:3000",
            "http://localhost:8000",
        ],
        allow_credentials=True,
        allow_methods=["GET", "PUT", "POST", "DELETE", "OPTIONS", "PATCH"],
        allow_headers=["Content-Type"] + get_all_cors_headers(),
    )

    @app.get("/")
    def read_root():
        return {"Hello": "World"}

    # TODO: start server

    return app

benchmark for vanilla app:

❯ wrk -t12 -c400 -d30s http://localhost:8008
Running 30s test @ http://localhost:8008
  12 threads and 400 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    36.99ms    7.54ms 210.71ms   80.16%
    Req/Sec     0.90k    69.06     1.05k    67.42%
  321465 requests in 30.07s, 43.53MB read
  Socket errors: connect 0, read 1092, write 12, timeout 0
Requests/sec:  10690.42
Transfer/sec:      1.45MB

benchmark with supertokens middleware

❯ wrk -t12 -c400 -d30s http://localhost:8009
Running 30s test @ http://localhost:8009
  12 threads and 400 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    86.59ms   20.83ms 499.97ms   94.85%
    Req/Sec   382.60     45.98   565.00     75.47%
  137462 requests in 30.08s, 18.62MB read
  Socket errors: connect 0, read 1251, write 1, timeout 0
Requests/sec:   4569.42
Transfer/sec:    633.70KB

Is this big difference expected?

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions