详解PHP7开启OPcache和Swoole性能的提升对比

admin3年前PHP教程30
目录
前期准备不开启opcache和laravel开启OPcache使用swoole加速包总结
前期准备

测试所用的主机为虚拟机,虚拟机配置在双核4GB的个人电脑中。虚拟机系统为linux,http服务器采用nginx,用lnmp脚本安装nginx、mysql、php。Laravel框架为7.X版本。

配置站点,在nginx的server块中配置虚拟主机

server{ listen 80; root "/vagrant/www/laravel7/public"; server_name test.laravel; index index index.php; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }

编辑/etc/hosts文件,在新行添加127.0.0.1 test.laravel

打开项目,新建控制器TestController,在里面新建一个test方法:


<?php
namespace App\Http\Controllers;
 
 
 
class TestController extends Controller
{
    public function test()
    {
        return 123;
    }
 
    
}

在routes/api.php中注册一个路由:


Route::get('test', 'TestController@test');

在app/Http/Kernel文件中,关掉频率限制中间件throttle。

不开启opcache和laravel

修改php-fpm.conf文件,修改pm和pm.max_children 配置,pm设置为static,pm.max_children设置为50,以获得较好的并发性能。

[www] listen = /tmp/php-cgi.sock listen.backlog = -1 listen.allowed_clients = 127.0.0.1 listen.owner = www listen.group = www listen.mode = 0666 user = www group = www pm = static pm.max_children = 50 pm.start_servers = 10 pm.min_spare_servers = 10 pm.max_spare_servers = 20 request_terminate_timeout = 100 request_slowlog_timeout = 0 slowlog = var/log/slow.log

重启fpm后用ab压测:ab -n 1000 -c 100 test.laravel/api/test

Server Software:        nginx

Server Hostname:        test.laravel

Server Port:            80

Document Path:          /api/test

Document Length:        3 bytes

Concurrency Level:      100

Time taken for tests:   148.306 seconds

Complete requests:      1000

Failed requests:        0

Total transferred:      253000 bytes

HTML transferred:       3000 bytes

Requests per second:    6.74 [#/sec] (mean)

Time per request:       14830.553 [ms] (mean)

Time per request:       148.306 [ms] (mean, across all concurrent requests)

Transfer rate:          1.67 [Kbytes/sec] received

此时的并发大约为为 7 qps

开启OPcache

在配置文件php.ini文件中开启opcache

zend_extension="opcache.so" opcache.enable=1 opcachemory_consumption=128 opcache.max_accelerated_files=10000 opcache.revalidate_freq=60 opcache.fast_shutdown=1 opcache.enable_cli=1 opcache.interned_strings_buffer=8

重启fpm后,用ab压测:ab -n 1000 -c 100 test.laravel/api/test

Server Software:        nginx

Server Hostname:        test.laravel

Server Port:            80

Document Path:          /api/test

Document Length:        4 bytes

Concurrency Level:      100

Time taken for tests:   11.006 seconds

Complete requests:      1000

Failed requests:        0

Total transferred:      254000 bytes

HTML transferred:       4000 bytes

Requests per second:    90.86 [#/sec] (mean)

Time per request:       1100.590 [ms] (mean)

Time per request:       11.006 [ms] (mean, across all concurrent requests)

Transfer rate:          22.54 [Kbytes/sec] received

Connection Times (ms)

              min  mean[+/-sd] median   max

Connect:        0    1   4.3      0      16

Processing:   409 1069 152.0   1092    1414

Waiting:      408 1069 152.0   1092    1414

Total:        424 1070 149.6   1092    1414

Percentage of the requests served within a certain time (ms)

  50%   1092

  66%   1126

  75%   1149

  80%   1162

  90%   1203

  95%   1242

  98%   1280

  99%   1309

 100%   1414 (longest request)

此时的达到了 90qps,性能是未开启时的 10 倍以上!。

使用swoole加速包

开源的laravel-swoole加速包

在项目目录下运行composer命令安装;在nginx的配置文件中配置,将请求转发到swoole监听的端口。

用 ab 压测 : ab -n 1000 -c 100 test.laravel/api/test

Server Software:        nginx

Server Hostname:        test.laravel

Server Port:            80

Document Path:          /api/test

Document Length:        4 bytes

Concurrency Level:      100

Time taken for tests:   1.158 seconds

Complete requests:      1000

Failed requests:        0

Total transferred:      225000 bytes

HTML transferred:       4000 bytes

Requests per second:    863.46 [#/sec] (mean)

Time per request:       115.813 [ms] (mean)

Time per request:       1.158 [ms] (mean, across all concurrent requests)

Transfer rate:          189.72 [Kbytes/sec] received

速度起飞!达到了800qps!

也就是一百多倍?

总结

当然这只是一个比较简单的测试,但是总的来说opcache扩展和swoole扩展对php脚本性能的提升还是很明显的。

以上就是详解PHP7开启OPcache和Swoole性能的提升对比的详细内容,更多关于PHP7开启OPcache和Swoole性能的提升对比的资料请关注其它相关文章!

免责声明:本文内容来自用户上传并发布,站点仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。请核实广告和内容真实性,谨慎使用。

相关文章

GPU服务器选择哪个品牌好美国GPU服务器购买配置选择

GPU服务器是什么?GPU服务器又被称为显卡服务器,是基于GPU的应用于视频编解码、深度学习、科学计算等多种场景的快速、稳定、弹性的计算服务,我们提供和标准云服务器一致的管理方式。GPU服务器出色的图...

美国高防服务器租用选择的方法

美国高防服务器租用选择的方法?选择适合的美国高防服务器租用服务商需要考虑以下几个因素:信誉度和口碑:选择口碑良好、信誉度高的服务商,可以避免一些不必要的风险和麻烦。可以通过查看客户评价、咨询其他用户等...

国内企业选择马来西亚服务器的原因?

国内企业选择马来西亚服务器的原因可能有以下几个方面:地理位置和网络连接:如果企业的目标用户或客户位于马来西亚或东南亚地区,那么选择在当地租用服务器可以降低访问延迟、提高网站速度和性能,从而提升用户体验...

laravel的数据表填充器使用详解

一、数据表填充器填充操作就是往数据表中写测试数据的操作(增加操作),在开发阶段是很实用的功能。1.1 填充器(种子文件)的创建与编写1.1.1、填充器默认的所在目录1.1.2、创建填充器php art...

10个常见网站安全攻击手段及防御方法?美国高防服务器如何防御网络攻击?

在某种程度上,互联网上的每个网站都容易遭受安全攻击。从人为失误到网络罪犯团伙发起的复杂攻击均在威胁范围之内。网络攻击者最主要的动机是求财。无论你运营的是电子商务项目还是简单的小型商业网站,潜在攻击的风...

日本服务器租用价格多少

日本服务器租用价格多少?影响日本服务器租用价格的因素有以下几点:服务器配置:服务器配置越高,租用价格相对越贵。例如,CPU、内存、硬盘等方面都会影响价格。带宽带宽是指网络连接速度,也是影响价格的因素之...