Speed Up Your Laravel App with Swoole

Swoole is an asynchronous programming framework for PHP. It is a PHP extension written in pure C language and allows PHP developers to write high-performance, scalable, concurrent TCP, UDP, Unix socket, HTTP, WebSocket services without knowing much about I&D programming that does not block php, and with its low Linux processor, Swoole gives you high performance.

Why are we employing Laravel at Swoole?

The following illustrat image shows the life cycle in PHP. As you can see, each time you run the php script, PHP needs to start modules and start Zend Engine for your running environment. And your PHP script must be compiled into OpCodes for Zend Engine to execute.

In other words, in the PHP lifecycle, it spends a lot of time creating and deleting resources for you to execute scripts. And in frameworks like Laravel, how many files do you need to upload for a request? There is also a lot of I/O consumption to load files.

So, what happens if we have a server built into Swoole and all scripts can be kept in memory after the first installation? This is why we try to run Swoole on Laravel can be a powerful performance booster and Laravel provides easy build and code usages. Which gives us the perfect combination.

setup

Requesting this package with Composer:

$ composer require swooletw/laravel-swoole

This package is based on the Swoole extension. Be sure to install Swoole before using this package.

Note: Swoole currently only supports Linux and OSX. Windows servers cannot use Swoole yet.

Then add the service provider:

Laravel if you are using add the service provider to the provider's array config/app.php:

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

Lumen if you are using add the following code bootstrap/app.php:

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

If you are running Laravel 5.5 and above, you can skip this step.

You can now run the following command to start the Swoole HTTP server.

$ php artisan swoole:http start

Then you can see the following message:

Starting swoole http server...
Swoole http server started:

You can now access your Laravel app at this address.

<http://127.0.0.1:1215>

Nginx Http Server

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 10.08s, 15.67MB read
Requests/sec: 8717.00
Transfer/sec: 1.55MB

For more information, check out the package in Github Repowater and Official Docs.

  Quote

KARABAY A, 2020 . Speed Up Your Laravel App with Swoole,

https://www.karabayyazilim.com/blog/php/framework/laravel/laravel-uygulamanizi-swoole-ile-hizlandirin-2020-06-21-131754

(Accessed June 21, 2020).


  Share this post

Comments (0)

Comment

Subscribe
Sign up for the email newsletter to be the first to know about my blog posts