背景介绍
要理解swoole加速框架的原理,原因是传统php-fpm的工作模式及缺点造成。php-fpm是管理php-cgi的进程管理器,在每一次请求过来,都需要加载一次PHP的全部运行文件,处理业务,请求结束后就会释放内存,无法做连接池。这样一旦访问量多了,需要的系统资源开销很大,系统很可能承受不住。
而swoole是长驻内存的模式,长驻内存一个最大的好处就是可以性能加速,原因是内存的效率远高于磁盘。简单来说就是之前是每次请求进来都需要加载一遍PHP运行文件,而现在只需要在第一次请求进来时加载这些文件,然后驻留内存中,之后再进来的请求都可以直接复用内存中的内容。
安装swoole扩展不介绍,
1、安装laravel-swoole
composer require swooletw/laravel-swoole
3、在config/app.php的数组providers增加
SwooleTW\Http\LaravelServiceProvider::class
2、安装配置
修改端口9501
php artisan vendor:publish --tag=laravel-swoole
3、nginx反向代理
把url的请求转发到swoole服务地址127.0.0.1:9501
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server
{
listen 80;
server_name www.xxx.com ;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/www.xxx.com/public;
location = /index.php {
try_files /not_exists @swoole;
}
location / {
try_files $uri $uri/ @swoole;
}
location @swoole {
set $suffix "";
if ($uri = /index.php) {
set $suffix ?$query_string;
}
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
proxy_set_header SERVER_PORT $server_port;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_pass http://127.0.0.1:9503$suffix;
}
}
4、启动服务器
php artisan swoole:http start
5、配置说明
6、压力测试
工具abs -n 1000 -c 100 https://www.xxx.com
php-fpm模式
Concurrency Level: 100
Time taken for tests: 57.148 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 836000 bytes
HTML transferred: 202000 bytes
Requests per second: 17.50 [#/sec] (mean)
Time per request: 5714.813 [ms] (mean)
Time per request: 57.148 [ms] (mean, across all concurrent requests)
Transfer rate: 14.29 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 15 89 209.0 29 1203
Processing: 1334 5370 858.6 5542 7048
Waiting: 148 5367 866.4 5541 7047
Total: 1368 5459 765.3 5594 7405
Percentage of the requests served within a certain time (ms)
50% 5594
66% 5814
75% 5916
80% 5999
90% 6201
95% 6364
98% 6653
99% 6911
100% 7405 (longest request)
swoole模式测试
Concurrency Level: 100
Time taken for tests: 14.013 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 620000 bytes
HTML transferred: 202000 bytes
Requests per second: 71.36 [#/sec] (mean)
Time per request: 1401.349 [ms] (mean)
Time per request: 14.013 [ms] (mean, across all concurrent requests)
Transfer rate: 43.21 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 22 526 240.8 520 1233
Processing: 38 798 263.0 801 1349
Waiting: 23 570 200.4 651 1115
Total: 543 1324 125.8 1342 1455
Percentage of the requests served within a certain time (ms)
50% 1342
66% 1367
75% 1382
80% 1391
90% 1412
95% 1426
98% 1435
99% 1443
100% 1455 (longest request)