Sanic FastApi Gin性能對比

FastApi

from fastapi import FastAPI

app = FastAPI()


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

run

uvicorn main:app --log-level critical --port 8003 --workers 4

wrk

wrk -t20 -d30s -c500 http://127.0.0.1:8003

result

Running 30s test @ http://127.0.0.1:8003
  20 threads and 500 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    25.85ms   25.89ms 200.40ms   91.93%
    Req/Sec     1.17k   720.43     7.18k    70.39%
  695567 requests in 30.10s, 99.50MB read
Requests/sec:  23108.61
Transfer/sec:      3.31MB

Sanic

code

from sanic import Sanic
from sanic.response import json

app = Sanic("My Hello, world app")


@app.route('/')
async def test(request):
    return json({"message": "Hello World"})


if __name__ == '__main__':
    app.run(port=8081)

run

sanic main2.app -p 8081 -w 4

wrk result

Running 30s test @ http://127.0.0.1:8081
  20 threads and 500 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    11.05ms    7.57ms 213.12ms   89.48%
    Req/Sec     2.43k     0.97k    8.31k    73.25%
  1450127 requests in 30.09s, 165.95MB read
Requests/sec:  48189.37
Transfer/sec:      5.51MB

Gin

code

package main

import (
    "io/ioutil"
    "github.com/gin-gonic/gin"
)

func main() {
    gin.SetMode(gin.ReleaseMode)
    gin.DefaultWriter = ioutil.Discard

    r := gin.Default()
    r.GET("/", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "Hello World",
        })
    })
    r.Run("0.0.0.0:8001") // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

Running 30s test @ http://127.0.0.1:8001
  20 threads and 500 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    18.23ms   26.19ms 378.59ms   87.93%
    Req/Sec     2.72k     2.25k   18.40k    85.15%
  1610627 requests in 30.10s, 227.33MB read
Requests/sec:  53513.85
Transfer/sec:      7.55MB

Sanic總體比FastApi更快, 多次測Sanic數據起伏大,還沒找到原因,Gin的數據多次測試起伏也大, Gin比Sanic高一些,在同一個數量級

從http請求處理來看, Gin>Sanic>FastApi

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章