Roy-Try-Catch
← Back to list

【Laravel】Laravel Octane With Swoole 體驗

Roy • Updated 2026-10-15 14:52:34

更新提醒(2026-10):本文寫於 2023 年,Octane 與 Swoole 的版本、安裝方式與壓測數據都是當時環境的結果,不同硬體與版本的數字可能差異很大。較新的實測可參考 【效能】k6 壓測 Laravel Octane:找出 2 核 VM 的真實上限。

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 是否在其中,確認後重新執行安裝
php artisan octane:install
啟動 Octane 時可以指定 host 與 port,不填則使用預設值
php artisan octane:start --host={host} --port={port}
Laravel 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_快取間隔

延伸閱讀

Comments

No comments yet.

請先登入