RoadRunner是一個用Go語言實現的高效能PSR-7伺服器,你沒看錯,是Go語言實現的。
RoadRunner首先用Go實現了一個高效能的伺服器,然後通過goridge IPC把請求轉寄給多個PHP CLI Worker,Worker會把請求重新封裝成PSR-7 Request交給架構進行處理。
娛樂測試
使用siege測試Laravel的Welcome頁面,250個並發使用者,持續30秒,對比Nginx + FPM和RoadRunner。
Nginx + FPM:
$ siege --no-parser -c 250 -t 30s http://127.0.0.1:3000/ > /dev/null** SIEGE 4.0.4** Preparing 250 concurrent users for battle.The server is now under siege...Lifting the server siege...Transactions: 2032 hitsAvailability: 97.83 %Elapsed time: 29.19 secsData transferred: 4.51 MBResponse time: 2.64 secsTransaction rate: 69.61 trans/secThroughput: 0.15 MB/secConcurrency: 183.50Successful transactions: 2032Failed transactions: 45Longest transaction: 27.34Shortest transaction: 0.
RoadRunner(4 worker):
$ siege --no-parser -c 250 -t 30s http://127.0.0.1:8080/ > /dev/null** SIEGE 4.0.4** Preparing 250 concurrent users for battle.The server is now under siege...Lifting the server siege...Transactions: 61417 hitsAvailability: 100.00 %Elapsed time: 29.26 secsData transferred: 135.95 MBResponse time: 0.12 secsTransaction rate: 2099.01 trans/secThroughput: 4.65 MB/secConcurrency: 249.06Successful transactions: 61417Failed transactions: 0Longest transaction: 0.20Shortest transaction: 0.05
如何加速Laravel應用
RoadRunner安裝
RoadRunner已經提供了先行編譯的執行檔案,下載對應平台的版本即可,設定檔可用RoadRunner提供的:Using RoadRunner.
Laravel配置
安裝依賴:
composer require spiral/roadrunner zendframework/zend-diactoros symfony/psr-http-message-bridge
在Laravel項目建立一個psr-worker.php
,代碼來自tobias-kuendig
<?phprequire __DIR__ . "/vendor/autoload.php";use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;$relay = new Spiral\Goridge\StreamRelay(STDIN, STDOUT);$psr7 = new Spiral\RoadRunner\PSR7Client(new Spiral\RoadRunner\Worker($relay));$app = require_once __DIR__ . '/bootstrap/app.php';$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);while ($req = $psr7->acceptRequest()) { try { $httpFoundationFactory = new HttpFoundationFactory(); $request = Illuminate\Http\Request::createFromBase($httpFoundationFactory->createRequest($req)); $response = $kernel->handle($request); $psr7factory = new DiactorosFactory(); $psr7response = $psr7factory->createResponse($response); $psr7->respond($psr7response); } catch (\Throwable $e) { $psr7->getWorker()->error((string)$e); }}
運行rr serv -v
搞定。