测试Octane
+Swoole
晋升水平
硬件:虚拟机VirtualBox, 1核2G, CPU为i5-8400;
零碎: Centos7 + 宝塔;
php环境: 启用opcache
, session
启用memcached
缓存,文件缓存启用redis
;
部署我的项目及引入octane
composer create-project laravel/laravel octance.swcd ./octance.sw## 引入octanecomposer require laravel/octane## 装置octanephp artisan octane:install
装置这一步应用RoadRunner
出了很多问题,最初跑起来也没能失常拜访,遂放弃应用RR;
应用swoole
倒是非常简单,宝塔面板中装好swoole
扩大,octane:install
时选swoole
间接实现。
对laravel
进行一般的生产环境优化;
composer install --no-dev## 设置配置文件,关掉debugvi .envAPP_ENV=productionAPP_DEBUG=false## :wq保留## 执行优化composer dump-autoload -ophp 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/randomThis 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 requestsCompleted 400 requestsCompleted 600 requestsCompleted 800 requestsCompleted 1000 requestsCompleted 1200 requestsCompleted 1400 requestsCompleted 1600 requestsCompleted 1800 requestsCompleted 2000 requestsFinished 2000 requestsServer Software: nginxServer Hostname: octane.swServer Port: 80Document Path: /api/randomDocument Length: 30 bytesConcurrency Level: 8Time taken for tests: 17.194 secondsComplete requests: 2000Failed requests: 0Total transferred: 512000 bytesHTML transferred: 60000 bytesRequests 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) min mean[+/-sd] median maxConnect: 0 0 0.4 0 5Processing: 7 68 68.2 53 641Waiting: 7 68 68.2 53 641Total: 7 69 68.2 53 641Percentage 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/randomThis 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 requestsCompleted 400 requestsCompleted 600 requestsCompleted 800 requestsCompleted 1000 requestsCompleted 1200 requestsCompleted 1400 requestsCompleted 1600 requestsCompleted 1800 requestsCompleted 2000 requestsFinished 2000 requestsServer Software: nginxServer Hostname: octane.swServer Port: 80Document Path: /api/randomDocument Length: 30 bytesConcurrency Level: 8Time taken for tests: 5.234 secondsComplete requests: 2000Failed requests: 0Total transferred: 592000 bytesHTML transferred: 60000 bytesRequests 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) min mean[+/-sd] median maxConnect: 0 0 0.7 0 11Processing: 4 21 12.4 18 118Waiting: 3 21 12.4 18 118Total: 4 21 12.5 18 118Percentage 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
的压测的QPS为380+
论断:QPS从110+晋升到380+,晋升幅度达到220%+,晋升很显著,但却低于冀望。官网所声称的QPS高达2000+,显著不能间接套用于生产环境。同时,如果按晋升程序来算,重构代码只是为了200%的晋升是否值得也须要好好考虑一下。