1. 云栖社区>
  2. PHP教程>
  3. 正文

swoole+phalcon/yaf性能测试 swoole_http_server+nginx反向代理配置优化

作者:用户 来源:互联网 时间:2017-11-30 17:01:51

测试nginx优化server性能配置代理反向swoolephalcon

swoole+phalcon/yaf性能测试 swoole_http_server+nginx反向代理配置优化 - 摘要: 本文讲的是swoole+phalcon/yaf性能测试 swoole_http_server+nginx反向代理配置优化,swoole性能如何 根据swoole官网和自己的测试来看,裸swoole和裸php-fpm+nginx的ab基准测试来看,swoole是php-fpm的2~4倍。 本文ab测试语句 ab -c 200 -n 100000 http://t

swoole性能如何

根据swoole官网和自己的测试来看,裸swoole和裸php-fpm+nginx的ab基准测试来看,swoole是php-fpm的2~4倍。


本文ab测试语句


ab -c 200 -n 100000 http://tsf.com/ #替换为测试的服务
生产应用有所不同

但是,实际情况并不是那么乐观,就一个安全和易维护的web程序来说,一个完备的路由组件是不可缺少的,这里我们选用了phalcon框架。swoole_http_server+phalcon基准测试和nginx+php-fpm+phalcon的将近持平,并没有太大优势。(这里只使用了phalcon的路由、应用、volt模板服务,没有进行任何数据库操作),这个结果让我很惊讶(是我姿势不对?如果是,请指教)。而swoole官方表示对http协议的支持还不是很完善,建议使用nginx进行反向代理,这进一步拉低了swoole的测试结果,结果只有nginx+php-fpm+phalcon的6成左右。下面给出大概的结果,详细结果会在文章后面贴出。因为测试程序并不复杂,启用opcache的效果并不明显,不再贴出启用opcache的结果。



测试环境
测试结果


