sanic & flask & fastapi 对比

代码:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "hello, flask!"


if __name__ == "__main__":
    app.run(host="127.0.0.1", port=8080)


# -----
from sanic import Sanic
from sanic.response import json


app = Sanic(name = "myApp")


@app.route("/")
async def test(request):
    return json({'hello':'world'})


if __name__ == "__main__":
    app.run()


# ----
from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}


测试:

flask->没开异步 2.68%

sanic -> 60%

fastapi -> 38%

# flask
echo "GET http://localhost:8080/" | vegeta attack -rate=10000  -duration=5s | tee results.bin | vegeta report
(base) ➜  ~ echo "GET http://localhost:8080/" | vegeta attack -rate=10000  -duration=5s | tee results.bin | vegeta report
Requests      [total, rate, throughput]         50000, 10000.28, 206.76
Duration      [total, attack, wait]             6.471s, 5s, 1.472s
Latencies     [min, mean, 50, 90, 95, 99, max]  11.618µs, 31.674ms, 28.548µs, 551.714µs, 1.411ms, 259.346ms, 6.445s
Bytes In      [total, mean]                     17394, 0.35
Bytes Out     [total, mean]                     0, 0.00
Success       [ratio]                           2.68%
Status Codes  [code:count]                      0:48662  200:1338



# sanic
echo "GET http://localhost:8000/" | vegeta attack -rate=10000  -duration=5s | tee results.bin | vegeta report
(base) ➜  ~ echo "GET http://localhost:8000/" | vegeta attack -rate=10000  -duration=5s | tee results.bin | vegeta report
Requests      [total, rate, throughput]         50000, 10000.51, 5984.48
Duration      [total, attack, wait]             5.039s, 5s, 38.79ms
Latencies     [min, mean, 50, 90, 95, 99, max]  14.909µs, 21.216ms, 26.785ms, 41.859ms, 49.848ms, 72.838ms, 89.982ms
Bytes In      [total, mean]                     512601, 10.25
Bytes Out     [total, mean]                     0, 0.00
Success       [ratio]                           60.31%
Status Codes  [code:count]                      0:19847  200:30153



# fastapi
echo "GET http://localhost:8000/" | vegeta attack -rate=10000  -duration=5s | tee results.bin | vegeta report
Requests      [total, rate, throughput]         50000, 10000.20, 3791.83
Duration      [total, attack, wait]             5.032s, 5s, 32.5ms
Latencies     [min, mean, 50, 90, 95, 99, max]  12.492µs, 22.474ms, 50.39µs, 59.181ms, 74.76ms, 131.035ms, 144.045ms
Bytes In      [total, mean]                     477050, 9.54
Bytes Out     [total, mean]                     0, 0.00
Success       [ratio]                           38.16%
Status Codes  [code:count]                      0:30918  200:19082
Error Set:
Get "http://localhost:8000/": dial tcp 0.0.0.0:0->[::1]:8000: socket: too many open files
Get "http://localhost:8000/": dial tcp 0.0.0.0:0->[::1]:8000: connect: connection refused
Get "http://localhost:8000/": dial tcp: lookup localhost: no such host

sanic - normal code

from sanic import Sanic
from sanic.log import logger
from sanic.response import text, HTTPResponse, json
from sanic.request import Request

app = Sanic(name="myApp")


# 默认路由 & 日志
@app.route("/")
async def test(request):
    logger.info("Here is your log~~")
    return json({'hello': 'world'})


# get请求
@app.get("/foo")
async def foo_handler(request):
    return text("i said foo!")


# 写注释
@app.get("/sync")
async def sync_handler(request: Request) -> HTTPResponse:
    return text("Done.")


# 返回json (在默认情况下, Sanic 使用 ujson)
@app.route("/json")
async def handler(request):
    return json({"foo": "bar"})


# url中提取参数
@app.get("/tag/<tag>")
async def tag_handler(request, tag):
    return text("Tag - {}".format(tag))

# post json
@app.route("/test_request_args", methods=["POST"])
async def test_request_args(request):
    a = 1
    request_body = request.json
    return json({
        "a": request_body["url"],
        "b": request_body["mode"]
    })




if __name__ == "__main__":
    app.run(host="localhost", debug=True, port="9005", access_log=True)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值