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

學(xué)無先后,達(dá)者為師

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

Python?flask與fastapi性能測(cè)試方法介紹_python

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

背景

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

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

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

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

apache ab介紹

apache ab性能測(cè)試

安裝

? ? yum -y install httpd-tools

部分參數(shù)說明

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

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

測(cè)試get請(qǐng)求

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

測(cè)試post請(qǐng)求

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

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

測(cè)試計(jì)劃

模擬真實(shí)每次請(qǐng)求調(diào)用腳本,分別對(duì)每一個(gè)數(shù)量級(jí)的請(qǐng)求量進(jìn)行測(cè)試。

請(qǐng)求總數(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

測(cè)試代碼

處理post請(qǐng)求,延時(shí)3s返回結(jié)果。flask啟動(dòng)20個(gè)進(jìn)程。fastapi啟動(dòng)一個(gè)進(jìn)程。

## 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
# 啟動(dòng)監(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('啟動(dòng)進(jìn)程第幾個(gè):{}', i)
            p = Process(target=server_forever)
            p.start()
if __name__ == "__main__":
    # 單進(jìn)程 + 協(xié)程
    # run(False)
    # 多進(jìn)程 + 協(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}
# 啟動(dòng)監(jiān)聽ip、端口
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8081)

測(cè)試結(jié)果

框架類型 請(qǐng)求總數(shù) 每次并發(fā)數(shù) 耗時(shí)(s) 每次并發(fā)數(shù) 耗時(shí)(s) 每次并發(fā)數(shù) 耗時(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

欄目分類
最近更新