详解基于Laravel8下的LaravelS实现高性能HTTP服务器

作者: 温新

分类: 【高性能PHP】

阅读: 3919

时间: 2020-11-01 15:20:52

准备工作

虚拟生命:vm16

环境:LNMP

系统版本:CentoOS8.2

框架:Laravel8

我使用的测试环境大多数是基于最新版本,目的是用于测试学习。既然推出了最新的版本,不管你接不接受或愿不愿意使用,它都是一个趋势。因此,基于学习的目的,使用新版本。

关于环境的搭建不再重复了,博客里面写了源码安装搭建PHP环境。

什么是LaravelS

LaravelS 是一个胶水项目,用于快速集成 Swoole 到 Laravel 或 Lumen,然后赋予它们更好的性能、更多可能性。

创建Laravel8项目

第一步:安装Laravel8

<span style="color: rgb(255, 255, 255);">composer create-project --prefer-dist laravel/laravel laravel_swoole</span><br></br>

第二步:配置Laravel8虚拟主机

<span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(232, 191, 106) !important">cd</span> /usr/local/nginx/conf/vhosts/</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(98, 151, 85) !important"># 注意:这个虚拟主机机配置Laravel8项目</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(232, 191, 106) !important">vim</span> laswoole.conf</span>

注意:我这里是vhosts目录下。在nginx.conf文件中通过 include 引入vhosts 目录下所有配置文件

<span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">server {</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">    listen        <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">80</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">    server_name laravelswoole.com;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">    root   <span style="box-sizing: border-box;color: rgb(152, 195, 121) !important">"/www/laravel_swoole/public"</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">    location / {</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        index index.php index.html;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        autoindex  off;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">    }</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">    location ~ \.php(.*)<span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$ </span>{</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        fastcgi_pass   <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">127</span>.0.0.1:9000;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        fastcgi_index  index.php;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        fastcgi_split_path_info  ^((?U).<span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">+</span>\.php)(/?.<span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">+</span>)<span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$;</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        fastcgi_param  SCRIPT_FILENAME  <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$document_root$fastcgi_script_name</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        fastcgi_param  PATH_INFO  <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$fastcgi_path_info</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        fastcgi_param  PATH_TRANSLATED  <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$document_root$fastcgi_path_info</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        include        fastcgi_params;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">    }</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">}</span>

第三步:重启nginx

<span style="color: rgb(255, 255, 255);">systemctl restart nginx.service</span><br></br>

第四步:windows10本机配置hosts文件

<span style="color: rgb(255, 255, 255);">192.168.157.129 laravelswoole.com</span><br></br>

安装Laravel-s

第一步:安装Laravels

<span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(232, 191, 106) !important">cd</span> laravel_swoole/</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(98, 151, 85) !important"># 安装</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">composer require hhxsv5/laravel-s</span>

第二步:生成配置

<span style="color: rgb(255, 255, 255);">php artisan laravels publish</span><br></br>

Laravel5.5以上版本不再需要手动配置app.php了。此命令完成之后即可使用。

第三步:运行Laravel-S

<span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">php</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">bin</span><span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">/</span><span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">laravels</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">start</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">_</span>                               <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">_</span>  <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">_____</span> </span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">|</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">|</span>                             <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">|</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">|/</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">____</span><span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">|</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">|</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">|</span>     <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">__</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">_</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">_</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">__</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">__</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">___</span>   <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">_____</span><span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">|</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">|</span> (<span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">___</span>  </span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">|</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">|</span>    <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">/</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">_`</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">|</span> <span style="box-sizing: border-box;color: rgb(152, 195, 121) !important">'__/ _` \ \ / / _ \ |\___ \ </span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(152, 195, 121) !important">| |___| (_| | | | (_| |\ V /  __/ |____) |</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(152, 195, 121) !important">|______\__,_|_|  \__,_| \_/ \___|_|_____/ </span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">                                           </span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(152, 195, 121) !important">Speed up your Laravel/Lumen</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(152, 195, 121) !important">>>> Components</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(152, 195, 121) !important">+---------------------------+---------+</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(152, 195, 121) !important">| Component                 | Version |</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(152, 195, 121) !important">+---------------------------+---------+</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(152, 195, 121) !important">| PHP                       | 7.4.11  |</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(152, 195, 121) !important">| Swoole                    | 4.5.5   |</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(152, 195, 121) !important">| LaravelS                  | 3.7.8   |</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(152, 195, 121) !important">| Laravel Framework [local] | 8.11.2  |</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(152, 195, 121) !important">+---------------------------+---------+</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(152, 195, 121) !important">>>> Protocols</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(152, 195, 121) !important">+-----------+--------+-------------------+----------------+</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(152, 195, 121) !important">| Protocol  | Status | Handler           | Listen At      |</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(152, 195, 121) !important">+-----------+--------+-------------------+----------------+</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(152, 195, 121) !important">| Main HTTP | On     | Laravel Framework | 127.0.0.1:5200 |</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(152, 195, 121) !important">+-----------+--------+-------------------+----------------+</span></span>

运行之后通过 ip+端口即可访问,如192.168.157.129:5200

第四步:配置nginx

注意了,这里是为Laravel-S单独配置一个服务器。传统的配置是nginx接收到一个php请求后转发给php-fmp处理;实现高性能Swoole HTTP服务器,这里是将请求转发给LaravelS处理。

<span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(232, 191, 106) !important">cd</span> /usr/local/nginx/conf/vhosts</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(232, 191, 106) !important">vim</span> laravel_s.conf</span>
<span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">upstream laravels {</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">    server <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">127</span>.0.0.1:5200 <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">weight</span><span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">=</span><span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">5</span> <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">max_fails</span><span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">=</span><span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">3</span> <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">fail_timeout</span><span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">=</span>30s;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">    keepalive <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">16</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">}</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">server {</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">    listen <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">80</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">    server_name laravel-s.com;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">    root /www/laravel_swoole/public;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">    index index.php index.html index.htm;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">    location / {</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        try_files <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$uri</span> @laravels;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">    }</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">    location @laravels {</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        proxy_http_version <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">1</span>.1;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        proxy_set_header Connection <span style="box-sizing: border-box;color: rgb(152, 195, 121) !important">""</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        proxy_set_header X-Real-IP <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$remote_addr</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        proxy_set_header X-Real-PORT <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$remote_port</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        proxy_set_header X-Forwarded-For <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$proxy_add_x_forwarded_for</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        proxy_set_header Host <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$http_host</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        proxy_set_header Scheme <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$scheme</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        proxy_set_header Server-Protocol <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$server_protocol</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        proxy_set_header Server-Name <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$server_name</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        proxy_set_header Server-Addr <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$server_addr</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        proxy_set_header Server-Port <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$server_port</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        proxy_pass http://laravels;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">    }</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">}</span>

