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 Flask


def create_app(test_config=None):
    app = Flask(__name__, instance_relative_config=True)

    if test_config is None:
        pass
    else:
        app.config.from_mapping(test_config)

    @app.route('/fk')
    def root():
        return {'result': 'Hello, flask111'}

    return app


if __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 requests


Server Software:        waitress
Server Hostname:        192.168.242.129
Server Port:            8001

Document Path:          /fk
Document Length:        29 bytes

Concurrency 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] received

Connection 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     990

Percentage of the requests served within a certain time (ms)
  50%    950
  66%    971
  75%    976
  80%    978
  90%    980
  95%    982
  98%    983
  99%    985
 100%    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 requests


Server Software:        uvicorn
Server Hostname:        192.168.242.129
Server Port:            8005

Document Path:          /fa
Document Length:        30 bytes

Concurrency 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] received

Connection 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     958

Percentage of the requests served within a certain time (ms)
  50%    919
  66%    925
  75%    928
  80%    932
  90%    940
  95%    943
  98%    953
  99%    956
 100%    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 requests


Server Software:        waitress
Server Hostname:        192.168.242.129
Server Port:            8001

Document Path:          /fk
Document Length:        29 bytes

Concurrency 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] received

Connection 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     101

Percentage of the requests served within a certain time (ms)
  50%     92
  66%     93
  75%     94
  80%     94
  90%     95
  95%     96
  98%     97
  99%     98
 100%    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 requests


Server Software:        uvicorn
Server Hostname:        192.168.242.129
Server Port:            8005

Document Path:          /fa
Document Length:        30 bytes

Concurrency 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] received

Connection 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     144

Percentage of the requests served within a certain time (ms)
  50%     60
  66%     67
  75%     75
  80%     83
  90%    100
  95%    112
  98%    122
  99%    127
 100%    144 (longest request)

结论

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

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

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

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

MySQL-实验-单表、多表数据查询和嵌套查询

目录 0.简单子查询 &#xff08;1&#xff09;带比较运算符的子查询 &#xff08;2&#xff09;关键字子查询 1.多表查询 3.子查询 4.多表子查询 0.简单子查询 &#xff08;1&#xff09;带比较运算符的子查询 在右侧编辑器补充代码&#xff0c;查询大于所有平均年龄的员…

10 SQL进阶 -- 综合练习题 -- 10道经典SQL题目,配套数据与解答

1. 创建表结构和导入数据 1.1 新建数据库 1.2 执行建表语句 点击下方链接直接下载创建数据表脚本:http://tianchi-media.oss-cn-beijing.aliyuncs.com/dragonball/SQL/create_table.sql 执行建表语句执行成功查看创建的表1.3 导入数据 点击下方链接直接下载插入数据脚本:htt…

VBA脚本终章编译器崩溃

一、介绍 本篇文章为VBA脚本隐藏技术的最后一篇&#xff0c;将介绍如何在保证VBA脚本正常执行的情况下&#xff0c;使分析人员无法打开编译器。 那么为什么需要分析人员无法打开编译器呢&#xff1f; 首先&#xff0c;我们需要引入一个知识点。 在上篇《VBA隐藏技术stompin…

笔记本wifi连接外网 网线连接办公内网 设置路由实现内外网可同时访问

工作提供的办公网络是企业内网,接上企业内网网线后 通过无线在连接手机wifi ,会发现内外网无法同时访问,我自己电脑是接上内网网线 也是只能访问外网,除非把外网无线暂时关闭,才可以访问内网 频繁切换很不方便 1.查看外网无线 wifi网卡信息 IPv4 地址: 192.168.18.114 IP…

数据结构学习记录

数据结构 数组 & 链表 相连性 | 指向性 数组可以迅速定位到数组中某一个节点的位置 链表则需要通过前一个元素指向下一个元素&#xff0c;需要前后依赖顺序查找&#xff0c;效率较低 实现链表 // head > node1 > node2 > ... > nullclass Node {constructo…

AI原生时代,操作系统为何是创新之源?

一直以来&#xff0c;操作系统都是软件行业皇冠上的明珠。 从上世纪40、50年代&#xff0c;汇编语言和汇编器实现软件管理硬件&#xff0c;操作系统的雏形出现&#xff1b;到60年代&#xff0c;高级编程语言和编译器诞生&#xff0c;开发者通过操作系统用更接近人的表达方式去…

面向对象(一)

一.类与对象的定义 (1)类(设计图):是对象共同特征的描述: (2)对象:是真实存在的具体东西。 在Java中,必须先设计类&#xff0c;才能获取对象。 二.如何定义类 public class 类名{1.成员变量(代表属性,一般是名词) 2.成员方法(代表行为,一般是动词) 3.构造器 4.代码块 5.内部…

Liunx入门学习 之 基础操作指令讲解(小白必看)

股票的规律找到了&#xff0c;不是涨就是跌 一、Linux下基本指令 1.ls 指令 2.pwd 命令 3.cd 指令 4.touch 指令 5.mkdir 指令 6.rmdir指令 && rm 指令 7.man 指令 8.cp 指令 9.mv指令 10.cat 11.more 指令 12.less 指令 13.head 指令 14.tail 指令 15…

