日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

Python?flask與fastapi性能測試方法介紹_python

作者:我愛看明朝 ? 更新時間: 2023-01-08 編程語言

背景

sy項目通過MQ接受業(yè)務系統(tǒng)的業(yè)務數(shù)據(jù),通過運行開發(fā)者開發(fā)的python腳本執(zhí)行業(yè)務系統(tǒng)與財務系統(tǒng)數(shù)據(jù)的一致性校驗。

sy系統(tǒng)需要每天運行大量的python腳本。目前使用falsk日運行6W+次python腳本,由于性能存在瓶頸,需要引入

新的fastapi框架,來解決cpu、內(nèi)存性能壓榨不夠及目前的性能瓶頸。本文目標給出兩者的性能測試報告。

給出選擇哪個框架的性能數(shù)據(jù)支撐。

apache ab介紹

apache ab性能測試

安裝

? ? yum -y install httpd-tools

部分參數(shù)說明

-n ?執(zhí)行的請求總數(shù)

-c 并發(fā)數(shù), 同時執(zhí)行的數(shù)量, c不能大于n
-p post請求指定的文件
-T header Content-type值,默認為 'text/plain'

測試get請求

ab -c 10 ?http://127.0.0.1:8081/cppla

測試post請求

ab -n 100 -c 10 -T 'application/json' -p httpjson.txt ?http://127.0.0.1:8081/cppla1 ?

// httpjson.txt的內(nèi)容
{"recordId": 123}

測試計劃

模擬真實每次請求調(diào)用腳本,分別對每一個數(shù)量級的請求量進行測試。

請求總數(shù) 每次并發(fā)數(shù) 每次并發(fā)數(shù) 每次并發(fā)數(shù)
100 10 100 1000
1000 10 100 1000
10000 10 100 1000
20000 10 100 1000
30000 10 100 1000
40000 10 100 1000
50000 10 100 1000
60000 10 100 1000
80000 10 100 1000

測試代碼

處理post請求,延時3s返回結(jié)果。flask啟動20個進程。fastapi啟動一個進程。

## flask 代碼
# coding: utf-8
from gevent import monkey
from gevent.pywsgi import WSGIServer
import requests
import datetime
import os
from multiprocessing import cpu_count, Process
from flask import Flask, jsonify,request
import json
import traceback
import importlib
from loguru import logger
import time
app = Flask(__name__)
# 執(zhí)行run方法
@app.route("/cppla1", methods=['POST', 'GET'])
def cppla1():
    data = request.json
    time.sleep(3)
    return data
# 啟動監(jiān)聽ip、端口
def run(MULTI_PROCESS):
    if MULTI_PROCESS == False:
        WSGIServer(('0.0.0.0', 8081), app).serve_forever()
    else:
        mulserver = WSGIServer(('0.0.0.0', 8081), app)
        mulserver.start()
        def server_forever():
            mulserver.start_accepting()
            mulserver._stop_event.wait()
       # for i in range(cpu_count()):
        for i in range(20):
            logger.info('啟動進程第幾個:{}', i)
            p = Process(target=server_forever)
            p.start()
if __name__ == "__main__":
    # 單進程 + 協(xié)程
    # run(False)
    # 多進程 + 協(xié)程
    log_init()
    run(True)
## fastapi
# coding: utf-8
# import web framework
from fastapi import FastAPI
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
# import base lib
import datetime
import os
import requests
import json
import traceback
import importlib
from loguru import logger
import time
app = FastAPI()
@app.post("/cppla1")
def function_benchmark(data:dict):
    time.sleep(3)
    return {"item": data}
# 啟動監(jiān)聽ip、端口
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8081)

測試結(jié)果

框架類型 請求總數(shù) 每次并發(fā)數(shù) 耗時(s) 每次并發(fā)數(shù) 耗時(s) 每次并發(fā)數(shù) 耗時(s)
fastapi 100 10 33.119 100 12.148 1000 ab命令不支持
flask 100 10 45.088 100 81.106 1000 ab命令不支持
fastapi 1000 10 304.057 100 78.283 1000 78.631
flask 1000 10 327.472 100 198.273 1000 303.442
fastapi 10000 10 x 100 754.296 1000 757.719
flask 10000 10 x 100 1550.119 1000 1970.427
fastapi 20000 10 x 100 x 1000 x
flask 20000 10 x 100 x 1000 x
fastapi 30000 10 x 100 x 1000 x
flask 30000 10 x 100 x 1000 x
fastapi 40000 10 x 100 x 1000 x
flask 40000 10 x 100 x 1000 x
fastapi 50000 10 x 100 x 1000 x
flask 50000 10 x 100 x 1000 x
fastapi 60000 10 x 100 x 1000 x
flask 60000 10 x 100 x 1000 x
fastapi 80000 10 x 100 x 1000 x
flask 80000 10 x 100 x 1000 x

結(jié)論

fastapi是flask性能的3倍,推薦使用fastap。

原文鏈接:https://blog.csdn.net/u013565163/article/details/128048213

欄目分類
最近更新