Swoole Is a production level asynchronous programming framework developed for PHP. It is a pure C development extension. It allows PHP developers to write high-performance and scalable concurrent TCP, UDP, UNIX socket, HTTP and websocket services in PHP without having too much non blocking I / O programming and low-level Linux kernel knowledge. You can put Swoole Imagine nodejs, but it will have higher performance for PHP.
Why run laravel on the swoole?
The following figure shows the life cycle of PHP. As you can see, every time you run a PHP script, PHP needs to initialize the module and start the Zend engine for your running environment. And compile the PHP script into opcodes for the Zend engine to execute.
However, such a lifecycle needs to be executed every time a request is made. Because the environment created by a single request is destroyed immediately after the request is executed.
In other words, in the traditional PHP life cycle, a lot of time is wasted creating and destroying resources for script execution. Imagine a framework like laravel, how many files need to be loaded in each request? It also wastes a lot of I / O operations
So what if we use the built-in application level server in swoole, and all script files can be saved in memory after loading once? That’s why we need to try running laravel on swoole. Swoole can provide powerful performance, while laravel can provide elegant code structure. These two are a perfect combination!
swooletw/laravel-swooleMain features of the system:
- stay Swoole run Laravel / lumen applications
- Excellent performance up to 30x
- Sandbox mode isolates the application container
- Support running websocket server in laravel application
- Supports cross process sharing of the Poole table
Install using Composer:
$ composer require swooletw/laravel-swoole
This package is dependent on swoole 。 Before using this package, please make sure that your machine has the correct swoole installed. Use the following command to quickly install (Linux):
pecl install swoole
After installing this extension, you need to edit php.ini to add
php -i | grep php.ini # check the php.ini file location sudo echo "extension=swoole.so" > php.ini # add the extension=swoole.so to the end of php.ini php -m | grep swoole # check if the swoole extension has been enabled
Note: swoole now only supports Linux and OSX. The windows server does not support it yet.
Then, add a service provider:
If you use Laravel, where are you
config/app.phpAdd the service provider to the service provider array:
[ 'providers' => [ SwooleTW\Http\LaravelServiceProvider::class, ], ]
If you use Lumen, please add the following code to
This package supports automatic package discovery mechanism. If you are running laravel 5.5 or above, you can skip this step.
Set up and run
Now, you can execute the following command to start the swoole HTTP service.
$ php artisan swoole:http start
Then you can see the following information:
Starting swoole http server... Swoole http server started: <http://127.0.0.1:1215>
You can now access
http://127.0.0.1:1215To enter the laravel application.
Test with MacBook Air 13 inch (2015) and clean lumen 5.5 project:
Benchmarking tools: wrk
wrk -t4 -c100 http://your.app
FPM based nginx
Running 10s test @ http://lumen.app:9999 4 threads and 100 connections Thread Stats Avg Stdev Max +/- Stdev Latency 1.14s 191.03ms 1.40s 90.31% Req/Sec 22.65 10.65 50.00 65.31% 815 requests in 10.07s, 223.65KB read Requests/sec: 80.93 Transfer/sec: 22.21KB
Swoole HTTP service
Running 10s test @ http://127.0.0.1:1215 4 threads and 100 connections Thread Stats Avg Stdev Max +/- Stdev Latency 11.58ms 4.74ms 68.73ms 81.63% Req/Sec 2.19k 357.43 2.90k 69.50% 87879 requests in 10.08s, 15.67MB read Requests/sec: 8717.00 Transfer/sec: 1.55MB
For more modern PHP knowledge, please go toLaravel / PHP knowledge community