第五步:重启nginx

<span style="color: rgb(255, 255, 255);">systemctl restart nginx.service</span><br></br>

第六步:windows10配置hosts文件

<span style="color: rgb(255, 255, 255);">192.168.157.129 laravel-s.com</span><br></br>

到这里就已经配置完成。配置基于域名之后,端口访问失败了。

说明:laravelswoole.com 是基础传统的HTTP服务器;laravel-s是基于Swoole。

性能测试对比

使用ab压力测试工具进行对比测试。

基于php-fpm的压力测试

<span style="color: rgb(255, 255, 255);">ab -n 500 -c 100 http://laravelswoole.com</span><br></br>
<span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Server Software:        nginx/1.18.0</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Server Hostname:        laravelswoole.com</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Server Port:            <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">80</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">...<span style="box-sizing: border-box;color: rgb(98, 151, 85) !important">#省略</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Concurrency Level:      <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">10</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Time taken <span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">for</span> tests:   <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">5</span>.806 seconds</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Complete requests:      <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">100</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Failed requests:        <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">0</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Non-2xx responses:      <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">100</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Total transferred:      <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">52046500</span> bytes</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">HTML transferred:       <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">52024900</span> bytes</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Requests per second:    <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">17</span>.22 [<span style="box-sizing: border-box;color: rgb(98, 151, 85) !important">#/sec] (mean)</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Time per request:       <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">580</span>.583 [ms] (mean)</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Time per request:       <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">58</span>.058 [ms] (mean, across all concurrent requests)</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Transfer rate:          <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">8754</span>.41 [Kbytes/sec] received</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">...<span style="box-sizing: border-box;color: rgb(98, 151, 85) !important">#省略</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Percentage of the requests served within a certain time (ms)</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">  <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">50</span>%    <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">574</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">  <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">66</span>%    <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">587</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">  <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">75</span>%    <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">596</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">  <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">80</span>%    <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">607</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">  <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">90</span>%    <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">642</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">  <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">95</span>%    <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">668</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">  <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">98</span>%    <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">677</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">  <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">99</span>%    <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">686</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">100</span>%    <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">686</span> (longest request)</span>

可以看到每秒处理请求是 17.22个。

基于swoole的压力测试

<span style="color: rgb(255, 255, 255);">ab -n 500 -c 100 http://laravel-s.com/</span><br></br>
<span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Server Software:        nginx/1.18.0</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Server Hostname:        laravel-s.com</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Server Port:            <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">80</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">...<span style="box-sizing: border-box;color: rgb(98, 151, 85) !important">#省略</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Concurrency Level:      <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">100</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Time taken <span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">for</span> tests:   <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">0</span>.803 seconds</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Complete requests:      <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">500</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Failed requests:        <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">0</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Total transferred:      <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">9254500</span> bytes</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">HTML transferred:       <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">8729000</span> bytes</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Requests per second:    <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">622</span>.59 [<span style="box-sizing: border-box;color: rgb(98, 151, 85) !important">#/sec] (mean)</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Time per request:       <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">160</span>.620 [ms] (mean)</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Time per request:       <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">1</span>.606 [ms] (mean, across all concurrent requests)</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Transfer rate:          <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">11253</span>.38 [Kbytes/sec] received</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">...<span style="box-sizing: border-box;color: rgb(98, 151, 85) !important">#省略</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">Percentage of the requests served within a certain time (ms)</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">  <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">50</span>%    <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">138</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">  <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">66</span>%    <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">144</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">  <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">75</span>%    <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">161</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">  <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">80</span>%    <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">167</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">  <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">90</span>%    <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">204</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">  <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">95</span>%    <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">229</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">  <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">98</span>%    <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">240</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">  <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">99</span>%    <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">244</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">100</span>%    <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">273</span> (longest request)</span>

可以看到基于swoole服务器之后的处理请求数是622.59。

总结

从中可以看到基于swoole之后很明显的优化,处理请求数大大提高。php-fmp是17.22,swoole是622.59。差距非常大。

配置的过程中遇到了一些错误,主要是配置虚拟主机的错误,好在都解决了。如果你也遇到相似的问题,欢迎一起讨论学习。

我是小白,期待和优秀的你一起同行!

小白

2020年11月01日

请登录后再评论