Skip to content

How to make Laravel faster

XieBiao edited this page May 23, 2018 · 17 revisions

General optimization

  • Generate cache for route
php artisan route:cache
  • Generate cache for config
php artisan config:cache
  • Compile classes of framework into one php file compiled.php
php artisan optimize --force
  • Optimize PSR0 and PSR4 packages to be loaded with classmaps
composer dumpautoload -o
  • Upgrade to PHP7+

  • Enable OPcache

  • Remove unnecessary middlewares

  • Close debug mode

//.env
APP_DEBUG=false
  • Make sure that your code is the best practice

Keep your code in memory all the time base Swoole

Swoole enables PHP developers to write high-performance, scalable, concurrent TCP, UDP, Unix socket, HTTP, Websocket services in PHP programming language without too much knowledge about non-blocking I/O programming and low-level Linux kernel.

  • Combine Laravel with Swoole simply, ignore the details
// Build a simple Http server for Laravel
class Laravel
{
    protected $app;
    protected $kernel;
    public function __construct()
    {
        require 'bootstrap/autoload.php';
        $this->app = require 'bootstrap/app.php';
        $this->kernel = $this->app->make(\Illuminate\Contracts\Http\Kernel::class);
    }
    public function handle(\Illuminate\Http\Request $request)
    {
        $response = $this->kernel->handle($request);
        $this->kernel->terminate($request, $response);
        return $response;
    }
}
class SwooleLaravel
{
    protected $swoole;
    protected $laravel;
    public function __construct($host, $port)
    {
        $this->swoole = new swoole_http_server($host, $port);
        $this->swoole->on('WorkerStart', function (swoole_http_server $server, $workerId) {
            // Initialize Laravel as the flow in public/index.php, keep one Laravel Application object in every worker process
            // 1. Bootstrap by including 'bootstrap/autoload.php'
            // 2. Create Application by including 'bootstrap/app.php'
            // 3. Create Kernel by $app->make(\Illuminate\Contracts\Http\Kernel::class)
            $this->laravel = new Laravel();
        });
        $this->swoole->on('Request', function (swoole_http_request $request, swoole_http_response $response) {
            // 1. Convert SwooleHttpRequest into IlluminateRequest
            // 2. Handle IlluminateRequest by Kernel and got IlluminateResponse
            // 3. Convert IlluminateResponse into SwooleHttpResonse
            // 4. Respond by SwooleHttpResonse
            // 5. Kernel terminate
            $_GET = isset($request->get) ? $request->get : [];
            $_POST = isset($request->post) ? $request->post : [];
            $_COOKIE = isset($request->cookie) ? $request->cookie : [];
            $_SERVER = isset($request->server) ? array_change_key_case($request->server, CASE_UPPER) : [];
            $_FILES = isset($request->files) ? $request->files : [];
            $illuminateRequest = \Illuminate\Http\Request::capture();
            $illuminateResponse = $this->laravel->handle($illuminateRequest);
            $response->end($illuminateResponse->getContent());
        });
    }
    public function run()
    {
        $this->swoole->start();
    }
}
$sl = new SwooleLaravel('127.0.0.1', 5201);
$sl->run();

Now, Swoole Http Server replaces PHP-FPM, and get better performance.

  • Take full advantage of Swoole
  1. Swoole Websocket Server

  2. Swoole TCP/UDP Server

  3. Async Task

  4. Async IO

  5. Millisecond Timer

  6. Coroutine clients

This post aims to make Laravel faster by all means, hope it works for you. And, is possible to combine Laravel with Swoole officially? Let's wait and see what happens.

Clone this wiki locally