论文解读-Contiguitas: The Pursuit of Physical Memory Contiguity in Datacenters

研究背景&#xff1a; 在内存容量飞速增长的背景下&#xff0c;使用小页管理内存会带来巨大的内存管理开销&#xff08;地址转换开销高&#xff09;。近些年来不少研究尝试给应用分配大段连续区域&#xff0c;或者改善页表结构&#xff08;如使用hash结构的页表&#xff09;以降…

质谱原理与仪器2-笔记

质谱原理与仪器2-笔记 常见电离源电子轰击电离源(EI)碎片峰的产生典型的EI质谱图 化学电离源(CI)快原子轰击源(FAB)基体辅助激光解析电离(MALDI)典型的MALDI质谱图 大气压电离源(API)电喷雾离子源(ESI)大气压化学电离源(APCI)APCI的正负离子模式 大气压光电离源(APPI) 常见电离…

玄子Share-计算机网络参考模型

玄子Share-计算机网络参考模型 分层思想 利用七层参考模型&#xff0c;便于在网络通信过程中&#xff0c;快速的分析问题&#xff0c;定位问题并解决问题 将复杂的流程分解为几个功能相对单一的子过程 整个流程更加清晰&#xff0c;复杂问题简单化 更容易发现问题并针对性的…

线上频繁fullgc问题-SpringActuator的坑

整体复盘 一个不算普通的周五中午&#xff0c;同事收到了大量了cpu异常的报警。根据报警表现和通过arthas查看&#xff0c;很明显的问题就是内存不足&#xff0c;疯狂无效gc。而且结合arthas和gc日志查看&#xff0c;老年代打满了&#xff0c;gc不了一点。既然问题是内存问题&…

Python练习03

题目 解题思路 Demo58 通过字符串切片来进行反转操作 def _reverse():"""这是一个反转整数的函数"""num input("请输入想要反转的整数")print(num[::-1]) 运行结果 Demo61 首先制作一个判断边长的函数&#xff0c;通过三角形两边…

又成长了,异常掉电踩到了MySQL主从同步的坑!

&#x1f4e2;&#x1f4e2;&#x1f4e2;&#x1f4e3;&#x1f4e3;&#x1f4e3; 哈喽&#xff01;大家好&#xff0c;我是【IT邦德】&#xff0c;江湖人称jeames007&#xff0c;10余年DBA及大数据工作经验 一位上进心十足的【大数据领域博主】&#xff01;&#x1f61c;&am…

Google Earth Engine 洪水制图 - 使用 Sentinel-1 SAR GRD

Sentinel-1 提供从具有双极化功能的 C 波段合成孔径雷达 (SAR) 设备获得的信息。该数据包括地面范围检测 (GRD) 场景,这些场景已通过 Sentinel-1 工具箱进行处理,以创建经过校准和正射校正的产品。该集合每天都会更新,新获得的资产会在可用后两天内添加。 该集合包含所有 G…

《王者荣耀》Hello Kitty 小兵皮肤完整设置指南

王者荣耀与三丽鸥的联动活动上线了 Hello Kitty 小兵皮肤&#xff0c;让我们的峡谷小兵们也能穿上漂亮的衣服啦&#xff01;这款皮肤极具卡哇伊风格&#xff0c;引起了许多玩家的关注。许多小伙伴都想知道如何使用这款 Hello Kitty 小兵皮肤&#xff0c;今天小编将为大家整理出…

STC单片机与串口触摸屏通讯程序

/***串口1切换通讯测试,单片机发送数据给触摸屏***/ /***切换到3.0 3.1发送数据到串口通信软件 ***/ /***设置温度 加热时间读写EEPROM正确 ***/ #include <REG52.H> //2023 3 5 L330 CODE2667 #include <intrin…

使用JDK自带工具进行JVM内存分析之旅

进行jvm内存分析可以排查存在和潜在的问题。 通过借助jdk自带的常用工具&#xff0c;可以分析大概可能的问题定位以及确定优化方向。 JVM内存分析有很多好处。 内存泄漏排查&#xff1a;JVM 内存泄漏是指应用程序中的对象占用的内存无法被垃圾回收器释放&#xff0c;导致内存…

遥瞻智慧:排水系统远程监控的卓越解决方案

遥瞻智慧&#xff1a;排水系统远程监控的卓越解决方案 在城市脉络的深层肌理中&#xff0c;排水系统犹如一条条隐秘的生命线&#xff0c;默默承载着城市的呼吸与律动。然而&#xff0c;如何以科技之眼&#xff0c;赋予这些无形网络以实时感知、精准调控的能力&#xff0c;使之…

基于机器学习的车辆状态异常检测

基于马氏距离的车辆状态异常检测&#xff08;单一传感器&#xff09; 基于多元自动编码器的车辆状态异常检测 基于单传感器平滑马氏距离的车辆状态异常检测 工学博士&#xff0c;担任《Mechanical System and Signal Processing》等期刊审稿专家&#xff0c;擅长领域&#xff1…
最新文章