Nginx|Nginx 和 Gunicorn 性能对比测试

前言 测试平台介绍 Nginx|Nginx 和 Gunicorn 性能对比测试
文章图片

压力测试工具介绍 使用名为 wrk 的压力测试工具
【Nginx|Nginx 和 Gunicorn 性能对比测试】参考文章:
ubuntu20.04 安装 wrk 压力测试工具以及简单使用
纯 Nginx 准备工作 安装 Nginx

sudo apt install nginx

替换默认的介绍页
location / { #设置content type default_type text/html ; # HTTP Status Code 和 内容 return 200"hello world! "; }

【Nginx】输出/返回 HelloWorld
重启 Nginx
sudo service nginx restart

查看 Nginx 状态:
─$ sudo service nginx status ● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: active (running) since Tue 2022-01-04 22:44:13 CST; 1s ago Docs: man:nginx(8) Process: 11215 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0> Process: 11216 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 11217 (nginx) Tasks: 17 (limit: 3391) Memory: 17.6M CPU: 95ms CGroup: /system.slice/nginx.service ├─11217 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on; " ├─11218 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "> ├─11219 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "> ├─11220 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "> ├─11221 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "> ├─11222 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "> ├─11223 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "> ├─11224 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "> ├─11226 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "> ├─11227 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "> ├─11228 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "> ├─11229 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "> ├─11230 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "> ├─11231 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "> ├─11232 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "> ├─11233 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "> └─11234 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ">1月 04 22:44:13 kali systemd[1]: Starting A high performance web server and a reverse proxy server... 1月 04 22:44:13 kali systemd[1]: Started A high performance web server and a reverse proxy server.

开始测试 测试1
wrk http://192.168.31.95 -t 16 -c 64 -d 10

测试结果
─?./wrk http://192.168.31.95-t16 -c64-d 10 Running 10s test @ http://192.168.31.95 16 threads and 64 connections Thread StatsAvgStdevMax+/- Stdev Latency5.85ms3.72ms48.92ms82.33% Req/Sec708.88348.741.48k67.47% 113734 requests in 10.09s, 17.35MB read Requests/sec:11271.96 Transfer/sec:1.72MB

测试2
wrk http://192.168.31.95-t32 -c160-d 10

测试结果
─?./wrk http://192.168.31.95-t32 -c160-d 10 Running 10s test @ http://192.168.31.95 32 threads and 160 connections Thread StatsAvgStdevMax+/- Stdev Latency8.00ms6.31ms69.27ms84.35% Req/Sec701.97395.331.29k47.11% 225052 requests in 10.07s, 34.33MB read Requests/sec:22349.79 Transfer/sec:3.41MB

测试3
wrk http://192.168.31.95-t64 -c320-d 10

测试结果
─?./wrk http://192.168.31.95-t64 -c320-d 10 Running 10s test @ http://192.168.31.95 64 threads and 320 connections Thread StatsAvgStdevMax+/- Stdev Latency14.08ms9.75ms84.81ms87.53% Req/Sec384.02157.251.03k66.21% 246408 requests in 10.07s, 37.59MB read Requests/sec:24458.27 Transfer/sec:3.73MB

纯 Gunicoen
from flask import Flask app = Flask(__name__)@app.route('/', methods=['GET']) def home(): success: bool = False return { 'status': success }@app.route('/upload/', methods=['POST']) def hello(): success: bool = False return { 'status': success }

纯多进程模式
import multiprocessingbind = "0.0.0.0:63000" workers = 32

运行 Gunicorn
─$ gunicorn fapi:app -c gunicorn.conf.py [2022-01-04 22:53:08 +0800] [11519] [INFO] Starting gunicorn 20.1.0 [2022-01-04 22:53:09 +0800] [11519] [INFO] Listening at: http://0.0.0.0:63000 (11519) [2022-01-04 22:53:09 +0800] [11519] [INFO] Using worker: sync [2022-01-04 22:53:09 +0800] [11520] [INFO] Booting worker with pid: 11520 [2022-01-04 22:53:09 +0800] [11521] [INFO] Booting worker with pid: 11521 [2022-01-04 22:53:09 +0800] [11522] [INFO] Booting worker with pid: 11522 [2022-01-04 22:53:09 +0800] [11523] [INFO] Booting worker with pid: 11523 [2022-01-04 22:53:09 +0800] [11524] [INFO] Booting worker with pid: 11524 [2022-01-04 22:53:09 +0800] [11525] [INFO] Booting worker with pid: 11525 [2022-01-04 22:53:09 +0800] [11526] [INFO] Booting worker with pid: 11526 [2022-01-04 22:53:09 +0800] [11527] [INFO] Booting worker with pid: 11527 [2022-01-04 22:53:09 +0800] [11528] [INFO] Booting worker with pid: 11528 [2022-01-04 22:53:09 +0800] [11529] [INFO] Booting worker with pid: 11529 [2022-01-04 22:53:09 +0800] [11530] [INFO] Booting worker with pid: 11530 [2022-01-04 22:53:09 +0800] [11531] [INFO] Booting worker with pid: 11531 [2022-01-04 22:53:09 +0800] [11532] [INFO] Booting worker with pid: 11532 [2022-01-04 22:53:09 +0800] [11533] [INFO] Booting worker with pid: 11533 [2022-01-04 22:53:09 +0800] [11534] [INFO] Booting worker with pid: 11534 [2022-01-04 22:53:09 +0800] [11535] [INFO] Booting worker with pid: 11535 [2022-01-04 22:53:09 +0800] [11536] [INFO] Booting worker with pid: 11536 [2022-01-04 22:53:09 +0800] [11537] [INFO] Booting worker with pid: 11537 [2022-01-04 22:53:10 +0800] [11538] [INFO] Booting worker with pid: 11538 [2022-01-04 22:53:10 +0800] [11539] [INFO] Booting worker with pid: 11539 [2022-01-04 22:53:10 +0800] [11540] [INFO] Booting worker with pid: 11540 [2022-01-04 22:53:10 +0800] [11541] [INFO] Booting worker with pid: 11541 [2022-01-04 22:53:10 +0800] [11542] [INFO] Booting worker with pid: 11542 [2022-01-04 22:53:10 +0800] [11543] [INFO] Booting worker with pid: 11543 [2022-01-04 22:53:10 +0800] [11544] [INFO] Booting worker with pid: 11544 [2022-01-04 22:53:10 +0800] [11545] [INFO] Booting worker with pid: 11545 [2022-01-04 22:53:10 +0800] [11546] [INFO] Booting worker with pid: 11546 [2022-01-04 22:53:10 +0800] [11547] [INFO] Booting worker with pid: 11547 [2022-01-04 22:53:10 +0800] [11548] [INFO] Booting worker with pid: 11548 [2022-01-04 22:53:10 +0800] [11549] [INFO] Booting worker with pid: 11549 [2022-01-04 22:53:10 +0800] [11550] [INFO] Booting worker with pid: 11550 [2022-01-04 22:53:10 +0800] [11551] [INFO] Booting worker with pid: 11551

进行压力测试
wrk http://192.168.31.95:63000-t64 -c320-d 10

测试结果
─?./wrk http://192.168.31.95:63000-t64 -c320-d 101 ? Running 10s test @ http://192.168.31.95:63000 64 threads and 320 connections Thread StatsAvgStdevMax+/- Stdev Latency76.09ms37.75ms 664.24ms71.68% Req/Sec33.7017.98191.0075.18% 21342 requests in 10.09s, 3.30MB read Requests/sec:2115.13 Transfer/sec:334.65KB

纯多线程模式
import multiprocessingbind = "0.0.0.0:63000" threads = 32

运行 Gunicorn
─$ gunicorn fapi:app -c gunicorn.conf.py [2022-01-04 22:57:38 +0800] [11607] [INFO] Starting gunicorn 20.1.0 [2022-01-04 22:57:38 +0800] [11607] [INFO] Listening at: http://0.0.0.0:63000 (11607) [2022-01-04 22:57:38 +0800] [11607] [INFO] Using worker: gthread [2022-01-04 22:57:38 +0800] [11608] [INFO] Booting worker with pid: 11608

进行压力测试
wrk http://192.168.31.95:63000-t64 -c320-d 10

测试结果
─?./wrk http://192.168.31.95:63000-t64 -c320-d 10 Running 10s test @ http://192.168.31.95:63000 64 threads and 320 connections Thread StatsAvgStdevMax+/- Stdev Latency122.28ms109.01ms2.00s98.58% Req/Sec42.3322.31303.0075.49% 8888 requests in 10.08s, 1.42MB read Socket errors: connect 0, read 0, write 0, timeout 73 Requests/sec:881.68 Transfer/sec:143.80KB

多线程和多进程混合模式 测试一:
import multiprocessingbind = "0.0.0.0:63000"workers = 8 threads = 8

运行 Gunicorn
─$ gunicorn fapi:app -c gunicorn.conf.py130 ? [2022-01-04 22:59:45 +0800] [11668] [INFO] Starting gunicorn 20.1.0 [2022-01-04 22:59:45 +0800] [11668] [INFO] Listening at: http://0.0.0.0:63000 (11668) [2022-01-04 22:59:45 +0800] [11668] [INFO] Using worker: gthread [2022-01-04 22:59:45 +0800] [11669] [INFO] Booting worker with pid: 11669 [2022-01-04 22:59:45 +0800] [11670] [INFO] Booting worker with pid: 11670 [2022-01-04 22:59:45 +0800] [11671] [INFO] Booting worker with pid: 11671 [2022-01-04 22:59:45 +0800] [11672] [INFO] Booting worker with pid: 11672 [2022-01-04 22:59:46 +0800] [11673] [INFO] Booting worker with pid: 11673 [2022-01-04 22:59:46 +0800] [11674] [INFO] Booting worker with pid: 11674 [2022-01-04 22:59:46 +0800] [11675] [INFO] Booting worker with pid: 11675 [2022-01-04 22:59:46 +0800] [11676] [INFO] Booting worker with pid: 11676

进行压力测试
wrk http://192.168.31.95:63000-t64 -c320-d 10

测试结果
─?./wrk http://192.168.31.95:63000-t64 -c320-d 10 Running 10s test @ http://192.168.31.95:63000 64 threads and 320 connections Thread StatsAvgStdevMax+/- Stdev Latency74.25ms24.12ms 304.79ms72.75% Req/Sec67.5317.83171.0077.02% 43368 requests in 10.10s, 6.91MB read Requests/sec:4293.70 Transfer/sec:700.34KB

测试二:
import multiprocessingbind = "0.0.0.0:63000"workers = 16 threads = 16

进行压力测试
wrk http://192.168.31.95:63000-t64 -c320-d 10

测试结果
─?./wrk http://192.168.31.95:63000-t64 -c320-d 10 Running 10s test @ http://192.168.31.95:63000 64 threads and 320 connections Thread StatsAvgStdevMax+/- Stdev Latency63.56ms32.04ms 242.04ms80.12% Req/Sec82.2729.26180.0069.46% 51927 requests in 10.08s, 8.27MB read Requests/sec:5150.42 Transfer/sec:840.00KB

测试三:
import multiprocessingbind = "0.0.0.0:63000"workers = 8 threads = 32

进行压力测试
wrk http://192.168.31.95:63000-t64 -c320-d 10

测试结果
─?./wrk http://192.168.31.95:63000-t64 -c320-d 10 Running 10s test @ http://192.168.31.95:63000 64 threads and 320 connections Thread StatsAvgStdevMax+/- Stdev Latency80.44ms31.75ms 515.60ms90.45% Req/Sec63.9116.65232.0087.49% 40686 requests in 10.07s, 6.48MB read Requests/sec:4039.57 Transfer/sec:658.99KB

测试四:
import multiprocessingbind = "0.0.0.0:63000"workers = 16 threads = 32

进行压力测试
wrk http://192.168.31.95:63000-t64 -c320-d 10

测试结果
─?./wrk http://192.168.31.95:63000-t64 -c320-d 10 Running 10s test @ http://192.168.31.95:63000 64 threads and 320 connections Thread StatsAvgStdevMax+/- Stdev Latency67.43ms35.35ms 529.30ms82.06% Req/Sec76.9327.15160.0074.54% 49099 requests in 10.10s, 7.82MB read Requests/sec:4861.03 Transfer/sec:792.80KB

Nginx 和 Gunicorn 混合模式

    推荐阅读