0
user-people-family-house-home

【Laravel】Laravel Octane With Swoole 體驗

Laravel Octane;中文文檔官方文檔為何要使用Swoole?這個問題很好,因為Laravel框架的boot 時...

Posted by Roy on 2023-01-07 09:14:17 Views

Laravel Octane 
中文文檔
官方文檔

為何要使用Swoole?

這個問題很好,因為Laravel框架的boot 時間可能比業務處理時間還長,並且隨著項目第三方service provider 的增多,其啟動速度越來越不受控。而Laravel Octane 則通過啟動Application 一次,常駐內存的方式來加速我們的應用。

Composer 安裝套件

composer require laravel/octane

利用php artisan 安裝 octane

php artisan octane:install
將會顯示以下選擇roadrunner & swoole
➜ php artisan octane:install

 Which application server you would like to use?:
  [0] roadrunner
  [1] swoole
 
這裡選擇1 Swoole
可能會發生錯誤,需要先安裝php-swoole套件
pecl install swoole
修改php.ini
extension=swoole.so
利用 php -m 查看 swoole 是否在其中,且重新安裝swoole
php artisan octane:install
啟用octane 可選擇host以及port 預設不填
php artisan octane:start --host={host} --port={port}
Larave Octane表現 (已自訂義Middleware)
Server Software:        swoole-http-server
Server Hostname:        127.0.0.1
Server Port:            8000

Document Path:          /test
Document Length:        20 bytes

Concurrency Level:      8
Time taken for tests:   5.545 seconds
Complete requests:      5000
Failed requests:        0
Total transferred:      1315000 bytes
HTML transferred:       100000 bytes
Requests per second:    901.71 [#/sec] (mean)
Time per request:       8.872 [ms] (mean)
Time per request:       1.109 [ms] (mean, across all concurrent requests)
Transfer rate:          231.59 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       3
Processing:     2    9  13.7      8     218
Waiting:        2    9  13.7      7     218
Total:          2    9  13.7      8     218

Percentage of the requests served within a certain time (ms)
  50%      8
  66%      8
  75%      8
  80%      8
  90%      9
  95%      9
  98%     10
  99%     13
 100%    218 (longest request)
未安裝Laravel Octane
Server Software:        
Server Hostname:        127.0.0.1
Server Port:            8001

Document Path:          /test
Document Length:        10 bytes

Concurrency Level:      8
Time taken for tests:   13.774 seconds
Complete requests:      5000
Failed requests:        0
Total transferred:      1460000 bytes
HTML transferred:       50000 bytes
Requests per second:    363.01 [#/sec] (mean)
Time per request:       22.038 [ms] (mean)
Time per request:       2.755 [ms] (mean, across all concurrent requests)
Transfer rate:          103.51 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       5
Processing:     4   22   2.0     21      38
Waiting:        4   22   1.9     21      38
Total:          5   22   2.0     21      38

Percentage of the requests served within a certain time (ms)
  50%     21
  66%     22
  75%     22
  80%     23
  90%     24
  95%     25
  98%     27
  99%     30
 100%     38 (longest request)

以上相同程式碼相同的vm中的比較中可以發現swoole大概快了三倍

利用Nginx反向代理

Octane Nginx 設定範例

並行任務

Octane 並行任務

計時及間隔

Swoole_計時及間隔

緩存間隔

Octane_緩存間隔

View Comments