Octane+Swoole提升程度测试
测试Octane
+Swoole
提升程度
写在前面
laravel项目使用octane
+swoole
对于性能的提升非常显著(220%+的QPS提升),但还是难以与静态语言抗衡。
测试过程
硬件:虚拟机VirtualBox, 1核2G, CPU为i5-8400;
系统: Centos7 + 宝塔;
php环境: 启用opcache
, session
启用memcached
缓存,文件缓存启用redis
;
部署项目及引入octane
composer create-project laravel/laravel octance.sw
cd ./octance.sw
## 引入octane
composer require laravel/octane
## 安装octane
php artisan octane:install
安装这一步使用
RoadRunner
出了很多问题,最后跑起来也没能正常访问,遂放弃使用RR;
使用
swoole
倒是非常简单,宝塔面板中装好swoole
扩展,octane:install
时选swoole
直接完成。对
laravel
进行普通的生产环境优化;composer install --no-dev## 设置配置文件,关掉debug
vi .env
APP_ENV=production
APP_DEBUG=false
## :wq保存
## 执行优化
composer dump-autoload -o
php artisan optimize
到中件间文件
app/Http/Kernel.php
中注释掉api
下的throttle
中间件,以免ab
压测时报429的错误测试以下内容:在控制器中返回一个0-100的随机数,控制器代码如下
function random()
{
$i = mt_rand(0, 100);
return response()->json([
'code' => 0,
'msg' => 'random: ' . str_pad($i, 3, '0', STR_PAD_LEFT)
]);
}
1, 压测
php-fpm
配置Nginx转发到php-fpm;然后进行
ab
压测,一次压测结果如下: .\ab -n2000 -c8 http://octane.sw/api/random
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking octane.sw (be patient)
Completed 200 requests
Completed 400 requests
Completed 600 requests
Completed 800 requests
Completed 1000 requests
Completed 1200 requests
Completed 1400 requests
Completed 1600 requests
Completed 1800 requests
Completed 2000 requests
Finished 2000 requestsServer Software:nginx
Server Hostname:octane.sw
Server Port:80Document Path:/api/random
Document Length:30 bytesConcurrency Level:8
Time taken for tests:17.194 seconds
Complete requests:2000
Failed requests:0
Total transferred:512000 bytes
HTML transferred:60000 bytes
Requests per second:116.32 [#/sec] (mean)
Time per request:68.777 [ms] (mean)
Time per request:8.597 [ms] (mean, across all concurrent requests)
Transfer rate:29.08 [Kbytes/sec] receivedConnection Times (ms)
minmean[+/-sd] medianmax
Connect:000.405
Processing:76868.253641
Waiting:76868.253641
Total:76968.253641Percentage of the requests served within a certain time (ms)
50%53
66%59
75%61
80%64
90%95
95%243
98%303
99%346
100%641 (longest request)
测试结果可见,普通优化下,php8的
php-fpm
能提供110+的QPS;
2, 压测
octane
+swoole
启动工作进程:
php /path/octane.sw/artisan octane:start --host="0.0.0.0" --port=8080 --workers=4 --max-requests=10000 --task-workers=10
并加入到
Supervisor
进程守护中;配置nginx,将所有请求都转发到
127.0.0.1:8080
进行
ab
压测,一次压测结果如下: .\ab -n2000 -c8 http://octane.sw/api/random
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking octane.sw (be patient)
Completed 200 requests
Completed 400 requests
Completed 600 requests
Completed 800 requests
Completed 1000 requests
Completed 1200 requests
Completed 1400 requests
Completed 1600 requests
Completed 1800 requests
Completed 2000 requests
Finished 2000 requestsServer Software:nginx
Server Hostname:octane.sw
Server Port:80Document Path:/api/random
Document Length:30 bytesConcurrency Level:8
Time taken for tests:5.234 seconds
Complete requests:2000
Failed requests:0
Total transferred:592000 bytes
HTML transferred:60000 bytes
Requests per second:382.10 [#/sec] (mean)
Time per request:20.937 [ms] (mean)
Time per request:2.617 [ms] (mean, across all concurrent requests)
Transfer rate:110.45 [Kbytes/sec] receivedConnection Times (ms)
minmean[+/-sd] medianmax
Connect:000.7011
Processing:42112.418118
Waiting:32112.418118
Total:42112.518118Percentage of the requests served within a certain time (ms)
50%18
66%21
75%23
80%25
90%32
95%43
98%65
99%76
100%118 (longest request)
【Octane+Swoole提升程度测试】对
octane
+swoole
的压测的QPS为380+结论:QPS从110+提升到380+,提升幅度达到220%+,提升很明显,但却低于期望。官方所宣称的QPS高达2000+,明显不能直接套用于生产环境。同时,如果按QPS提升程度来算,重构代码只是为了200%+的提升是否值得也需要好好考虑一下。
推荐阅读
- 【#2-戴栋】区块链可以提升哪些商业上的效率改进
- 父亲提升孩子的自信
- 三招让你提升幸福感
- #你和你最好的朋友能好到什么程度#
- 如何转录逐字稿(|如何转录逐字稿?| 提升咨询技能的必备神器)
- 2020-02-24|2020-02-24 7个C提升孩子的抗挫力
- Python爬虫技术要学到什么程度才可以找到工作()
- 爱育幼童提升孩子生活品质day29~20181221
- 成功通航(用宜搭提升数字化管理效能,确保每次飞行任务安全执行)
- 怎么样提升您的业务在本地搜索可见性(可以通过提高本地SEO来实现)