对于想了解docker使用xhprof进行thinkphp性能分析的读者,本文将提供新的信息,我们将详细介绍dockerthinkphp6,并且为您提供关于docker-如何收集分布式容器化PHP应用
对于想了解docker 使用 xhprof 进行 thinkphp 性能分析的读者,本文将提供新的信息,我们将详细介绍docker thinkphp6,并且为您提供关于docker - 如何收集分布式容器化PHP应用的xhprof分析结果、PHP 7.1安装xhprof进行性能分析的介绍、php 性能分析工具 xhprof、PHP 性能分析工具XHProf使用的有价值信息。
本文目录一览:- docker 使用 xhprof 进行 thinkphp 性能分析(docker thinkphp6)
- docker - 如何收集分布式容器化PHP应用的xhprof分析结果
- PHP 7.1安装xhprof进行性能分析的介绍
- php 性能分析工具 xhprof
- PHP 性能分析工具XHProf使用
docker 使用 xhprof 进行 thinkphp 性能分析(docker thinkphp6)
一、小试牛刀
1. 环境准备
进入 docker,执行下面动作安装扩展
wget https://pecl.php.net/get/xhprof-2.3.9.tgz
tar xvf xhprof-2.3.9.tgz
mv xhprof-2.3.9/extension /usr/src/php/ext/xhprof
docker-php-ext-install xhprof
2. 运行测试
代码参考:https://zhuanlan.zhihu.com/p/351056021
中间显示 [View Full Callgraph] 报错:failed to execute cmd: " dot -Tsvg". stderr: `sh: dot: not found ''
, 解决方法参考: 缺少包 graphviz
apk add graphviz
二、thinkphp 使用
代码参考:https://blog.csdn.net/weixin_29001327/article/details/116264651 不过上面的代码有点乱,可以参考我下面的代码。 application/tags.php
// 应用行为扩展定义文件
return [
// 应用初始化
''app_init'' => [
],
// 应用开始
''app_begin'' => [
''app\\common\\behavior\\XhprofAnalyze'',
''app\\common\\behavior\\Logrecord''
],
// 模块初始化
''module_init'' => [],
// 操作开始执行
''action_begin'' => [],
// 视图内容过滤
''view_filter'' => [],
// 日志写入
''log_write'' => [],
// 应用结束
''app_end'' => [
''app\\common\\behavior\\XhprofAnalyze'',
''app\\common\\behavior\\Logrecord''
],
];
application/common/behavior/XhprofAnalyze.php
<?php
namespace app\common\behavior;
class XhprofAnalyze
{
public function appBegin($params)
{
xhprof_enable(XHPROF_FLAGS_NO_BUILTINS | XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY);
save_log(''xhprof enable'', 1, ''开始xhprof'', ''xhprof_analyze'');
}
public function appEnd($params)
{
$xhprof_data = xhprof_disable();
$XHPROF_ROOT = realpath(dirname(env(''PUB_PATH'')) . ''/xhprof'');
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";
$xhprof_runs = new \XHProfRuns_Default();
// save the run under a namespace "xhprof_foo"
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");
$site_url = ''http://mall-api.test''; //config(''custom.site_url'');
save_log([''run_id''=>$run_id], 1, ''结束xhprof'', ''xhprof_analyze'');
echo "<a href=''{$site_url}/xhprof/xhprof_html/index.php?run={$run_id}&source=xhprof_foo''>查看</a>\n";
}
}
如果发现 appEnd 没有执行,请检查控制里返回 json 之后是否执行了 exit () 之类的操作终止进程。也可以考虑给 xhprof_disable 放到其它的 hook 中。
docker - 如何收集分布式容器化PHP应用的xhprof分析结果
我有一些服务是利用
最近想集中收集每个容器里的1%流量的xhprof分析结果,不知道有什么可行的方案?
回复内容:
我有一些服务是利用docker容器化的php服务,是在mesos集群上分布式部署的。服务容器化后,我将所有容器里的日志(包括nginx和php)都通过syslog forward到一个日志服务器,然后在日志服务器上通过logstash将日志push到es,elk实时分析nginx和php的日志。
最近想集中收集每个容器里的1%流量的xhprof分析结果,不知道有什么可行的方案?
PHP 7.1安装xhprof进行性能分析的介绍
这篇文章主要介绍了关于php 7.1安装xhprof进行性能分析的介绍,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
安装扩展
该 xhprof扩展版本是从 https://github.com/longxinH/xhprof 获取的(第三方的一个库,官方版本不支持php7)
下载并编译xhprof扩展
在web的html目录下操作:
git clone https://github.com/longxinH/xhprof
编译扩展
cd xhprof/extension/phpize ./configure makemake install
修改php.ini配置
立即学习“PHP免费学习笔记(深入)”;
[xhprof] extension=xhprof.so; xhprof.output_dir=/tmp/xhprof
其中 xhprof.output_dir 是 xhprof 的输出目录,每次执行 xhprof 的 save_run 方法时都会生成一个 run_id.project_name.xhprof 文件。这个目录在哪里并不重要。注意此路径的权限要可读写!!否则文件无法生成成功
重启 php-fpm
sudo service php7.1-fpm restart
添加测试代码
<?php xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);// 要检查性能的代码 $xhprof_data = xhprof_disable(); include_once '/var/www/html/xhprof/xhprof_lib/utils/xhprof_lib.php'; include_once '/var/www/html/xhprof/xhprof_lib/utils/xhprof_runs.php'; $xhprof_runs = new \XHProfRuns_Default(); $run_id = $xhprof_runs->save_run($xhprof_data, 'your_project');
测试代码中要引入xhprof_lib.php和xhprof_runs.php两个文件
查看生成报告
需要访问:xhprof/xhprof_html/index.php文件查看:
http://localhost/xhprof/xhprof_html/index.php?run=5b35d3dfa8c29&source=your_project
run后的参数为$run_id,source参数为your_project配置的名字
如果图表生成错误,需要安装插件:
sudo apt-get install graphviz
实际演示代码
<?php function test1(){ for($i=0;$i<10;$i++){ echo 'aaa'.$i.'<br>'; } }// start profilingxhprof_enable(); test1(); // stop profiler $xhprof_data = xhprof_disable(); // display raw xhprof data for the profiler runprint_r($xhprof_data); include_once "xhprof_lib.php";include_once "xhprof_runs.php"; // save raw data for this profiler run using default // implementation of iXHProfRuns. $xhprof_runs = new XHProfRuns_Default(); // save the run under a namespace "xhprof_test" $run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_test");echo "---------------\n". "Assuming you have set up the http based UI for \n". "XHProf at some address, you can view run at \n". "http://<xhprof-ui-address>/index.php?run=$run_id&source=xhprof_test\n". "---------------\n";
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
LAMP、LNMP和LNAMP的区别和安装
使用Wamp搭建Php本地开发环境以及HBuilder调试的方法
以上就是PHP 7.1安装xhprof进行性能分析的介绍的详细内容,更多请关注php中文网其它相关文章!
php 性能分析工具 xhprof

安装方法:
wget http://pecl.php.net/get/xhprof-0.9.x.tgz
cp xhprof-0.9.x.tgz /home/www/xhprof.tgz
tar zxvf /home/www/xhprof.tgz
cd xhprof/extension
phpize
./configure --enable-xhprof --with-php-config=/usr/bin/php-config
make && make install
vim /etc/php.ini
[xhprof]
extension=xhprof.so
;下面的这个地址是用来保存测量记录的目录,在页面输出测量得到的数据的时候,它会自动到这儿来找输出的文件。
xhprof.output_dir=/tmp
service php-fpm restart
ok!
使用方法:
<?php
//来自xhprof的例子
function bar($x) {
if ($x > 0) {
bar($x - 1);
}
}
// 在代码开头加上
xhprof_enable();
// xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);这个还可以分析cpu和内存使用情况
// 你的程序
foo();
// 在代码结尾加上
$xhprof_data = xhprof_disable();
// xhprof的数据
print_r($xhprof_data);
//这里是xhprof的地址,echo出来直接复制地址访问可以查看报告
$XHPROF_ROOT = realpath(dirname(__FILE__) .''/..'');
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";
// save raw data for this profiler run using default
// implementation of iXHProfRuns.
$xhprof_runs = new XHProfRuns_Default();
// save the run under a namespace "xhprof_foo"
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");
echo "---------------\n".
"Assuming you have set up the http based UI for \n".
"XHProf at some address, you can view run at \n".
"http://<xhprof-ui-address>/index.php?run=$run_id&source=xhprof_foo\n".
"---------------\n";
PHP 性能分析工具XHProf使用
来自: http://blog.csdn.net//clh604/article/details/21934453
当php程序逻辑执行很复杂的时候,可能会带来性能上的问题,为了有效的找到影响性能的代码,推荐大家使用PHP新能分析工具XHProf,该工具能有效的分析每段代码的执行情况,非常好用
1、安装配置PHP的扩展XHProf
$ wget https://github.com/facebook/xhprof/tarball/master -O xhprof.tar.gz$ tar zxf xhprof.tar.gz$ cd facebook-xhprof-b8c76ac/extension/# phpize# ./configure --with-php-config=`/path/to/php-config`# make && make install# make test# vi /etc/php.d/xhprof.ini; 内容为:extension = xhprof.so; 注意:output_dir 必须存在且可写xhprof.output_dir = /tmp/xhpro然后重启apache服务# service php-fpm restart 或 service httpd restart
2、使用XHProf
// start profilingxhprof_enable();// run program....// stop profiler$xhprof_data = xhprof_disable();//// Saving the XHProf run// using the default implementation of iXHProfRuns.//include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";$xhprof_runs = new XHProfRuns_Default();// Save the run under a namespace "xhprof_foo".//// **NOTE**:// By default save_run() will automatically generate a unique// run id for you. [You can override that behavior by passing// a run id (optional arg) to the save_run() method instead.]//$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");echo "---------------\n"."Assuming you have set up the http based UI for \n"."XHProf at some address, you can view run at \n"."http://<xhprof-ui-address>/index.php?run=$run_id&source=xhprof_foo\n"."---------------\n";如此一来,会在上面设定的xhprof.output_dir目录里生成名字类似49bafaa3a3f66.xhprof_foo的数据文件,可以很方便的通过Web方式浏览效果:
立即学习“PHP免费学习笔记(深入)”;
3、展示输出结果
在 xhprof 源码包中提供了xhprof_html 和 xhprof_lib 两个文件夹,xhprof_lib是用于 PHP 开发,而xhprof_html用于显示 xhprof 分析结果的 web 界面,访问形式如下
今天的关于docker 使用 xhprof 进行 thinkphp 性能分析和docker thinkphp6的分享已经结束,谢谢您的关注,如果想了解更多关于docker - 如何收集分布式容器化PHP应用的xhprof分析结果、PHP 7.1安装xhprof进行性能分析的介绍、php 性能分析工具 xhprof、PHP 性能分析工具XHProf使用的相关知识,请在本站进行查询。
本文标签: