fastapi项目部署与golang性能粗略对比(娱乐模式)

本文对比了Python的FastAPI和Golang的Gin框架在无数据库访问情况下的性能。测试结果显示,两者性能差距不大,实际应用中可忽略不计。在考虑开发效率和成本平衡时,FastAPI和Gin的性能差异是可以接受的。使用wrk工具进行压测,Gin的Requests/sec约为3982.66,而FastAPI的Requests/sec约为2437.98。
摘要由CSDN通过智能技术生成

概述

这是一个粗略的fastapi和gin的性能对比。
测试的时候基本上不涉及数据库访问,单纯考虑的是语言和框架自身的性能。
先上代码(项目地址:https://github.com/Chise1/fastapi_vs_gin.git)。
tips:这个项目随便写的,是在学习golang的时候看到的,拿过来改吧改吧就做个比较。

结论

gin和fastapi的性能差并没有想象中的那么大,在考虑到数据库的影响的时候,这点性能差是完全可以接受的。
在开发商省掉的时间完全可以弥补服务器上需要的额外费用。
使用wrk测试的:

在我自己的笔记本上的结果: python:

root@cs:~/wrk# ./wrk -t80 -c200 -d30s --latency http://127.0.0.1:8000/getOne
Running 30s test @ http://127.0.0.1:8000/getOne
  80 threads and 200 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    65.75ms   30.52ms 324.83ms   73.11%
    Req/Sec    30.68     12.01    90.00     80.93%
  Latency Distribution
     50%   60.80ms
     75%   82.29ms
     90%  103.94ms
     99%  160.36ms
  73394 requests in 30.10s, 8.99MB read
Requests/sec:   2437.98
Transfer/sec:    305.83KB

golang:

root@cs:~/wrk# ./wrk -t80 -c200 -d30s --latency http://127.0.0.1:8080/getOne
Running 30s test @ http://127.0.0.1:8080/getOne
  80 threads and 200 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    46.67ms   48.89ms 550.70ms   86.42%
    Req/Sec    50.43     29.29   250.00     64.74%
  Latency Distribution
     50%   36.64ms
     75%   69.47ms
     90%  108.03ms
     99%  211.33ms
  119883 requests in 30.10s, 13.72MB read
Requests/sec:   3982.66
Transfer/sec:    466.72KB

代码

golang

package main

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

func GetProduct(ctx *gin.Context) {
	ctx.Writer.Write([]byte("true"))

}
func main() {
	r:=gin.Default()
	r.GET("/getOne",GetProduct)
	r.Run()
}
//压测工具 wrk

python代码

from fastapi import FastAPI

app = FastAPI()

@app.get("/getOne")
def get_product():
    return True

部署说明

python采用了guvicorn进行部署,因为golang自动会利用多核,所以这块就不考虑了。
具体部署

python部署

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.8
LABEL maintainer="Chise <chise123@live.com>"
RUN pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple/
ENV POETRY_VIRTUALENVS_CREATE=false
RUN pip3 install poetry
COPY pyproject.toml poetry.lock /app/
RUN poetry install --no-root --no-dev
COPY . /app

docker-compose

version: '3'
services:
  wrkt:
    build: .
    environment:
      MODULE_NAME: main.py
      VARIABLE_NAME: "app"
      APP_MODULE: main:app
      PORT: 8000
    ports:
      - '8000:8000'
    image: wrkt
    command: "/start.sh"

后记

对golang的部署还不熟,希望有大佬可以指正我的问题。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值