Skip to content

How to make Laravel faster

XieBiao edited this page May 23, 2018 · 17 revisions

General optimization

  • Upgrade to PHP7+

  • Enable OPcache

  • Remove unnecessary middlewares

  • Close debug mode

//.env
APP_DEBUG=false
  • 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
  • Make sure that your code is the best practice

Memory resident

Keep your code in memory all the time base Swoole, get a lot of performance improvement.

  • 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(copy from Swoole official website). As far as I know, PHP-FPM mode will load Laravel framework every request. But now we can use Swoole Http Server to avoid it, decrease time cost.

  • 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 to IlluminateRequest
            // 2. Handle IlluminateRequest by Kernel and got IlluminateResponse
            // 3. Convert IlluminateResponse to SwooleHttpResonse
            // 4. Respond with SwooleHttpResonse
            // 5. Kernel terminates
            $_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', 5200);
$sl->run();
// Next: curl http://127.0.0.1:5200

Now, Swoole Http Server replaces PHP-FPM, and get better performance. But you need to be careful of global/static variable and singleton instance.

  • Take full advantage of Swoole
  1. Swoole Websocket server

  2. Swoole TCP/UDP/UnixSocket server

  3. Async IO: task, read/write file, dns lookup, event loop, many clients, millisecond timer

  4. Operate memory: lock, buffer, table, atomic, mmap, channel, serialize

  5. Support coroutine

  6. Manage process/process pool

This post aims to make Laravel faster by trying all means, hope it works for you.

Is possible to support Swoole by Laravel officially?

Let's wait and see what happens.

Clone this wiki locally