翻译:陈笑笑 chenxiaoxiao@shopex.cn2012年01月12日 14:50:18
官方文档地址:
https://mirror.facebook.net/facebook/xhprof/doc.html徐仁禄 <xurenlu [at] gmail.com> 翻译的
https://www.162cm.com/p/xhprofdoc.html
XHProf 是 FaceBook 开发的一个函数级别的 PHP 分层分析器。 数据收集部分是一个基于 C 的 PHP 扩展,分析报告是一系列基于 PHP 的 HTML 导航页面。 XHProf 能统计每个函数的调用次数、内存使用、CPU占用等多项重要的数据。 并且 XHProf 还能比较两个统计样本,或从多个数据样本中汇总结果。 XHProf 是分析 PHP 程序执行效率的利器,能让我们得到更底层的的分析数据。
安装之前这里说下我把第三方编译的程序都放在根目录下的 opt 目录下了, 下面用 wget 下载程序之前先进入
cd /opt
下面的步骤,应该在Linux / Unix环境下进行命令令安装
wget https://pecl.php.net/get/xhprof-0.9.2.tgz tar zxvf xhprof-0.9.2.tgz cd xhprof-0.9.2 cp -r xhprof_html xhprof_lib <这里是你的webroot目录> cd extension # 执行以下命令需要root权限,如ubuntu系统需要在命令前面加上 sudo phpize //执行phpize,比如cent os里面在 /usr/local/php/bin/phpize ./configure --with-php-config=<path to php-config> # ubuntu里边是 /usr/bin/php-config make make install make test
我们还需要修改 php.ini 来确保 XHProf 的正常运行。 需要建立一个目录用来存储分析数据文件
[xhprof] extension=xhprof.so xhprof.output_dir=<这里是放xhprof_log文件的目录>重启服务让修改生效,现在就可以使用XHProf了。不过为了查看更加清晰的显示效果,继续安装Graphviz。
重启命令如下:
# 重启 nginx sudo service nginx restart # 重启 php-cgi p.s.我们要做的是先杀死进程, 然后在启动 sudo killall -HUP php5-cgi sudo /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid
wget https://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.24.0.tar.gz tar zxf graphviz-2.24.0.tar.gz cd graphviz-2.24.0 # 执行以下命令需要root权限,如Ubuntu系统需要在命令前面加上 sudo ./configure make make install
安装完成后,会生成/usr/local/bin/dot文件,你应该确保路径在PATH环境变量里,以便XHProf能找到它。 点View Full Callgraph图形化显示,最大的性能问题会用红色标出,其次是黄色,很明显。
我们用的操作系统是 ubuntu 11.04
查看View的时候,提示:
Error: either we can not find profile data for run_id 4d7f0bd99a12f or the threshold 0.01 is too small or you do not have ‘dot’ image generation utility installed.关于dot的介绍 https://mirror.facebook.net/facebook/xhprof/doc.html
dot (image generation utility): The callgraph image visualization ([View Callgraph]) feature relies on the presence of Graphviz “dot” utility in your path. “dot” is a utility to draw/generate an image for a directed graph.
原因是xhprof绘制的是png图,graphviz-2.24.0不支持。绘图的dot拓展没装成功, 不支持PNG。
1. 编译安装 libpng
sudo wget https://sourceforge.net/projects/libpng/files/libpng15/1.5.1/libpng-1.5.1.tar.gz sudo ./configure sudo make sudo make install
2. 重装 graphviz
完成之后你就可以再去看一看View啦!
使用 XHProf 非常简单,我们只需要修改少许代码即可实现。
例如,我们需要分析代码执行时关于 CPU 和内存的数据。
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY); // Test Code Start ………… // Test Code End $xhprofData = xhprof_disable(); include_once "/data/ecos.cn/xhprof_lib/utils/xhprof_lib.php"; include_once "/data/ecos.cn/xhprof_lib/utils/xhprof_runs.php"; $xhprofRuns = new XHProfRuns_Default(); $xhprofRuns->save_run($xhprofData, "xhprof");
这样,当我们运行程序之后,会在 /data/ecos.cn/xhprof/ 之中生成分析数据文件。 文件名类似于 4b4c239a86593.xhprof ,我们可以通过改变 save_run 的参数,来改变文件后缀。 由于分析可能会影响响应速度,通常我们会加上一个随机数,随机取样,而不是分析所有用户请求的执行过程。
$randKey = mt_rand(1, 10000); if ($randKey == 1) { xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY); } // Test Code Start ………… // Test Code End if ($randKey == 1) { $xhprofData = xhprof_disable(); include_once "/data/ecos.cn/xhprof_lib/utils/xhprof_lib.php"; include_once "/data/ecos.cn/xhprof_lib/utils/xhprof_runs.php"; $xhprofRuns = new XHProfRuns_Default(); $xhprofRuns->save_run($xhprofData, "xhprof"); }
例如,我的域名是 ecos.cn,现在我们就可以通过 web 来查看详细的分析数据了。
1. 查看单个报告。
# 我们想要查看 4b4c239a86593.xhprof 这个报告的详细信息,查看链接如下: https://ecos.cn/xhprof_html/index.php?run=4b4c239a86593&source=xhprof
2. 比较两个报告。
# 我们想比较 4b4c239a86593.xhprof 和 4b4c2645794f0.xhprof 两个报告,查看链接如下: https://ecos.cn/xhprof_html/index.php?run1=4b4c239a86593&run2=4b4c2645794f0&source=xhprof我们可以看到精确到函数级的分析数据,包括调用次数、CPU、内存等,还可以不断的向下跟踪。
相信如此详细的数据,将会给程序优化工作,带来巨大的帮助和便利。
仅供参考
使用XHProf, 在你要监测的Php代码头尾部分别加入代码xhprof_enable()和xhprof_disable()
php代码
<?php
// start profiling
xhprof_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".
"https://<xhprof-ui-address>/index.php?run=$run_id&source=xhprof_foo\n".
"---------------\n";
php代码
<?php
// start profiling
xhprof_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".
"https://<xhprof-ui-address>/index.php?run=$run_id&source=xhprof_foo\n".
"---------------\n";
如此一来,会在上面设定的 xhprof.output_dir 目录里生成名字类似 49bafaa3a3f66.xhprof_foo 的数据文件,可以很方便的通过Web方式浏览效果:
https://<xhprof-ui-address>/index.php?run=49bafaa3a3f66&source=xhprof_foo
目前显示的是表格形式的显示,点击页面上的__View Full Callgraph__,就能看到精美的图片显示了。