Flask、Django、FastApi、SpringBoot、Go性能比较
说明
测试环境为两台1核2G的centos7虚拟机,一台使用wrk进行测试,一台部署业务,数据库为部署于业务机器的Mysql5.7,最大连接数为2000。
测试代码均不相同(没时间写太多Demo,均找一些功能相似的接口进行测试),主要逻辑是由数据库中读取单条数据后序列化通过api返回。
大致测试结果为,经供参考:
框架 | 部署方式 | 每秒处理请求数 |
---|---|---|
Django2.0+DRF | gunicorn+gevent | 75 |
Django3.1+DRF | gunicorn+gevent wsgi | 95 |
Django3.1+DRF | uvicorn asgi | 181 |
Flask+SQLAlchemy | gunicorn+gevent | 210 |
SpringBoot | java -jar | 343 |
FastApi+Tortoise ORM | uvicorn asgi | 568 |
Go(Gin框架) | ./bubble conf/config.ini | 3984.92 |
测试数据
Django2.0+DRF
Django+DjangoRestFramework核心代码;
def retrieve(self, request, *args, **kwargs):
instance = self.get_object()
serializer = TodoSerializer(instance, context=self.get_serializer_context())
return success_response(serializer.data)
Django+gunicorn+gevent 资源详情接口
[root@localhost opt]# wrk -t 8 -c 100 -d 120 --latency http://192.168.11.5:6000/api/todo/f83e3a9c-2650-4beb-b1f1-a2a93cd13175/
Running 2m test @ http://192.168.11.5:6000/api/todo/f83e3a9c-2650-4beb-b1f1-a2a93cd13175/
8 threads and 100 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 25.28ms 120.30ms 1.95s 98.89%
Req/Sec 64.00 24.27 90.00 79.69%
Latency Distribution
50% 12.36ms
75% 13.36ms
90% 23.06ms
99% 258.84ms
8977 requests in 2.00m, 5.85MB read
Socket errors: connect 0, read 492, write 0, timeout 718
Requests/sec: 74.78
Transfer/sec: 49.88KB
Top数据
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
4619 mysql 20 0 1189752 295300 8732 S 34.9 15.9 0:08.83 mysqld
4966 root 20 0 384904 66672 6732 R 32.2 3.6 0:08.28 gunicorn
4967 root 20 0 385156 66908 6724 R 32.2 3.6 0:08.60 gunicorn
Flask
Flask核心代码
def retrieve():
log.info(f'调用/retrieve接口')
_id = request.args.get('id')
serializer = CookieSerializer(cookie)
return return_template(code=1, msg='success', result=serializer.data))
Flask+gunicorn+gevent 资源详情接口
[root@localhost opt]# wrk -t 8 -c 100 -d 120 --latency "http://192.168.11.5:6000/cookies/retrieve?cookie_id=2c1ec53e-b4d2-4601-9298-7e608c8eb5d7&platform=0"
Running 2m test @ http://192.168.11.5:6000/cookies/retrieve?cookie_id=2c1ec53e-b4d2-4601-9298-7e608c8eb5d7&platform=0
8 threads and 100 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 454.27ms 70.46ms 763.42ms 69.37%
Req/Sec 26.99 13.76 110.00 69.20%
Latency Distribution
50% 445.85ms
75% 493.55ms
90% 547.84ms
99% 661.73ms
25309 requests in 2.00m, 18.60MB read
Requests/sec: 210.81
Transfer/sec: 158.67KB
Top
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
5290 root 20 0 949312 82068 3896 R 47.4 4.4 0:37.43 gunicorn
5289 root 20 0 944252 75156 3876 R 46.7 4.0 0:36.86 gunicorn
4619 mysql 20 0 1211500 303244 8948 S 4.3 16.3 0:44.21 mysqld
SpringBoot
wrk测试结果:
[root@localhost opt]# wrk -t 8 -c 500 -d 30 --latency http://192.168.11.5:8809/api/registerCenter/page
Running 30s test @ http://192.168.11.5:8809/api/registerCenter/page
8 threads and 500 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 1.25s 287.88ms 2.00s 80.07%
Req/Sec 51.77 40.90 386.00 73.08%
Latency Distribution
50% 1.27s
75% 1.39s
90% 1.55s
99% 1.95s
10334 requests in 30.06s, 5.11MB read
Socket errors: connect 0, read 0, write 0, timeout 857
Requests/sec: 343.77
Transfer/sec: 174.24KB
TOP数据
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6161 root 20 0 3133868 521532 14068 S 90.2 28.0 2:08.70 java
4619 mysql 20 0 1191436 302916 9100 S 8.2 16.3 1:01.23 mysqld
Django3.1
django3.1+gunicorn wsgi
[root@localhost ~]# wrk -t 8 -c 500 -d 30 --latency http://192.168.11.5:8000/api/command/1/
Running 30s test @ http://192.168.11.5:8000/api/command/1/
8 threads and 500 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 24.08ms 66.73ms 1.94s 99.56%
Req/Sec 48.30 9.63 108.00 87.76%
Latency Distribution
50% 20.11ms
75% 20.95ms
90% 23.99ms
99% 35.17ms
2877 requests in 30.07s, 1.59MB read
Socket errors: connect 0, read 408, write 0, timeout 142
Requests/sec: 95.67
Transfer/sec: 54.00KB
django3.1+uvicorn asgi
[root@localhost ~]# wrk -t 8 -c 500 -d 30 --latency http://192.168.11.5:8000/api/command/1/
Running 30s test @ http://192.168.11.5:8000/api/command/1/
8 threads and 500 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 1.80s 12.53ms 1.82s 95.62%
Req/Sec 159.07 231.52 610.00 75.42%
Latency Distribution
50% 1.81s
75% 1.81s
90% 1.81s
99% 1.81s
5456 requests in 30.04s, 2.84MB read
Socket errors: connect 0, read 0, write 0, timeout 5205
Requests/sec: 181.63
Transfer/sec: 96.85KB
FastAPI
Fastapi+uvicorn+Tortoise ORM
[root@localhost ~]# wrk -t 8 -c 500 -d 30 --latency http://192.168.11.5:8000/client/1
Running 30s test @ http://192.168.11.5:8000/client/1
8 threads and 500 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 751.06ms 526.96ms 1.95s 59.53%
Req/Sec 112.46 107.55 650.00 78.82%
Latency Distribution
50% 848.00ms
75% 934.46ms
90% 1.65s
99% 1.82s
17120 requests in 30.09s, 8.72MB read
Socket errors: connect 0, read 0, write 0, timeout 860
Requests/sec: 568.93
Transfer/sec: 296.69KB
TOP数据
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
28082 root 20 0 380140 56708 8628 R 87.3 3.0 1:52.78 uvicorn
1132 mysql 20 0 1159764 254644 9260 S 11.0 13.7 1:14.19 mysqld
Go(Gin框架)
Wrk测试结果
[root@localhost wrk]# wrk -t 8 -c 500 -d 30 --latency http://192.168.11.5:9000/v1/todo
Running 30s test @ http://192.168.11.5:9000/v1/todo
8 threads and 500 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 201.99ms 281.57ms 1.65s 85.82%
Req/Sec 501.23 97.01 0.88k 69.65%
Latency Distribution
50% 89.46ms
75% 172.46ms
90% 698.35ms
99% 1.13s
119909 requests in 30.09s, 84.16MB read
Socket errors: connect 0, read 0, write 0, timeout 516
Requests/sec: 3984.92
Transfer/sec: 2.80MB
TOP数据