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)
min mean[+/-sd] median max
Connect: 0 0 0.5 0 16
Processing: 0 5 3.1 4 38
Waiting: 0 5 3.1 4 38
Total: 0 5 3.2 4 38
.....................
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)
min mean[+/-sd] median max
Connect: 0 0 0.7 0 21
Processing: 1 25 10.5 24 166
Waiting: 1 25 10.5 24 166
Total: 1 26 10.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)
min mean[+/-sd] median max
Connect: 0 1 22.4 0 1002
Processing: 0 10 10.8 6 208
Waiting: 0 10 10.8 6 208
Total: 0 10 24.9 6 1041
...................
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)
min mean[+/-sd] median max
Connect: 0 1 1.4 0 21
Processing: 6 45 19.7 42 1042
Waiting: 5 45 19.7 41 1041
Total: 9 46 19.6 42 1042
.................
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)
min mean[+/-sd] median max
Connect: 0 2 2.2 1 22
Processing: 0 27 37.6 23 1102
Waiting: 0 26 37.6 22 1092
Total: 0 29 37.8 25 1103
......
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)
min mean[+/-sd] median max
Connect: 0 7 1.5 7 21
Processing: 2 9 2.1 9 47
Waiting: 2 7 2.1 7 40
Total: 9 16 1.9 16 67
.............
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)
min mean[+/-sd] median max
Connect: 2 7 1.6 7 24
Processing: 2 9 2.2 9 27
Waiting: 1 7 2.1 7 24
Total: 5 17 2.2 17 41
............