代码:
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/")
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)