docker php7 swoole,Using Swoole PHP 7.2 Docker image

1460000015110382

We all read about Swoole in Laravel-news article, and the results they provided looks nice, this pushed me to create a new Docker image which has PHP 7.2 CLI and Swoole complied with it directly.

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

$ composer create-project laravel/lumen

$composercreate-projectlaravel/lumen

2. Install Required Packages

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

$ cd lumen

$ composer require swooletw/laravel-swoole

$cdlumen

$composerrequireswooletw/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

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

$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

FROM zaherg/php72-swoole:latest

LABEL Maintainer="Zaher Ghaibeh "

ENV APP_ENV ${APP_ENV:-production}

ENV APP_DEBUG ${APP_DEBUG:-false}

ENV APP_TIMEZONE ${APP_TIMEZONE:-UTC}

ENV SWOOLE_HTTP_PORT ${SWOOLE_HTTP_PORT:-80}

ENV SWOOLE_HTTP_HOST ${SWOOLE_HTTP_HOST:-"0.0.0.0"}

USER root

ADD ./ /var/www

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

FROMzaherg/php72-swoole:latest

LABELMaintainer="Zaher Ghaibeh "

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"}

USERroot

ADD.//var/www

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

5. Build the Docker image

Now we are ready to build our docker image

$ docker build -t lumen-swoole .

$dockerbuild-tlumen-swoole.

6. Run our Docker image

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

$ docker run --rm --name swoole -p 80:80 -d lumen-swoole:latest

$dockerrun--rm--nameswoole-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:

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

Running 10s test @ http://api.test/v1/all

4 threads and 100 connections

Thread Stats Avg Stdev Max +/- Stdev

Latency 1.48s 245.72ms 1.73s 66.67%

Req/Sec 1.50 2.22 10.00 82.14%

32 requests in 10.05s, 13.84KB read

Socket errors: connect 0, read 124, write 5, timeout 29

Requests/sec: 3.18

Transfer/sec: 1.38KB

wrk-t4-c100http://api.test/v1/all

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

4threadsand100connections

ThreadStatsAvgStdevMax+/-Stdev

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

Req/Sec1.502.2210.0082.14%

32requestsin10.05s,13.84KBread

Socketerrors:connect0,read124,write5,timeout29

Requests/sec:3.18

Transfer/sec:1.38KB

Swoole:

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

Running 10s test @ http://api.test/v1/all

4 threads and 100 connections

Thread Stats Avg Stdev Max +/- Stdev

Latency 603.12ms 104.12ms 1.05s 81.77%

Req/Sec 40.21 17.70 101.00 59.08%

1607 requests in 10.05s, 627.73KB read

Socket errors: connect 0, read 59, write 0, timeout 0

Requests/sec: 159.82

Transfer/sec: 62.43KB

$wrk-t4-c100http://api.test/v1/all

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

4threadsand100connections

ThreadStatsAvgStdevMax+/-Stdev

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

Req/Sec40.2117.70101.0059.08%

1607requestsin10.05s,627.73KBread

Socketerrors: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.

Now it is up to you which one to use.

Related

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值