Accelerate laravel with swoole (in a formal environment)


1. Where is the speed bottleneck of laravel?

1.1 some existing optimization methods

1.1.1 laavel officially provides some optimization methods to optimize laavel

php artisan optimize
php artisan config:cache
php artisan route:cache

1.1.2 using opcache acceleration, PHP is an interpreted language. When executing, you must first read the program and compile it into opcode by Zend engine. Finally, the Zend virtual machine executes these opcodes in sequence to complete the operation. Opcache is used to cache opcode, so as to reduce compilation time and CPU density.

1.1.3 when using php7.1, don’t ask me why

1.2 bottlenecks on disk IO

Laravel needs a lot of files to start itself. In addition, its famous ecological environment is good. We will have many existing wheels in development, which makes the disk IO of one start particularly high (that is, to load many files). Although the official PHP artisan optimize method optimizes the loading of files, it does not actually solve the IO problem.
If you know the problem, it’s easy to solve. Just don’t reload every time you start. Now it’s swoole’s turn to play.

2 Swoole

Swoole is a PHP extension that enables PHP to execute asynchronously, just like node. It can also use socket to provide PHP with a series of asynchronous IO, event driven and parallel data structure functions. I won’t say the specific installation method. Google yourself.

3 existing wheels

Search the existing swoole on GitHub, start the wheel of laravel, and find three wheels


Using laravefly, I felt cool listening to the name, but the result was not satisfactory. I really didn’t like its tough starting method. It doesn’t go well with laravel’s style – ‘elegance’. So I wanted to write it myself. In the middle of writing it, I found that the laravoole project had been updated, and then the startup method (using the artist command, using the bash script before the update). The code style was very cool. That’s what I wanted to do!

The wheel of Chongyi / schoole laravel framework was shared by the author in the wechat group when I was writing the wheel. Interested friends can try it. I haven’t tried it yet.

4 several points for attention in laravoole

You can seeAuthor’s document, I’ll just summarize a few points I encountered in the process of using
You should no longer use the following super global variables, because they are created by the web server, and their use by a non hot start project may cause variable pollution. You can get the data you want from laravel’s request class.


2 because I want to develop wechat related, I use itEASYWECHATThis package, but the OAuth method of this package uses the native session, so it should also be changed to redis and other methods to store sessions. The specific code is as follows.

//In your controller or middleware
  public function handle(Request $request, Closure $next)
  //Omit code
  $redirect = config('app.url') . $request->getRequestUri();// This address requires a token
  $options = [
                'app_id' => config('app.appid'),
                'secret' => config('app.secret'),
                'oauth' => [
                    'scopes' => ['snsapi_userinfo'],
                    'callback' => $redirect,

   $app = new Application($options);
   //Replace the native session with the laravel session
   //Omit the following code

3 does not support hot start, so you need to restart the laravoole process after each code update.

$ php artisan laravoole restart

If you need to support hot start, please Google swoole + inotify by yourself. The general principle is to use inotify to monitor file changes. If you update and restart swoole, you can write your own deployment script in the formal environment and restart the service after git pull. There are many methods, which are not listed one by one.

5 happy to prepare for the test

Testing machine:
Alibaba cloud
Disregarding the impact of bandwidth, he requested the local machine. The test results are as follows. He has been tested several times, with an average of about 700rps. The original only had more than 20 RPS.

Accelerate laravel with swoole (in a formal environment)

Accelerate laravel with swoole (in a formal environment)