In this article am going to show you how you can use it with a simple lumen application, and show you the results I got from using Swoole comparing to using nginx.

1. Creating Lumen Project

First of all, let’s create a simple Lumen project

PHP

1

$composer create-project laravel/lumen

2. Install Required Packages

Secondly, we need to install the lumen package for Swoole, and made small change

PHP

1

2

$cd lumen

$composer requireswooletw/laravel-swoole

3. Register the Service Provider

Then we need to edit our bootstrap/app.php file and register Swoole service provider, just add the following line within Register Service Providers section

PHP

1

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

4. Prepare Our Dockerfile

Lastly, we need to prepare our simple Dockerfile which should live within our lumen project directory

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

FROM zaherg/php72-swoole:latest

LABEL Maintainer="Zaher Ghaibeh <z@zah.me>"

ENVAPP_ENV${APP_ENV:-production}

ENVAPP_DEBUG${APP_DEBUG:-false}

ENVAPP_TIMEZONE${APP_TIMEZONE:-UTC}

ENVSWOOLE_HTTP_PORT${SWOOLE_HTTP_PORT:-80}

ENVSWOOLE_HTTP_HOST${SWOOLE_HTTP_HOST:-"0.0.0.0"}

USER root

ADD.//var/www

CMD["php","artisan","swoole:http","start"]

5. Build the Docker image

Now we are ready to build our docker image

PHP

1

$docker build-tlumen-swoole.

6. Run our Docker image

Once it’s finished you can run it with the command

PHP

1

$docker run--rm--name swoole-p80:80-dlumen-swoole:latest

Open your browser to localhost and you will be greeted with lumen default route.

Some tests with real data:

I did a small test with real data, to return 500 records at once from MySQL, and I tried hard to make the environment equal in both, but the only difference is that the nginx box is running PHP 7.1 which comes with alpine by default, meanwhile Swoole is running PHP 7.2

And this is what I got

Nginx:

PHP

1

2

3

4

5

6

7

8

9

10

wrk-t4-c100 http://api.test/v1/all

Running10stest@http://api.test/v1/all

4threads and100connections

Thread Stats Avg Stdev Max+/-Stdev

Latency1.48s245.72ms1.73s66.67%

Req/Sec1.502.2210.0082.14%

32requests in10.05s,13.84KBread

Socket errors:connect0,read124,write5,timeout29

Requests/sec:3.18

Transfer/sec:1.38KB

Swoole:

PHP

1

2

3

4

5

6

7

8

9

10

$wrk-t4-c100 http://api.test/v1/all

Running10stest@http://api.test/v1/all

4threads and100connections

Thread Stats Avg Stdev Max+/-Stdev

Latency603.12ms104.12ms1.05s81.77%

Req/Sec40.2117.70101.0059.08%

1607requests in10.05s,627.73KBread

Socket errors:connect0,read59,write0,timeout0

Requests/sec:159.82

Transfer/sec:62.43KB

Basically with Swoole, I was able to execute 160 requests per second, meanwhile, with Nginx, I was able to execute 3.18 requests per second.

Yes, it works. I am using Windows 10 Pro. Invoking the default Lumen welcome page with Apache Benchmark I get 280 req/sec. Command: ./ab -c 9 -n 200 “http://localhost/” Hardware: i7 3770 4C 3400MHz. I recommend Windows 10 Professional for docker, although it can run on Windows 10 Home as well, but with a different setup. Excellent article, btw. Thanks!

There is no direct answer to this question, as this depends on your production infrastructure, as for example some will put this image behind a loadbalancer, some will use Docker swarm or even K8s .. etc.