Flask vs FastApi 性能对比测试

news/2024/10/20 13:33:01/

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

python"># 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

python"># -*- 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个线程或工作进程来测试:
flaskfastapi的启动命令分别为:

# 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.ppmy.cn/news/1426910.html

相关文章

The O-one:开源语言模型计算机的革命

在人工智能的浪潮中&#xff0c;The O-one作为一个创新的开源项目&#xff0c;正以其独特的功能和开放性吸引着全球开发者和科技爱好者的目光。这个项目不仅仅是一个简单的语言模型&#xff0c;它是一个能够通过语音交互与计算机进行对话的智能系统&#xff0c;极大地提升了人机…

解决Keil V5.38 和 ST-Link V3 Debug不能运行问题

目录 概述 1 问题描述 1.1 情况一 1.2 情况二 1.3 情况三 2 解决方法 2.1 认识Keil Mico Lib 2.2 使能Keil Mico Lib 3 验证 3.1 进入C程序Main验证 3.2 断点验证 3.3 上电重启验证 4 结论 笔者使用的验证代码下载地址&#xff1a; stm32-freeRTOS-queue资源-CSD…

爬虫 Selector 选择器查找元素

// <!--jsoup解析工具所需依赖--> // <dependency> // <groupId>org.jsoup</groupId> // <artifactId>jsoup</artifactId> // <version>1.10.3</version> // </depende…

DNS的背景工作原理和作用

1.背景: DNS的背景起源于20世纪60年代末的美国国防部高级研究计划局&#xff08;ARPA&#xff09;建立的试验性计算机网络ARPAnet。DNS&#xff0c;全称域名系统&#xff08;Domain Name System&#xff09;&#xff0c;是为了解决互联网上主机名与IP地址对应关系而发展起来的…

文件夹变白色文件,数据恢复全攻略助你轻松找回!

在日常的电脑使用中&#xff0c;你是否曾遇到过文件夹突然变成了白色文件的情况&#xff1f;这种看似无害的变化&#xff0c;实则可能隐藏着数据丢失的风险。当你发现原本正常的文件夹图标变成了无关联程序的白板图标&#xff0c;且无法正常打开时&#xff0c;内心无疑是焦虑不…

Clark Transform的FPGA代码实现讲解

Clark 变换是坐标转换&#xff0c;将输入的三相电流转换到两相直角坐标下电流&#xff0c;如下图为坐标表示方法。 根据坐标的投影我们可以得到 从而可以推知&#xff1a; 上述公式为最终代码中实现的计算公式。 在FPGA中实现时&#xff0c;由于FPGA中不擅长浮点数计算&#xf…

Android 13 有线以太网静态ip保存逻辑梳理分析

源码环境&#xff1a;高通Android 13 这里特别说明&#xff0c;Android13中&#xff0c;ipconfig.txt配置文件目录有改变 以前&#xff1a;/data/misc/ethernet/ipconfig.txt 最新的有线网配置文件保存目录&#xff1a; /data/misc/apexdata/com.android.tethering/misc/ethe…

UE4_动画基础_动画重定位原理

动画重定位 是一种允许在共用相同骨架资源但比例差异很大的角色之间复用动画的功能。通过重定位&#xff0c;可以防止生成动画的骨架在使用来自不同外形的角色的动画时丢失比例或产生不必要的变形。 通过动画重定位&#xff0c;还可以在使用 不同骨架 资源的角色之间共享动画&a…