nginx+php
Requests per second: 8182.15 [#/sec]


swoole_http_server
Requests per second: 41849.30 [#/sec]


nginx+phalcon
Requests per second: 7219.17 [#/sec]


swoole+phalcon
Requests per second: 7825.38 [#/sec]


swoole+yaf
Requests per second: 19321.51 [#/sec]


nginx+swoole+phalcon
Requests per second: 4329.06 [#/sec]


nginx+swoole+yaf
Requests per second: 6952.89 [#/sec]

对测试结果的分析

在没有进行优化之前,swoole_http_server的实际应用并发性能并没有什么优势。而相比phalcon来说,yaf+swoole的组合可以获得更佳的性能(看phalcon的路由组件源码,可是料到这个结果)。而在用nginx反向代理swoole应用,性能会进一步下降,被nginx+php-fpm+phalcon/yaf甩出几条街。


但是swoole依然有着数据库连接池、异步任务、异步网络客户端等不可跨越的优势和便利。而在实际应用中,另一个真正影响性能的大户,是数据库操作。swoole的连接池在方面有巨大优势(nginx+php-fpm可以使用奇虎360的数据库中间件实现)。所以,大叔决定,优化nginx配置,再看看结果。


针对swoole优化nginx反向代理设置

原理其实很简单,对nginx反向代理进行缓存,提升性能。再次要说的是,swoole官网给出的swoole nginx反向代理配置,跑不起来(首页无法反向代理)。。本文读者请根据本文进行配置。


首先需要编辑nginx配置文件

编辑/usr/local/nginx/conf/nginx.conf添加或修改以下内容:


proxy_temp_path /data/ngx_cache 1 2;
#keys_zone=cache1:100m 表示这个zone名称为cache1,分配的内存大小为100MB
#/data/ngx_cache/cache1 表示cache1这个zone的文件要存放的目录
#levels=1:2 表示缓存目录的第一级目录是1个字符,第二级目录是2个字符,即/data/ngx_cache/cache1/a/1b这种形式
#inactive=1d 表示这个zone中的缓存文件如果在1天内都没有被访问,那么文件会被cache manager进程删除掉
#max_size=10g 表示这个zone的硬盘容量为10GB
proxy_cache_path /data/ngx_cache/cache1 levels=1:2 keys_zone=cache1:100m inactive=1d max_size=10g;
编辑nginx反向代理swoole配置文件

以lnmp一建安装包php运行环境为例。正常添加网站,伪静态规则设置为swoole.conf。然后编辑/usr/local/nginx/config/swoole.conf内容如下:


location / {
if (-f $request_filename/index.html){
proxy_pass http://127.0.0.1:9501;
}
if (-f $request_filename/index.php){
proxy_pass http://127.0.0.1:9501;
}
if (!-f $request_filename){
proxy_pass http://127.0.0.1:9501;
}
#替换成自己设置的swoole监听地址和端口
proxy_next_upstream http_502 http_504 error timeout invalid_header;
#讲这些错误类型返回nginx处理
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
#access_log off;
#设置资源缓存的zone
proxy_cache cache1;
#设置缓存的key
proxy_cache_key $host$uri$is_args$args;
#设置状态码为200和304的响应可以进行缓存,并且缓存时间为30分钟
proxy_cache_valid 200 304 30m;
expires 1d;
#$upstream_cache_status表示资源缓存的状态,有HIT MISS EXPIRED三种状态
add_header X-Cache $upstream_cache_status;
}
#或者不开启缓存
location / {
   proxy_next_upstream http_502 http_504 error timeout invalid_header;
#讲这些错误类型返回nginx处理
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;

if (-f $request_filename/index.html){
proxy_pass http://127.0.0.1:9501;
}
if (-f $request_filename/index.php){
proxy_pass http://127.0.0.1:9501;
}
if (!-f $request_filename){
proxy_pass http://127.0.0.1:9501;
}
}
优化后的结果

测试环境
优化后结果
优化前结果


nginx+swoole+phalcon
Requests per second: 12119.43 [#/sec]
4329.06 [#/sec]


nginx+swoole+yaf
Requests per second: 12075.86 [#/sec]
6952.89 [#/sec]

ab测试结果有很大的提升,相比裸swoole+phalcon有很大提升,而且swoole+phalcon+nginx的测试结果超过了swoole+yaf+nginx,这种情况下,phalcon功能更强大,当然选择swoole+phalcon+nginx的组合了。


下面贴出详细ab测试结果
swoole_http_server
ab -c 200 -n 100000 -k http://127.0.0.1:9501/
.......................
Server Software: swoole-http-server
Server Hostname: 127.0.0.1
Server Port: 9501
Document Path: /
Document Length: 28 bytes
Concurrency Level:200
Time taken for tests: 2.390 seconds
Complete requests:100000
Failed requests: 0
Keep-Alive requests:100000
Total transferred:18100000 bytes
HTML transferred:2800000 bytes
Requests per second:41849.30 [#/sec] (mean)
Time per request:4.779 [ms] (mean)
Time per request:0.024 [ms] (mean, across all concurrent requests)
Transfer rate: 7397.19 [Kbytes/sec] received
Connection Times (ms)
minmean[+/-sd] median max
Connect: 00 0.5016
Processing: 05 3.1438
Waiting: 05 3.1438
Total: 05 3.2438
.....................
swoole+phalcon
ab -c 200 -n 100000 -k http://127.0.0.1:9501/
............................
Server Software: swoole-http-server
Server Hostname: 127.0.0.1
Server Port: 9501
Document Path: /
Document Length: 23 bytes
Concurrency Level:200
Time taken for tests: 12.779 seconds
Complete requests:100000
Failed requests: 0
Keep-Alive requests:100000
Total transferred:17600000 bytes
HTML transferred:2300000 bytes
Requests per second:7825.38 [#/sec] (mean)
Time per request:25.558 [ms] (mean)
Time per request:0.128 [ms] (mean, across all concurrent requests)
Transfer rate: 1344.99 [Kbytes/sec] received
Connection Times (ms)
minmean[+/-sd] median max
Connect: 00 0.7021
Processing: 1 2510.5 24 166
Waiting: 1 2510.5 24 166
Total: 1 2610.5 24 166
................
swoole+yaf
ab -c 200 -n 100000 -k http://127.0.0.1:9501/
....................
Server Software: swoole-http-server
Server Hostname: 127.0.0.1
Server Port: 9501
Document Path: /
Document Length: 342 bytes
Concurrency Level:200
Time taken for tests: 5.176 seconds
Complete requests:100000
Failed requests: 0
Keep-Alive requests:100000
Total transferred:49600000 bytes
HTML transferred:34200000 bytes
Requests per second:19321.51 [#/sec] (mean)
Time per request:10.351 [ms] (mean)
Time per request:0.052 [ms] (mean, across all concurrent requests)
Transfer rate: 9358.85 [Kbytes/sec] received
Connection Times (ms)
minmean[+/-sd] median max
Connect: 0122.401002
Processing: 0 1010.86 208
Waiting: 0 1010.86 208
Total: 0 1024.961041
...................
nginx+swoole+phalcon未优化前结果
ab -c 200 -n 100000 http://tsf.com/
....................
Server Software: nginx
Server Hostname: tsf.com
Server Port: 80
Document Path: /
Document Length: 23 bytes
Concurrency Level:200
Time taken for tests: 23.100 seconds
Complete requests:100000
Failed requests: 0
Total transferred:15800000 bytes
HTML transferred:2300000 bytes
Requests per second:4329.06 [#/sec] (mean)
Time per request:46.199 [ms] (mean)
Time per request:0.231 [ms] (mean, across all concurrent requests)
Transfer rate: 667.96 [Kbytes/sec] received
Connection Times (ms)
minmean[+/-sd] median max
Connect: 01 1.4021
Processing: 6 4519.7 421042
Waiting: 5 4519.7 411041
Total: 9 4619.6 421042
.................
nginx+swoole+yaf未优化前
ab -c 200 -n 100000 http://tsf.com/
.......
Server Software: nginx
Server Hostname: tsf.com
Server Port: 80
Document Path: /
Document Length: 342 bytes
Concurrency Level:200
Time taken for tests: 14.383 seconds
Complete requests:100000
Failed requests: 0
Total transferred:47800000 bytes
HTML transferred:34200000 bytes
Requests per second:6952.89 [#/sec] (mean)
Time per request:28.765 [ms] (mean)
Time per request:0.144 [ms] (mean, across all concurrent requests)
Transfer rate: 3245.59 [Kbytes/sec] received
Connection Times (ms)
minmean[+/-sd] median max
Connect: 02 2.2122
Processing: 0 2737.6 231102
Waiting: 0 2637.6 221092
Total: 0 2937.8 251103
......
nginx+swoole+phalcon配置优化后
ab -c 200 -n 100000 http://tsf.com/
...........
Server Software: nginx
Server Hostname: tsf.com
Server Port: 80
Document Path: /
Document Length: 23 bytes
Concurrency Level:200
Time taken for tests: 8.251 seconds
Complete requests:100000
Failed requests: 0
Total transferred:24200072 bytes
HTML transferred:2300000 bytes
Requests per second:12119.43 [#/sec] (mean)
Time per request:16.502 [ms] (mean)
Time per request:0.083 [ms] (mean, across all concurrent requests)
Transfer rate: 2864.17 [Kbytes/sec] received
Connection Times (ms)
minmean[+/-sd] median max
Connect: 07 1.5721
Processing: 29 2.1947
Waiting: 27 2.1740
Total: 9 16 1.9 1667
.............
nginx+swoole+yaf配置优化后
ab -c 200 -n 100000 http://tsf.com/
................
Server Software: nginx
Server Hostname: tsf.com
Server Port: 80
Document Path: /
Document Length: 23 bytes
Concurrency Level:200
Time taken for tests: 8.281 seconds
Complete requests:100000
Failed requests: 0
Total transferred:24200000 bytes
HTML transferred:2300000 bytes
Requests per second:12075.86 [#/sec] (mean)
Time per request:16.562 [ms] (mean)
Time per request:0.083 [ms] (mean, across all concurrent requests)
Transfer rate: 2853.86 [Kbytes/sec] received
Connection Times (ms)
minmean[+/-sd] median max
Connect: 27 1.6724
Processing: 29 2.2927
Waiting: 17 2.1724
Total: 5 17 2.2 1741
............

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索测试 , nginx , 优化 , server , 性能 , 配置 , 代理 , 反向 , swoole phalcon ,以便于您获取更多的相关知识。