Speed Up Laravel on Top of Swoole

Published on by

Speed Up Laravel on Top of Swoole image

Swoole is a production-grade async programming framework for PHP. It is a PHP extension written in pure C language, which enables PHP developers to write high-performance, scalable, concurrent TCP, UDP, Unix socket, HTTP, WebSocket services in PHP without too much knowledge of the non-blocking I/O programming and low-level Linux kernel. You can think of Swoole as something like NodeJS but for PHP, with higher performance.

Why run Laravel on Swoole?

The image below illustrates the lifecycle in PHP. As you can see, when you run php script every time, PHP needs to initialize modules and launch Zend Engine for your running environment. And your PHP script needs to be compiled to OpCodes for Zend Engine’s execution.

However, this lifecycle needs to go over and over again in each request. Because the environment created for a single request will be immediately destroyed after the request process is done.

In other words, in traditional PHP lifecycle, it wastes a bunch of time building and destroying resources for your script execution. And imagine in frameworks like Laravel, how many files does it need to load for one request? There’s a lot of I/O consumption for loading files as well.

So what if we have a built-in server on top of Swoole, and all the scripts can be kept in memory after the first load? This is why we try to run Laravel on Swoole. Swoole can be a powerful performance booster and Laravel provides the elegant structure and code usages. That’s a perfect combination!

Installation

Here are the main features of swooletw/laravel-swoole:

  • Run Laravel/Lumen application on top of Swoole.
  • Outstanding performance boosting up to 30x.
  • Sandbox mode to isolate app container.
  • Support running WebSocket server in Laravel.
  • Support Socket.io protocol.
  • Support Swoole table for cross-process data sharing.

Require this package with Composer:

$ composer require swooletw/laravel-swoole

This package relies on Swoole extension. Make sure you’ve installed Swoole before you use this package. Using this command to install it quickly:

pecl install swoole

After installing the extension, you will need to edit php.ini and add an extension=swoole.so line before you use it.

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

Visit the official website for more information.

Notice: Swoole currently only supports Linux and OSX. Windows servers are not able to use Swoole yet.

Then, add the service provider:

If you are using Laravel, add the service provider to the provider’s array in config/app.php:

[
'providers' => [
SwooleTW\Http\LaravelServiceProvider::class,
],
]

If you are using Lumen, append the following code to bootstrap/app.php:

$app->register(SwooleTW\Http\LumenServiceProvider::class);

It supports package auto discovery. If you’re running Laravel 5.5, you can skip this step.

Up and Running

Now, you can run the following command to start Swoole HTTP server.

$ php artisan swoole:http start

Then you can see the following message:

Starting swoole http server...
Swoole http server started: <http://127.0.0.1:1215>

Now you can access your Laravel application on http://127.0.0.1:1215.

Benchmark

Test with clean Lumen 5.5, using MacBook Air 13, 2015. Benchmarking Tool: wrk

wrk -t4 -c100 http://your.app

Nginx with FPM

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 Server

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

Learn More

Check out the official package at Github Repo and Official Docs for more information.

Albert Chen photo

Hello, I'm Albert Chen, a software architect, Laravel artisan and Swoole enthusiast located in Taiwan

My website: https://albert-chen.com

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Tinkerwell

Version 4 of Tinkerwell is available now. Get the most popular PHP scratchpad with all its new features and simplify your development workflow today.

Visit Tinkerwell
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
LoadForge logo

LoadForge

Easy, affordable load testing and stress tests for websites, APIs and databases.

LoadForge
Paragraph logo

Paragraph

Manage your Laravel app as if it was a CMS – edit any text on any page or in any email without touching Blade or language files.

Paragraph
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
DocuWriter.ai logo

DocuWriter.ai

Save hours of manually writing Code Documentation, Comments & DocBlocks, Test suites and Refactoring.

DocuWriter.ai
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector

The latest

View all →
Laravel Pint --bail Flag image

Laravel Pint --bail Flag

Read article
Laravel Herd for Windows is now released! image

Laravel Herd for Windows is now released!

Read article
The Laravel Worldwide Meetup is Today image

The Laravel Worldwide Meetup is Today

Read article
Cache Routes with Cloudflare in Laravel image

Cache Routes with Cloudflare in Laravel

Read article
Learn how to manage timezones in your Laravel Apps image

Learn how to manage timezones in your Laravel Apps

Read article
"Can I PHP?" Raycast Extension image

"Can I PHP?" Raycast Extension

Read article