Calmcode - fastapi: async one

Can we measure the async performance in FastAPI?

1 2 3 4 5 6 7 8 9

Async Code

This is the code that we've added to app.py in this part;

import time
import asyncio

@app.get("/sleep_slow")
def sleep_slow():
    time.sleep(1)
    return {"status": "done"}

@app.get("/sleep_fast")
async def sleep_fast():
    await asyncio.sleep(1)
    return {"status": "done"}

If you get an error while running this it might be a python version issue. Make sure that you're using a recent version of python (3.6 and up).

Boom!

To use boom make sure it is installed first via:

brew install boom

Once that's dealt with, you can run the following commands;

boom http://127.0.0.1:8000/sleep_slow -c 200 -n 200
boom http://127.0.0.1:8000/sleep_fast -c 200 -n 200

In both commands boom with send a total of (defined via -n) 200 requests and will do it with a concurrency of 200 (defined via -c). Concurrency is just a fancy word of saying "at the same time". If you'd like boom to send 100 requests in batches of 20 at a time you'd use this command;

boom http://127.0.0.1:8000/sleep_fast -c 20 -n 100