Python asynchronous programming web framework asynchronous vs synchronous Redis concurrency comparison

1 | 0 test basic information

Subject: Comparing the performance difference between asynchronous framework and synchronous framework in RedisIO operation
python version : python 3.8
database : redis 5.0.7
pressure testing tool : locust
web framework : synchronous: flask asynchronous: starlette
request concurrency : simulate 10 users
server configuration : Intel(R) i7-12700F
client configuration : Intel(R) i7-8700 3.20GHz

2 | 0 flask sync framework

Flask is a lightweight web framework in python, characterized by flexibility, light weight, and high scalability. At the same time, flask is a synchronization framework, and the official and most recommended package redis-py is used to operate Redis. Early versions of redis-py only support synchronous mode, and asynchronous mode is already supported in higher versions. The interface function is very simple. Connect to Redis to read a key from it, which has been written to redis in advance.

from flask import Flask
from redis import StrictRedis

app = Flask(__name__)


@app.route('/user')
def user_list():
    redis_conn = StrictRedis(host="127.0.0.1", port=6379, decode_responses=True)
    res = redis_conn.get("name")
    return res


if __name__ == '__main__':
    app.run(port=8090, host="0.0.0.0")

2 | 1 Pressure test results

Concurrency: 342

3 | 0 starlette asynchronous framework

starlette is one of the only two modules that the popular asynchronous framework fastapi depends on, and is mainly used for processing asynchronous requests. redis-py supports both synchronous mode and asynchronous mode, so its asynchronous mode is used in the asynchronous framework of starlette.

from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route
from uvicorn.main import run
from redis.asyncio import StrictRedis

async def user(request):
    redis_conn = StrictRedis(host="127.0.0.1", port=6379, decode_responses=True)
    res = await redis_conn.get("name")
    await redis_conn.close()
    return JSONResponse(res)


app = Starlette(debug=True, routes=[Route('/user', user),])


if __name__ == "__main__":
    run(app, host="0.0.0.0")

3 | 1 Pressure test results

Concurrency: 1361
 

4 | 0 vs.

Concurrency graph comparison:

Parameter comparison:

frame Return IO Pure framework without IO file I/O Database I/O
flask 315 463 453 225
starlet 1361 1539 1538 1496
Performance ratio (async/sync) 4.3 3.3 3.4 6.6

5 | 0Summary _

In terms of Redis IO, the performance of the asynchronous framework is about 4.3 times that of the synchronous framework, which is roughly the same as file IO and database IO. The comparison between horizontal and database IO has dropped slightly, and this is indeed the case in multiple tests. It is guessed that it is related to the asynchronous mode of the redis-py module.
This article is the last in the comparison series. From the comparison of IO tasks closely related to asynchrony, it can be seen that the concurrency of asynchronous programming is relatively high. In the following pages, the principle and use of asynchronous programming in python will be explained.

Come on, testers! If you need to improve your plans, do it, it's better to be on the road than to wait and see from the beginning. Your future self will definitely thank your hard-working self now!

 

Guess you like

Origin blog.csdn.net/weixin_47648853/article/details/131248268