FastAPI、Flask、Golang性能测试

栏目: IT技术 · 发布时间: 6年前

内容简介:最近看到一篇讲python 几款异步框架,其中一款框架让人眼前一亮,他就是FastAPI: 500并发 30秒Flask: 500并发 30秒

前言

最近看到一篇讲 python 几款异步框架,其中一款框架让人眼前一亮,他就是 Fastapi 。其中介绍到 其性能能与Node相匹配,甚至在某种情况下可以与Golang相媲美 。这是什么概念?传统观念中,python生来视乎就与高性能、大并发风马牛不相及,而Golang似乎是高性能的代名词。这里python出现一款框架与Golang相媲美,无法不让人对这个东西产生联想。这里就对这三个做一个性能测试,看看情况如何

环境

  • 2 核CPU, 8G内存 虚拟机
  • 压测工具: webbench

代码示例

FastAPI

from fastapi import FastAPI

app = FastAPI()

@app.get('/')
async def index():
    return {'message': 'hello world'}

flask

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return {'message':'hello world'}

if __name__ == '__main__':
    app.run(host='0.0.0.0')

Golang

package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request){
    fmt.Fprintf(w, "{'message': 'hello world'}")
}

func main(){
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8001", nil)
}

压测结果

FastAPI: 500并发 30秒

FastAPI、Flask、Golang性能测试

Flask: 500并发 30秒

FastAPI、Flask、Golang性能测试

Golang: 500并发 30秒

FastAPI、Flask、Golang性能测试

FastAPI: 1000并发 30秒

FastAPI、Flask、Golang性能测试

Flask: 1000并发 30秒

FastAPI、Flask、Golang性能测试

GOlang: 1000并发 30秒

FastAPI、Flask、Golang性能测试

总结

从结果来看 FastAPI性能确实已经远远超过Flask,虽然和Golang还有差距,但是已经接近了。python3.6是一个里程碑的版本,很多库开始支持异步,python也和 性能 这一个关键词联系上了,还需要迁移到Golang吗?开发方便,各种库齐全,兼具了快速开发和高性能的python,还要什么自行车?赶紧用起来吧。。。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Blockchain Basics

Blockchain Basics

Daniel Drescher / Apress / 2017-3-16 / USD 20.99

In 25 concise steps, you will learn the basics of blockchain technology. No mathematical formulas, program code, or computer science jargon are used. No previous knowledge in computer science, mathema......一起来看看 《Blockchain Basics》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试