Swoole Installation and Laravel

Swoole and Laravel

Hello again everyone, today I will try to tell you more about Swoole. It doesn’t matter whether you’ve heard of it before or not, because when you finish reading this article, you will understand why I decided to talk about it. So bear with me and let’s start!

First of all, What is Swoole?

swoole medianova

Swoole is an Asynchronous, Event Oriented Coroutine based concurrent network communication engine written in C and C ++ developed for PHP. We can say that it is a new era for PHP without getting to explanation. In this article, we will focus on its usage on Laravel.

System requirements

  • Operating System: Linux*, FreeBSD, macOS
  • Linux Kernel Version ≥ 2.3.32
  • PHP Version ≥ 5.3.10
  • GCC version ≥ 4.4
  • Cmake Version ≥ 2.4 **

* CentOS 6.2+ | Ubuntu 12+ | Debian 6+
** Required for Cmake Swoole Library to compile in C / C ++.
Recommended Environments are ≥ 14 and CentOS ≥ 7 for Ubuntu.


Setup

We can set up Swoole via Pecl. as follows;

#!/bin/bash
pecl install swoole

In this way, we ensure that the Swoole library is installed on our system. If we want to install it manually;

#!/bin/bash
cd /tmp
wget https://github.com/swoole/swoole-src/archive/v4.4.16.zip
cd swoole
phpize
./configure
make
swoole/module/swoole.so
sudo make install

After that, a library was added in our system. But we did not activate it. So now let’s do the activation process.

php -i | grep php.ini
sudo echo "extension=swoole.so" >> php.ini
php -m | grep swoole

The above blog makes a definition for Swoole into our php.ini file. You can check this by going to your php.ini file.

After all, it is now ready to use Swoole. But since we will talk about its use on Laravel, there are a few more steps we need to take, let’s go through them quickly.

Laravel and Swoole

Now that you have come here, I will not talk about creating a Laravel Project. You will find that it can be easily added with a single change in the directory where your project is located. Let’s add it;

composer require swooletw/laravel-swoole

We start by adding the swooletw / laravel-swoole package to our project via Composer, then | We go to our /config/app.php file and add our Service class to providers.

#config/app.php
[ 'providers' =>
    [
      SwooleTWHttpLaravelServiceProvider::class,
    ],
]

Thus, we have added Swoole to our Laravel Project. Now let’s run our project and do our tests. We run the following code over Artisan CLI.

php artisan swoole:http start

If you get this message, our project is working.

Starting swoole http server… Swoole http server started:

Now we can make a few settings. First of all, let’s run on port 80 instead of port 1215. After installation, we have created swoole_http.php file in our Config folder. All of our Swoole related settings are only here for now.

‘port’ => env(‘SWOOLE_HTTP_PORT’, ‘1215’) instead

‘port’ => env(‘SWOOLE_HTTP_PORT’, ’80’),

At this point, we can clearly see

that this is not the right method. We add the following key to our .env file and run it again.

#! /.env
SWOOLE_HTTP_PORT=80
#!/bin/bash 
php artisan optimize:clear  
php artisan swoole:http start

Then our project started broadcasting on our 80 ports. We can now run a benchmark test. I share the sample outputs with you. I am writing my tests yet, and I will share them extensively here in another article. If you share your results and test conditions in the comments, you may have more ideas. Thanks in advance.

#! Nginx – PHP-FPM

Running 10s test @ http://127.0.0.1

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 ServerRunning 10s test @ http://127.0.0.1

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

Resources :

https://github.com/swooletw/laravel-swoole

https://www.swoole.co.uk

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors