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.

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
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

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
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Rector logo

Rector

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

Rector
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
API Platform for Laravel image

API Platform for Laravel

Read article
Pan - A simple, lightweight, and privacy-focused product analytics php package image

Pan - A simple, lightweight, and privacy-focused product analytics php package

Read article
The Inertia.js v2 Beta is Here image

The Inertia.js v2 Beta is Here

Read article
Mastering Laravel, No Compromises, and SourceDive with Joel Clermont image

Mastering Laravel, No Compromises, and SourceDive with Joel Clermont

Read article
Now you can install PHP and the Laravel installer with a single command image

Now you can install PHP and the Laravel installer with a single command

Read article
WireSpy is a Sleek New Debug Bar for Laravel Livewire image

WireSpy is a Sleek New Debug Bar for Laravel Livewire

Read article