Flask vs FastApi 性能对比测试

发布于:2024-04-20 ⋅ 阅读:(26) ⋅ 点赞:(0)

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就是轻量,上手快,没有的功能就是装插件,“可插拔”。