php开启opcache前后性能差异

本打算发帖到v2ex上求助,结果自己碰巧解决了,因此在博客上记录一下完整的从发现到解决的过程 环境 硬件配置:腾讯云的2核4G服务器 系统:ubuntu server 16.04 LTS lnmp:通过docker-compose做编排的php-fpm7.2(基于alpine)+nginx(官方镜像)+mysql5.7(官方镜像) 缓存:redis 框架:laravel5.5 路由大概 之前QPS 不改变入口文件,请求http://hostname/,QPS:19-21 不改变入口文件,请求http://hostname/任意路径,QPS:18-19 在入口文件第二行直接die();,请求http://hostname/,QPS:240-250 已做过的调试 开启了php-fpm的slowlog记录(并确保其正常工作:docker开启了cap_add: SYS_PTRACE),但通过压测工具并未发现slowlog有任何记录(代码里加sleep(2)会记录到slowlog里) 已经用过laravel自带的配置缓存,路由缓存以及optimize等命令 通过htop查看系统负载发现cpu是被php-fpm pool www这个command跑满的,因此可以排除其他原因 其他说明 服务器通过nginx监听80端口对来源请求进行反向代理至php所在容器进行处理,所有容器中只有nginx对外开放了80和443,其他都是容器内部通过container_name进行访问 问题 可以从哪些方面着手提高QPS? 问题解决-开启opcache opcaches是什么 以下是开启opcache和关闭后QPS的对比 #开启opcache(Dockerfile中使用docker-php-ext-install opcache) #php.ini配置文件中开启opcache相关配置 ➜ ~ http_load -p 80 -s 30 url 2201 fetches, 80 max parallel, 1.33667e+07 bytes, in 30 seconds 6073 mean bytes/connection 73.3667 fetches/sec, 445556 bytes/sec msecs/connect: 4.99642 mean, 32.34 max, 2.791 min msecs/first-response: 1066.44 mean, 2130.71 max, 52.395 min HTTP response codes: code 200 -- 2201 ➜ ~ http_load -p 80 -s 30 url 2389 fetches, 80 max parallel, 1.45084e+07 bytes, in 30 seconds 6073 mean bytes/connection 79.6333 fetches/sec, 483613 bytes/sec msecs/connect: 4.92044 mean, 41.46 max, 2.818 min msecs/first-response: 980.024 mean, 1819.01 max, 42.171 min HTTP response codes: code 200 -- 2389 #关闭opcache(未安装opcache) ➜ ~ http_load -p 80 -s 30 url 555 fetches, 80 max parallel, 3.37052e+06 bytes, in 30 seconds 6073 mean bytes/connection 18.5 fetches/sec, 112350 bytes/sec msecs/connect: 5.68439 mean, 31.76 max, 2.826 min msecs/first-response: 4035.69 mean, 5985 max, 120.81 min HTTP response codes: code 200 -- 555 ➜ ~ http_load -p 80 -s 30 url 559 fetches, 80 max parallel, 3.39481e+06 bytes, in 30 seconds 6073 mean bytes/connection 18.6333 fetches/sec, 113160 bytes/sec msecs/connect: 5.78894 mean, 18.31 max, 2.944 min msecs/first-response: 3970.26 mean, 5100.76 max, 163.014 min HTTP response codes: code 200 -- 559 然后通过缓存配置&&路由缓存&&优化io读写&&优化自动加载,将QPS提升到了90+ ...

January 17, 2019 · 2 min · 422 words

docker安装php7.2记录

问题 准备通过Dockerfile构建php镜像,在使用docker-php-ext-install安装mcrypt扩展时遇到如下错误: error: /usr/src/php/ext/mcrypt does not exist 原因 mycrypt extension is not provided with the PHP source since 7.2 , but are instead available through PECL. To install a PECL extension in docker, use pecl install to download and compile it, then use docker-php-ext-enable to enable it: php从7.2开始不再在源码里支持mcrypt扩展而转到pecl方式支持 pecl install mcrypt-1.0.1 docker-php-ext-enable mcrypt 解决 版本切换到7.1再执行构建时问题没有复现,同时按照 pecl install mcrypt-1.0.1 docker-php-ext-enable mcrypt 的方式安装之后也没有出现上述问题 问题 安装过程中提示(虽然不会影响最终结果) WARNING: Ignoring APKINDEX.e3d33561.tar.gz: No such file or directory 解决 doing apk update fixes the problem (updates the index). ...

September 21, 2018 · 2 min · 310 words

tp3.2.3实现支持点击排序

假设: Admin/Home/Controller/BaseController.class.php是一个基础控制器 $current_params和$in是两个在Admin/Home/Controller/BaseController.class.php中用来保存接收参数的属性,并且已经在构造函数中对其赋值 Admin/Tpl/Index/footer.html是布局中的公共部分 Public/Model/BaseModel.class.php是公共模型 请求中用来表示模块,控制器,操作的参数名称分别为:m,c,a #searchForm和#excelForm分别为条件搜索form和excel表单导出请求提交时用来临时保存和传递筛选条件的form 全局改动 Admin/Home/Controller/BaseController.class.php改动: 新增protected $current_params属性 _initialize方法尾部新增以下代码(用于处理传入的有效参数): unset($this->current_params['m']); unset($this->current_params['c']); unset($this->current_params['a']); $this->assign('page_url', U(CONTROLLER_NAME.'/'.ACTION_NAME,$this->current_params)); 控制器尾部新增以下方法: /* * todo:处理排序请求 * @param $sort string 用来mysql排序的字符串 * @param $column string 用来指定排序字段名称 * @param $value int 排序值,1:顺序;2:倒序 * @param $table string 需要排序字段在当前sql语句中表的别名 * @return string 返回处理后的$order * */ protected function assembleSort($sort,$column,$value,$table){ $order = urldecode($sort); $v_sort[$column] = $value == 1 ? 2 : 1; $v_sort['param_sort'] = $sort; $v_sort['param_column'] = $column; $v_sort['param_value'] = $v_sort[$column]; $v_sort['param_table'] = $table; $this->assign('sort', $v_sort); return $order; } Admin/Tpl/Index/footer.html改动: 在</body>标签前新增以下代码: html(保存当前页面并携带除排序相关参数的url): ...

September 14, 2017 · 2 min · 338 words