這篇文章主要介紹了關於使用RoadRunner 加速Laravel 應用,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下
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 hits Availability: 97.83 % Elapsed time: 29.19 secs Data transferred: 4.51 MB Response time: 2.64 secs Transaction rate: 69.61 trans/sec Throughput: 0.15 MB/sec Concurrency: 183.50 Successful transactions: 2032 Failed transactions: 45 Longest transaction: 27.34 Shortest 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 hits Availability: 100.00 % Elapsed time: 29.26 secs Data transferred: 135.95 MB Response time: 0.12 secs Transaction rate: 2099.01 trans/sec Throughput: 4.65 MB/sec Concurrency: 249.06 Successful transactions: 61417 Failed transactions: 0 Longest transaction: 0.20 Shortest transaction: 0.05
RoadRunner已經提供了預先編譯的執行文件,下載對應平台的版本即可,設定檔可用RoadRunner提供的:Using RoadRunner.
安裝依賴:
composer require spiral/roadrunner zendframework/zend-diactoros symfony/psr-http-message-bridge
在Laravel專案創建一個psr-worker.php
,程式碼修改來自tobias-kuendig:
<?php require __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(); if (strpos($req->getHeaderLine("content-type"), "application/json") === 0) { $body = $req->getBody(); $parsedBody = json_decode($body, true); $req = $req->withParsedBody($parsedBody); } $symfonyReq = $httpFoundationFactory->createRequest($req); $request = Illuminate\Http\Request::createFromBase($symfonyReq); $response = $kernel->handle($request); $psr7factory = new DiactorosFactory(); $psr7response = $psr7factory->createResponse($response); $psr7->respond($psr7response); } catch (\Throwable $e) { $psr7->getWorker()->error((string)$e); } }
運行rr serve -v
搞定。
以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!
相關推薦:
Laravel微信小程式取得使用者詳細資料及帶參數小程式碼擴展的分析
以上是使用RoadRunner 加速 Laravel 應用的詳細內容。更多資訊請關注PHP中文網其他相關文章!