乐趣区

关于laravel:OctaneSwoole提升程度测试

测试 Octane+Swoole 晋升水平

硬件:虚拟机 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 requests


Server Software:        nginx
Server Hostname:        octane.sw
Server Port:            80

Document Path:          /api/random
Document Length:        30 bytes

Concurrency 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] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.4      0       5
Processing:     7   68  68.2     53     641
Waiting:        7   68  68.2     53     641
Total:          7   69  68.2     53     641

Percentage 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 requests


Server Software:        nginx
Server Hostname:        octane.sw
Server Port:            80

Document Path:          /api/random
Document Length:        30 bytes

Concurrency 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] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.7      0      11
Processing:     4   21  12.4     18     118
Waiting:        3   21  12.4     18     118
Total:          4   21  12.5     18     118

Percentage 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% 的晋升是否值得也须要好好考虑一下。

退出移动版