FastAPI、Flask、Golang性能测试
2011 年 5 月 12 日
前言
最近看到一篇讲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秒
Flask: 500并发 30秒
Golang: 500并发 30秒
FastAPI: 1000并发 30秒
Flask: 1000并发 30秒
GOlang: 1000并发 30秒
总结
从结果来看 FastAPI性能确实已经远远超过Flask,虽然和Golang还有差距,但是已经接近了。python3.6是一个里程碑的版本,很多库开始支持异步,python也和 性能 这一个关键词联系上了,还需要迁移到Golang吗?开发方便,各种库齐全,兼具了快速开发和高性能的python,还要什么自行车?赶紧用起来吧。。。