标签(空格分隔): phpphp
Swoole 是为 PHP 开发的生产级异步编程框架。 他是一个纯 C 开发的扩展, 他容许 PHP 开发者在 PHP 中写 高性能,可扩展的并发 TCP, UDP, Unix socket, HTTP, WebSocket 服务, 而不须要拥有太多的非阻塞 I/O 编程和低级别的 Linux 内核知识。 你能够把 Swoole 想象成 Go, 但对于 PHP 来讲将有更高性能。html
在PHP的生命周期中, 当你每次运行PHP脚本的时候, PHP都须要初始化模块并为你的运行环境启动zend引擎. 并将PHP脚本编译为OpCodes
以便Zend
引擎执行.linux
可是, 这样的生命周期须要在每次请求的时候都执行一遍, 由于单个请求建立的环境在请求结束后当即销毁.nginx
换句话说, 在传统的PHP生命周期中, 为了脚本执行而浪费了大量的时间去建立和销毁资源. 想象一下像laravel这样的框架, 在每次请求中须要加载多少文件? 同时也浪费了大量的I/O操做.laravel
swoole是内置在应用级别的server, 而且全部脚本文件在加载一次以后即可以保存在内存中. 这就是为何咱们须要尝试在swoole上运行laravel.git
swoole能够提供强大性能而Laravel则能够提供优雅代码结构. 完美!github
app | php | laravel | swoole |
---|---|---|---|
your.domain.com | 7.1.16 | 5.4.36 | 4.1.2 |
基准测试工具:wrkdocker
// wrk命令参数 -c, --connections: total number of HTTP connections to keep open with each thread handling N = connections/threads -d, --duration: duration of the test, e.g. 2s, 2m, 2h -t, --threads: total number of threads to use -s, --script: LuaJIT script, see SCRIPTING -H, --header: HTTP header to add to request, e.g. "User-Agent: wrk" --latency: print detailed latency statistics --timeout: record a timeout if a response is not received within this amount of time.
pecl install swoole
编程
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \ && sed -i 's/nl.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories RUN apk add --no-cache --virtual .build-deps \ $PHPIZE_DEPS \ && docker-php-source extract \ && cd /usr/src \ && wget http://pecl.php.net/get/swoole-4.1.2.tgz \ && tar -xzvf swoole-4.1.2.tgz \ && mv /usr/src/swoole-4.1.2 /usr/src/php/ext/swoole \ && docker-php-ext-configure swoole --enable-openssl \ && docker-php-ext-install swoole \ && runDeps="$( \ scanelf --needed --nobanner --format '%n#p' --recursive /usr/local \ | tr ',' '\n' \ | sort -u \ | awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \ )" \ && apk add --no-cache --virtual .php-rundeps $runDeps \ && docker-php-source delete \ && apk del .build-deps \ && rm -rf /usr/src/swoole-4.1.2.tgz
如下是 swooletw/laravel-swoole 的主要特色:bootstrap
使用 Composer 安装:
$ composer require swooletw/laravel-swoole
这个包支持包自动发现机制。若是你运行 Laravel 5.5 以上版本,你能够跳过这一步。
laravel配置: 在 config/app.php 服务提供者数组添加该服务提供者
[ 'providers' => [ SwooleTW\Http\LaravelServiceProvider::class, ], ]
lumen配置: 请将下面的代码添加到 bootstrap/app.php
$app->register(SwooleTW\Http\LumenServiceProvider::class);
如今,你能够执行如下的命令来启动 Swoole HTTP 服务。
php artisan swoole:http start
而后你能够看到如下信息:
Starting swoole http server...
Swoole http server started: http://127.0.0.1:1215
如今能够经过访问 http://127.0.0.1:1215
来进入 Laravel 应用。
若是须要修改端口号或服务地址, 可配置相应的环境变量
// vendor/swooletw/laravel-swoole/config/swoole_http.php SWOOLE_HTTP_HOST: '127.0.0.1' SWOOLE_HTTP_PORT: '1215'
详细的文档参考: https://wiki.swoole.com/wiki/page/14.html
wrk -t4 -c100 http://your.domain.com/version Running 10s test @ http://your.domain.com/version 4 threads and 100 connections Thread Stats Avg Stdev Max +/- Stdev Latency 341.64ms 108.70ms 801.47ms 69.08% Req/Sec 71.72 27.35 171.00 65.57% 2864 requests in 10.03s, 2.84MB read Requests/sec: 285.63 Transfer/sec: 289.79KB
wrk -t12 -c400 -d30s http://your.domain.com/version Running 30s test @ http://your.domain.com/version 12 threads and 400 connections Thread Stats Avg Stdev Max +/- Stdev Latency 801.35ms 349.89ms 2.00s 68.56% Req/Sec 40.61 19.83 126.00 65.80% 14390 requests in 30.10s, 14.24MB read Socket errors: connect 0, read 0, write 0, timeout 132 Requests/sec: 478.09 Transfer/sec: 484.34KB
wrk -t4 -c100 http://your.domain.com/version Running 10s test @ http://your.domain.com/version 4 threads and 100 connections Thread Stats Avg Stdev Max +/- Stdev Latency 198.64ms 324.54ms 1.96s 88.59% Req/Sec 225.62 91.30 430.00 62.72% 9021 requests in 10.09s, 7.90MB read Socket errors: connect 0, read 0, write 0, timeout 25 Requests/sec: 893.71 Transfer/sec: 801.26KB
wrk -t12 -c400 -d30s http://your.domain.com/version Running 30s test @ http://your.domain.com/version 12 threads and 400 connections Thread Stats Avg Stdev Max +/- Stdev Latency 319.84ms 398.59ms 2.00s 85.59% Req/Sec 66.94 35.58 240.00 66.09% 23862 requests in 30.09s, 20.89MB read Socket errors: connect 0, read 0, write 0, timeout 619 Requests/sec: 793.04 Transfer/sec: 711.05KB
使用Nginx来代理运行于Swoole上的Laravel
server { listen 80; server_name your.domain.com; root /path/to/laravel/public; index index.php; location = /index.php { # Ensure that there is no such file named "not_exists" # in your "public" directory. try_files /not_exists @swoole; } location / { try_files $uri $uri/ @swoole; } location @swoole { set $suffix ""; if ($uri = /index.php) { set $suffix "/"; } proxy_set_header Host $host; proxy_set_header SERVER_PORT $server_port; proxy_set_header REMOTE_ADDR $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # IF https # proxy_set_header HTTPS "on"; proxy_pass http://127.0.0.1:1215$suffix; } }