Welcome to Abdul Malik Ikhsan's Blog

Using Swoole in Mezzio application with Sdebug

Posted in Laminas, Mezzio by samsonasik on April 5, 2020


If you didn’t try Swoole, you need to try it. It is a PECL extension for developing asynchronous applications in PHP. If you build a Mezzio Application, there is already mezzio-swoole component that ease for its settings and usage.

First, if you didn’t have a mezzio skeleton, you can install the skeleton:

➜ composer create-project mezzio/mezzio-skeleton

Next, install the swoole extension:

➜ sudo pecl install swoole

After it, you can install the mezzio-swoole component:

➜ composer require mezzio/mezzio-swoole

That’s it, you can now open mezzio-skeleton directory and run the mezzio-swoole command, and we will get the following output:

➜ cd mezzio-skeleton
➜ ./vendor/bin/mezzio-swoole start

Swoole is running at 127.0.0.1:8080, in /Users/samsonasik/www/mezzio-skeleton

PHP Warning:  Swoole\Server::start(): Using Xdebug in coroutines is extremely dangerous, please notice that it may lead to coredump! in /Users/samsonasik/www/mezzio-skeleton/vendor/mezzio/mezzio-swoole/src/SwooleRequestHandlerRunner.php on line 169

If you have Xdebug installed, you will get above command output “PHP Warning” output. To fix it, we can uninstall the Xdebug, and install Sdebug instead. We can do the following command:

➜ sudo pecl uninstall xdebug
➜ git clone https://github.com/swoole/sdebug.git
➜ cd sdebug && sudo ./rebuild.sh

Now, you will get the Sdebug information if we run php -v:

➜  ~ php -v
PHP 7.4.4 (cli) (built: Mar 24 2020 10:45:52) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Sdebug v2.9.3-dev, Copyright (c) 2002-2020, by Derick Rethans
    with Zend OPcache v7.4.4, Copyright (c), by Zend Technologies

Let’s try run mezzio-swoole command again:

➜ cd mezzio-skeleton
➜ ./vendor/bin/mezzio-swoole start

Swoole is running at 127.0.0.1:8080, in /Users/samsonasik/www/mezzio-skeleton
Worker started in /Users/samsonasik/www/mezzio-skeleton with ID 0

If you got Segmentation fault in the future, that may because of the `Sdebug`, if you don’t require the ‘Xdebug’/’Sdebug’ feature. You can just uninstall them all together

Succeed! Now, time to benchmark! I used wrk for it. I tested it in Macbook Pro 2011, core i5, with 16GB RAM. I access the page with HTML+JS+CSS in there.

1. Without Swoole

Let’s CUT the previous mezzio-swoole ( type CTRL + C ) command and use PHP Development server:

➜ cd mezzio-skeleton
➜ composer serve

> php -S 0.0.0.0:8080 -t public/
[Sun Apr  5 12:24:15 2020] PHP 7.4.4 Development Server (http://0.0.0.0:8080) started

Now, we can run the benchmark with run wrk command in separate terminal:

➜ wrk -c 1000 -t 10 http://localhost:8080/     
   
Running 10s test @ http://localhost:8080/
  10 threads and 1000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.33s   629.53ms   2.00s    50.33%
    Req/Sec    16.09     12.96    60.00     69.26%
  544 requests in 10.09s, 4.25MB read
  Socket errors: connect 759, read 580, write 1, timeout 393
Requests/sec:     53.90
Transfer/sec:    430.92KB

2. With Swoole

Let’s CUT the previous PHP Development server command ( type CTRL + C ) command and use mezzio-swoole command:

➜ cd mezzio-skeleton
➜ ./vendor/bin/mezzio-swoole start

Swoole is running at 127.0.0.1:8080, in /Users/samsonasik/www/mezzio-skeleton
Worker started in /Users/samsonasik/www/mezzio-skeleton with ID 0

Now, we can run the benchmark with run wrk command in separate terminal:

➜ wrk -c 1000 -t 10 http://localhost:8080/

Running 10s test @ http://localhost:8080/
  10 threads and 1000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.15s   590.35ms   2.00s    54.14%
    Req/Sec    30.13     35.31   170.00     83.03%
  1373 requests in 10.09s, 25.10MB read
  Socket errors: connect 759, read 80, write 0, timeout 418
Requests/sec:    136.07
Transfer/sec:      2.49MB

Above, we get double total requests with swoole in same time! That’s it!

References:

Tagged with: , ,

2 Responses

Subscribe to comments with RSS.

  1. Alejandro Celaya said, on April 5, 2020 at 2:18 pm

    Really good article, Abdul. As usual 😉

    I didn’t know about this sdebug extension. Will take a look.

    I have been using swoole in a mezzio project for the past year or so (previously, with expressive), and it’s amazing.

    The only thing I miss is a proper hot reloading mechanism for development envs, that doesn’t imply restarting the service every time.

    Swoole has a way to make it reload elements in memory, and mezzio includes a feature to listen for file changes in order to invoke that feature, however, it doesn’t work very well.

    Other than that, it’s great.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: