Flask vs FastApi 性能对比测试

Flask和Fastapi都是Python下流行的Web框架,前者有大量拥趸,是一个老牌框架,后者相对较新,但是利用了异步技术和uvloop,都说性能比Flask好很多,于是就我就对比实测一下。由于Windows下不支持uvloop,发挥不了Fastapi的性能,于是我的测试环境为:

Ubuntu 			  22.04.4
python 			  3.10
Flask             3.0.3
waitress          3.0.0
fastapi           0.110.1
uvicorn           0.29.0
uvloop            0.19.0

准备工作

测试代码非常简单,如下:

一、flask

# flask
# -*- coding: utf-8 -*-from flask import Flaskdef create_app(test_config=None):app = Flask(__name__, instance_relative_config=True)if test_config is None:passelse:app.config.from_mapping(test_config)@app.route('/fk')def root():return {'result': 'Hello, flask111'}return appif __name__ == '__main__':pass

二、fastapi

# -*- coding: utf-8 -*-from fastapi import FastAPI
# import uvloop  # windows 下暂不支持app = FastAPI()@app.get("/fa")
def read_root():return {'result': 'Hello, fastapi.22'}if __name__ == '__main__':pass

由于waitress-serve默认是4线程运行,这里强制为1个线程,用waitress-serve运行flask命令如下:

waitress-serve --call --host='0.0.0.0' --port='8001' --threads=1 't_flask:create_app'

同时将uvicorn设置为1个worker(单个工作进程),用uvicorn运行fastapi命令如下:

uvicorn t_fastapi:app --host '0.0.0.0' --port 8005 --log-level error --workers 1

测试结果

用ab工具测试并发如下:
flask的结果

# ab -n 10000 -c 1000 http://192.168.242.129:8001/fk
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 192.168.242.129 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requestsServer Software:        waitress
Server Hostname:        192.168.242.129
Server Port:            8001Document Path:          /fk
Document Length:        29 bytesConcurrency Level:      1000
Time taken for tests:   9.530 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      1740000 bytes
HTML transferred:       290000 bytes
Requests per second:    1049.32 [#/sec] (mean)
Time per request:       952.997 [ms] (mean)
Time per request:       0.953 [ms] (mean, across all concurrent requests)
Transfer rate:          178.30 [Kbytes/sec] receivedConnection Times (ms)min  mean[+/-sd] median   max
Connect:        0    6  15.6      0      64
Processing:    43  899 171.7    950     989
Waiting:        2  897 171.7    948     987
Total:         66  904 157.5    950     990Percentage of the requests served within a certain time (ms)50%    95066%    97175%    97680%    97890%    98095%    98298%    98399%    985100%    990 (longest request)

fastapi的结果

ab -n 10000 -c 1000 http://192.168.242.129:8005/fa
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 192.168.242.129 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requestsServer Software:        uvicorn
Server Hostname:        192.168.242.129
Server Port:            8005Document Path:          /fa
Document Length:        30 bytesConcurrency Level:      1000
Time taken for tests:   9.132 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      1550000 bytes
HTML transferred:       300000 bytes
Requests per second:    1095.02 [#/sec] (mean)
Time per request:       913.225 [ms] (mean)
Time per request:       0.913 [ms] (mean, across all concurrent requests)
Transfer rate:          165.75 [Kbytes/sec] receivedConnection Times (ms)min  mean[+/-sd] median   max
Connect:        0    6  18.3      0      81
Processing:    88  860 155.5    919     958
Waiting:        7  859 157.0    918     957
Total:         88  867 138.5    919     958Percentage of the requests served within a certain time (ms)50%    91966%    92575%    92880%    93290%    94095%    94398%    95399%    956100%    958 (longest request)

从结果看出,fastapi只比flask快了 (1095.02 - 1049.32) / 1049.32 * 100 = 4.4%,优势不明显。

接下来使用4个线程或工作进程来测试:
flask和fastapi的启动命令分别为:

# flask
waitress-serve --call --host='0.0.0.0' --port='8001' --threads=4 't_flask:create_app'# fastapi
uvicorn t_fastapi:app --host '0.0.0.0' --port 8005 --log-level error --workers 4

新的测试结果如下:
flask结果:

ab -n 1000 -c 100 http://192.168.242.129:8001/fk
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 192.168.242.129 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requestsServer Software:        waitress
Server Hostname:        192.168.242.129
Server Port:            8001Document Path:          /fk
Document Length:        29 bytesConcurrency Level:      100
Time taken for tests:   0.943 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      174000 bytes
HTML transferred:       29000 bytes
Requests per second:    1060.45 [#/sec] (mean)
Time per request:       94.300 [ms] (mean)
Time per request:       0.943 [ms] (mean, across all concurrent requests)
Transfer rate:          180.19 [Kbytes/sec] receivedConnection Times (ms)min  mean[+/-sd] median   max
Connect:        0    1   2.3      0      10
Processing:    12   88  11.1     91      98
Waiting:        2   84  18.2     90      96
Total:         13   89   9.8     92     101Percentage of the requests served within a certain time (ms)50%     9266%     9375%     9480%     9490%     9595%     9698%     9799%     98100%    101 (longest request)

fastapi结果:

# ab -n 1000 -c 100 http://192.168.242.129:8005/fa
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 192.168.242.129 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requestsServer Software:        uvicorn
Server Hostname:        192.168.242.129
Server Port:            8005Document Path:          /fa
Document Length:        30 bytesConcurrency Level:      100
Time taken for tests:   0.688 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      155000 bytes
HTML transferred:       30000 bytes
Requests per second:    1453.58 [#/sec] (mean)
Time per request:       68.796 [ms] (mean)
Time per request:       0.688 [ms] (mean, across all concurrent requests)
Transfer rate:          220.02 [Kbytes/sec] receivedConnection Times (ms)min  mean[+/-sd] median   max
Connect:        0    7  10.1      4      55
Processing:     8   59  20.7     54     142
Waiting:        3   57  20.4     52     139
Total:          9   66  20.7     60     144Percentage of the requests served within a certain time (ms)50%     6066%     6775%     7580%     8390%    10095%    11298%    12299%    127100%    144 (longest request)

结论

开启4线程或4工作进程后,flask几乎一样,基本没有提升(1049.32 --> 1060.45)。而fastapi有显著性能提升(1095.02 --> 1453.58),但也不是4倍那么多。

所以说,多个工作进程的情况下fastapi是更好的,但flask性能也不差。也许是ab测试工具的问题,欢迎大家讨论。

fastapi有不少有点,比如支持异步、ws、自动生成文档、强调声明变量类型等。而flask就是轻量,上手快,没有的功能就是装插件,“可插拔”。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/1117.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

63、ARM/STM32中IIC相关学习20240417

完成温湿度传感器数据采集实验。 【思路&#xff1a;1.通过IIC通信原理&#xff0c;理解其通信过程&#xff0c;通过调用封装的IIC函数达成主机和从机之间&#xff1a;起始信号、终止信号、读、写数据的操作&#xff1b; 2.了解温湿度传感器控制芯片SI7006的工作原理&#…

【C++】飞机大战项目记录

源代码与图片参考自《你好编程》的飞机大战项目&#xff0c;这里不进行展示。 本项目是仅供学习使用的项目 飞机大战项目记录 飞机大战设计报告1 项目框架分析1.1 敌机设计&#xff1a;1.2 玩家飞机控制&#xff1a;1.3 子弹发射&#xff1a;1.4 游戏界面与互动&#xff1a;1.5…

解决Linux根分区空间不足的方法:利用Home分区进行扩容

前言 在进行系统安装时&#xff0c;一个常见的困扰是默认分区设置可能导致home分区拥有过多的空间&#xff0c;而root分区却显得十分紧缺。这种情况下&#xff0c;用户往往会陷入无法继续安装软件或存储文件的困境。本文将向您展示如何通过合理的调整&#xff0c;将home分区中多…

二叉排序树及实现

二叉排序树及实现 二叉排序树&#xff08;Binary Sort Tree, BST&#xff09;又称为二叉查找树。在一般情况下&#xff0c;查询效率要比链表结构要高。对于二叉排序树中的任何一个非叶子节点&#xff0c;要求左子节点的值比当前节点的值小&#xff0c;右子节点的值比当前节点的…

【6】mysql查询性能优化-关联子查询

【README】 0. 先说结论&#xff1a;一般用inner join来改写in和exist&#xff0c;用left join来改写not in&#xff0c;not exist&#xff1b;&#xff08;本文会比较内连接&#xff0c;包含in子句的子查询&#xff0c;exist的性能 &#xff09; 1. 本文总结自高性能mysql 6…

Python 面向对象——1.基本概念

本章学习链接如下&#xff1a; 基本概念与语法 类&#xff08;Class&#xff09;&#xff1a;定义了一组对象共有的属性和方法的蓝图。类是创建对象的模板。 对象&#xff08;Object&#xff09;&#xff1a;类的实例。对象包含实际的数据和操作数据的方法。 属性&#xff0…

NLP_知识图谱_三元组实战

文章目录 三元组含义如何构建知识图谱模型的整体结构基于transformers框架的三元组抽取baselinehow to use预训练模型下载地址训练数据下载地址 结构图代码及数据bertconfig.jsonvocab.txt datadev.jsonschemas.jsontrain.jsonvocab.json 与bert跟data同个目录model.pytrain.py…

原型和原型链--图解

https://juejin.cn/post/7255605810453217335 prototype是函数的属性&#xff08;一个对象&#xff09;&#xff0c;不是对象的属性&#xff0c;普通函数和构造函数的prototype属性是空对象&#xff5b;&#xff5d;&#xff08;其实有2个属性&#xff0c;一个是constructor&a…

Vue3: toRefs与toRef的基本使用

一、前言 本文主要介绍toRefs与toRef的基本使用。 二、内容 1、基本概念 作用: toRefs与toRef可以将一个响应式对象中的每一 个属性&#xff0c;转换为ref对象&#xff1b;不同 toRefs与toRef功能一致&#xff0c;但toRefs可以批量转换。 2、toRefs 如果把reactive定义的…

记录交叉编译环境配置--海思开发板的 嵌入式nginx和 php的移植

嵌入式 lnmp搭建的记录 一些交叉编译的配置环境思路分享&#xff1a;P&#xff1a;php编译PHP可能遇到的问题configure阶段&#xff1a;Makefile-make阶段&#xff1a;Makefile-make install阶段&#xff1a; N&#xff1a;Nginx 文章比较水&#xff0c;并没有没解决什么实际问…

java面向对象的继承Shape

* - Shape基类 拥有draw()和erase()两个方法 * - 基于Shape类创建Circle子类 * - 基于Shape类创建Square子类 * - 创建Shape类创建Triangle子类 * - 创建随机形状发生的类(不是子类) * - 定义公共类(测试类),创建Shape类的数组对象长度为10&#xff0c;并为对象数组赋值其内容C…

二维码门楼牌管理应用平台建设:助力场所整改与消防安全

文章目录 前言一、二维码门楼牌管理应用平台的构建背景二、二维码门楼牌管理应用平台在场所整改中的作用三、二维码门楼牌管理应用平台的意义与价值四、二维码门楼牌管理应用平台的未来展望 前言 随着城市管理的日益精细化&#xff0c;二维码门楼牌管理应用平台的建设成为了提…

el-table 遇到的问题一

点击按钮一&#xff0c;出现table1 点击按钮二&#xff0c;出现table2 刚开始很简单&#xff0c;就用简单的 v-if 添加&#xff0c;但是会出现问题&#xff0c;就是点击按钮一&#xff0c;会出现table1&#xff0c;点击按钮二&#xff0c;会出现table2&#xff0c;再点击按钮…

C++ - STL详解(七)— stack和queue的介绍及使用

目录 一. stack 1.1 stack的介绍 1.2 stack的定义 1.3 stack的使用 ​编辑 二. queue 2.1 queue的介绍 2.2 queue的定义 2.3 queue的使用 一. stack 1.1 stack的介绍 stack是一种容器适配器&#xff0c;专门用在具有后进先出操作的上下文环境中&#xff0c;其删除…

Java中的消息队列(如RabbitMQ、Kafka)是如何工作的

Java中的消息队列&#xff08;Message Queue&#xff09;是一种用于应用程序之间或应用程序组件之间进行异步通信的机制。消息队列允许发送者&#xff08;生产者&#xff09;将消息发送到队列中&#xff0c;而接收者&#xff08;消费者&#xff09;可以从队列中读取并处理这些消…

代码随想录算法训练营第十八天 | 513.找树左下角的值、112. 路径总和、113. 路径总和ii

代码随想录算法训练营第十八天 | 513.找树左下角的值、112. 路径总和、113. 路径总和ii 自己看到题目的第一想法看完代码随想录之后的想法自己实现过程中遇到哪些困难 链接: 513.找树左下角的值 链接: 112. 路径总和&#xff0c;和 113. 路径总和ii 链接: 从中序与后序遍历序…

Debezium系列之:部署Debezium采集Oracle数据库的详细步骤

Debezium系列之:部署Debezium采集Oracle数据库的详细步骤 一、部署Debezium Oracle连接器二、Debezium Oracle 连接器配置三、添加连接器配置四、可插拔数据库与不可插拔数据库一、部署Debezium Oracle连接器 部署的详细步骤可以参考博主这篇技术文章: Debezium系列之:安装…

怎么用3ds MAX制作蜂窝状模型?

1、新建多边形&#xff1a;打开3ds MAX软件&#xff0c;在样条线中新建一个多边形。 2、设置参数&#xff1a;切换到顶视图&#xff0c;设置多边形的参数&#xff0c;例如半径为10&#xff0c;变数为6&#xff0c;以形成一个六边形的基础。 3、复制并形成圆柱状&#xff1a;打开…

如何通过Postgres的日志进行故障排查?

文章目录 一、配置日志记录二、查看和分析日志三、使用日志进行故障排查的示例四、总结 在进行数据库管理和维护时&#xff0c;日志分析是一项至关重要的技能。PostgreSQL的日志记录功能可以帮助我们追踪数据库的运行状态&#xff0c;定位问题&#xff0c;以及优化性能。下面&a…

深入Git配置

git配置 git config -h usage: git config [<options>]Config file location--global use global config file--system use system config file--local use repository config file--worktree use per-worktree con…