Speed Up Your Laravel Projects Using Laravel Octane & Swoole Server
As we already know, the Laravel framework gives us abundant features. It also has a lot of packages that we can use to build our next great apps. But, here’s a question for yourself. Do you know how’s the lifecycle of Laravel works? This thing is important to us, as a developer, because if we understand how it works, we can be more understand what’s in here and what’s missing, and maybe we can extend the functionalities further.
Laravel lifecycle is pretty complicated to explain in this short article but, let me explain in this way. Every request that comes into our app, will start to execute the public/index.php as the starting point. From this index file, the request will go through to the HTTP/Console Kernels, Service Providers, Routing, and finally send back a response to the user. You can see the detailed lifecycle in the documentation here. In the steps above, Laravel will execute, load, and compile some scripts. Later, when the response is being sent back, all of the instances will be destroyed.
PHP is an interpreted language. This means, when a PHP script runs, the interpreter parses, compiles, and executes the code over and over again on each request. This may result in wasting CPU resources and additional time. https://kinsta.com/blog/php-8/
Fortunately, Swoole comes to help. And recently, Laravel published a new official package called Laravel Octane that uses Swoole as one of the application servers. What Swoole does is storing the compiled scripts/containers into the memory, to be reused later and ready to accept the next requests. With that features, your app doesn’t need to recompile the scripts again and again.
Before we go on, what is Swoole?
High Performance Programmatic Server for PHP with Async IO, Coroutines and Fibers. https://www.swoole.co.uk
Swoole is a package for PHP built with C where it offers a lot of features such as Coroutines, web sockets, and many more. With the coroutines, we can build PHP asynchronously so it’ll be much more efficient than the traditional one. Also, there are some other features such as Swoole Table that we can build our own web socket on top of it. You may need to see the detailed features of Swoole here https://www.swoole.co.uk.
So in this article, I want to do the simple benchmark/comparison between running the Laravel 8 applications using Artisan Serve and using Swoole.
- Laravel Octane Installation
- Benchmark: Web Requests
- Benchmark: API Requests
#1 Laravel Octane Installation
There are some prerequisites for installing Laravel Octane into our application. The first thing is, make sure you have PHP version 8 because it’s required from the package.
PHP 8.0.6 (cli) (built: May 13 2021 22:39:30) ( NTS )
The second thing is, make sure you have the Swoole installed into your PHP extension. You can install the Swoole using pecl.
pecl install swoole
Or if you encounter any difficulties when installing Swoole, consider looking at the official documentation here https://www.swoole.co.uk/docs/get-started/installation.
Then, we need to install the Laravel Octane package into our project and enable the Swoole.
composer require laravel/octane
php artisan octane:install
The Last thing to do, we can start the server using Swoole
php artisan octane:start
That’s it, now your server is running using Swoole server and ready to accept the requests.
#2 Benchmark: Web Requests
The first comparison I want to do is a simple one. I’ll run the Laravel using Artisan Serve and compare it using Swoole server. I won’t change anything inside the web routes, which means it’ll be a default Laravel welcome page.
For the benchmark, I’m using ApacheBench in the terminal with this command:
ab -n 1000 -c 100 http://localhost:8000/
The command above means that there’ll be 1000 requests with 100 concurrencies.
Here’s the result of Swoole Server:
The result of Artisan Serve:
From the simple benchmark above, we can see that for 1000 requests with 100 concurrencies, it takes 0.452 seconds for Swoole and 4.531 seconds for Artisan Serve to finish. Then, for the total requests per second that can be handled: Swoole can handle 2214 requests per second, where Artisan Serve can only handle 220 requests per second. Those numbers prove that they have some big gaps in performances, with up to 10x faster using Swoole.
#3 Benchmark API Requests
For the API benchmark, it’ll be a simple /test endpoint that returns a string “Hello World”. I’ll put the Lumen also in this benchmark because Lumen claims that it has a better performance compared to Laravel. Let’s see the results:
Laravel Swoole Server:
Laravel Artisan Serve:
Lumen Artisan Serve:
Based on the benchmark above, we can see that Lumen’s performance is similar to Swoole. Lumen can handle 1168 requests per second and finish the tasks in 856 ms, compared to Swoole with 1687 requests per second and 593 ms to finish the tasks.
Unfortunately, right now Laravel Octane doesn’t support the Lumen application, so we can’t see the Lumen performance when running using Swoole. But, based on the numbers above, the Swoole performance is slightly better than the Lumen micro-framework.
After seeing Swoole’s abilities from the numbers above, what do you think? Is it worth it to try? Notice that Laravel Octane features are not only limited to serve the server on top of Swoole. Instead, Laravel Octane has a lot of features that Swoole supports, so it’ll definitely make your app more faster and efficient. I hope this article and the comparison can help you understand what Laravel Octane & Swoole can do in your project.
Speed Up Laravel on Top of Swoole
Swoole is a production-grade async programming framework for PHP. It is a PHP extension written in pure C language…