FastAPI performs worse than Flask

Question:

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:

Lokust results

Here, FastAPI does not look faster at all. Did I do something wrong?

Asked By: Niels

||

Answers:

Well… What you are doing is mainly CPU bound work.

A web application will usually be more I/O bound, and then compute the CPU bound work elsewhere.

That said, the benchmark is not wrong. It’s just a misconception to say that a web framework is slower than the other based on CPU bound work.

As a note, you can increase the time on which uvicorn runs installing uvloop and httptools.

Disclaimer: I’m a uvicorn maintainer.

Answered By: Marcelo Trylesinski

Should have an async before the route def in fastapi to take advantage of asynchronous requests which is one of the main reasons fastapi is much faster in production (but only when multiple requests are made at the same time)

Answered By: user3395901
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.