-
Notifications
You must be signed in to change notification settings - Fork 479
How to make Laravel faster
XieBiao edited this page May 23, 2018
·
17 revisions
- Generate cache for route
php artisan route:cache
- Generate cache for config
php artisan config:cache
- Compiled 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
-
Make sure that your code is the best practice
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();
- Take full advantage of Swoole
-
Swoole Websocket Server
-
Swoole TCP/UDP Server
-
Async Task
-
Async IO
-
Millisecond Timer
-
Coroutine clients
This post aims to make Laravel faster by all means, hope it works for you.