Skip to content

Instantly share code, notes, and snippets.

@gaganpreet
Last active March 23, 2022 13:15
Show Gist options
  • Save gaganpreet/ef3a3615db834c64d1a2f7681fbbfdb0 to your computer and use it in GitHub Desktop.
Save gaganpreet/ef3a3615db834c64d1a2f7681fbbfdb0 to your computer and use it in GitHub Desktop.
WSGIMiddleware overhead
from a2wsgi import WSGIMiddleware
from fastapi import FastAPI
from fastapi.responses import ORJSONResponse, UJSONResponse
from flask import Flask
from sqlalchemy.pool import StaticPool
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
flask_app = Flask(__name__)
app = FastAPI()
DATABASE_URL = "sqlite://"
db = create_engine(DATABASE_URL, connect_args={"check_same_thread": False}, poolclass=StaticPool)
Session = sessionmaker(bind=db)
Base = declarative_base()
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
name = Column(String, nullable=False, unique=True)
def get_users():
session = Session()
users = session.query(User).all()
return {"users": [{"name": user.name} for user in users]}
@flask_app.route("/users_flask")
def users_flask():
return get_users()
@app.get("/users_fastapi", response_class=ORJSONResponse)
async def users_fastapi():
return get_users()
def init_db():
Base.metadata.create_all(db)
session = Session()
for i in range(5000):
session.execute(
User.__table__.insert(),
[
{"name": "user{}".format(i)},
],
)
session.commit()
app.mount("/", WSGIMiddleware(flask_app))
init_db()

uvicorn

uvicorn main:app --port 8000 --no-access-log  --workers 1
$ wrk http://127.0.0.1:8000/users_fastapi

Running 10s test @ http://127.0.0.1:8000/users_fastapi
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   568.68ms   68.02ms 637.11ms   95.32%
    Req/Sec    12.06      7.11    20.00     36.21%
  171 requests in 10.01s, 16.15MB read
Requests/sec:     17.08
Transfer/sec:      1.61MB
$ wrk http://127.0.0.1:8000/users_flask

Running 10s test @ http://127.0.0.1:8000/users_flask
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.24s   157.89ms   1.56s    77.03%
    Req/Sec     7.58      5.52    20.00     64.06%
  74 requests in 10.01s, 6.99MB read
Requests/sec:      7.39
Transfer/sec:    714.88KB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment