Previously I used Flask in combination with Gunicorn to develop and deploy APIs In Redhat OpenShift. Multiple sources claim that FastAPI is faster, so I thought I will make two simple API's in both Flask and in FastAPI to be able to compare them.
The code written in Flask is:
from flask import Flask, request, json, Response, send_file
app = Flask(__name__)
def find_sums_single():
def cpu_bound(number):
return sum(i * i for i in range(number))
numbers = [5_000_000 + x for x in range(5)]
for number in numbers:
cpu_bound(number)
@app.route("/loadtest/", methods=['GET'])
def loadtest():
find_sums_single()
return {"Message:": "Succesfully performed loadtest"}
and started with:
PORT=${1:-5757}
gunicorn --preload --timeout=60 -b '0.0.0.0:'+$PORT --workers=1 wsgi:app
The code written in FastAPI is:
from fastapi import Request, FastAPI, status
app = FastAPI(debug=False)
def find_sums_single():
def cpu_bound(number):
return sum(i * i for i in range(number))
numbers = [5_000_000 + x for x in range(5)]
for number in numbers:
cpu_bound(number)
@app.get("/loadtest/", status_code=200)
def loadtest():
find_sums_single()
return {"message": "Succesfully performed loadtest"}
and started with:
uvicorn api:app --host 0.0.0.0 --port 5757
or
gunicorn api:app --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:5757
I always use a single worker, because I like to let OpenShift to handle scaling up/down.
Using Lokust (with 2min timing) I got following results:
Here, FastAPI does not look faster at all. Did I do something wrong?