XHProf:facebook之PHP轻量级的性能分析工具
XHProf简介:
XHProf是facebook开源出来的一个php轻量级的性能分析工具,跟Xdebug类似,但性能开销更低,还可以用在生产环境中,也可以由程序开关来控制是否进行profile。
安装xhprof:
# wget http://pecl.php.net/get/xhprof-0.9.2.tgz # tar zxf xhprof-0.9.2.tgz # cd xhprof-0.9.2 # cp -r xhprof_html xhprof_lib /www/www.hx.com/xhprof/ # cd extension/ # /usr/local/webserver/php/bin/phpize # ./configure –with-php-config=/usr/local/webserver/php/bin/php-config # make && make install
安装完提示:
Installing shared extensions: /usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/
配置文件php.ini中添加:
extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/" extension=xhprof.so xhprof.output_dir=/www/logs/xhprof #分析日志输出在/www/logs/xhprof目录
重新加载php配置文件和重启web服务器:
/usr/local/webserver/php/sbin/php-fpm reload /usr/local/webserver/nginx/sbin/nginx -s reload
查看xhprof扩展是否已经安装:
/path/to/bin/php -m | grep -i xhprof
安装画图工具graphviz:
# wget http://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 # ./configure # make && make install
程序示例头部:
<?php xhprof_enable(); //xhprof_enable(XHPROF_FLAGS_NO_BUILTINS); 不记录内置的函数 //xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY); 同时分析CPU和Mem的开销 $xhprof_on = true; 我觉得用xhprof_enable();就够用了,只统计运行时间(Wall Time)。 生产环境可使用: if (mt_rand(1, 10000) == 1) { xhprof_enable(); $xhprof_on = true; }
程序示例尾部:
<?php if($xhprof_on) { $xhprof_data = xhprof_disable(); $xhprof_root = '/www/www.hx.com/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(); $run_id = $xhprof_runs->save_run($xhprof_data, "hx"); echo '<a href="http://www.blogdaren.com/xhprof/xhprof_html/index.php?run='.$run_id.'&source=hx" target="_blank">统计</a>'; }运行程序,底部出现统计字样,点过去就可以看到性能分析了。按运行时间排序,很容易找出化时间最长的函数。
点击 [View Full Callgraph] 图形化显示,最大的性能问题会用红色标出,其次是黄色,很明显。
官方文档:http://mirror.facebook.net/facebook/xhprof/doc.html
名词解释:
Inclusive Time (或子树时间):包括子函数所有执行时间。 Exclusive Time/Self Time:函数执行本身花费的时间,不包括子树执行时间。 Wall时间:花去了的时间或挂钟时间。 CPU时间:用户耗的时间+内核耗的时间 Function Name 函数名 Calls 调用次数 Calls% 调用百分比 Incl. Wall Time (microsec) 调用的包括子函数所有花费时间 以微秒算(一百万分之一秒) IWall% 调用的包括子函数所有花费时间的百分比 Excl. Wall Time (microsec) 函数执行本身花费的时间,不包括子树执行时间,以微秒算(一百万分之一秒) EWall% 函数执行本身花费的时间的百分比,不包括子树执行时间 Incl. CPU(microsecs) 调用的包括子函数所有花费的cpu时间。减Incl. Wall Time即为等待cpu的时间减Excl. Wall Time即为等待cpu的时间 ICpu% Incl. CPU(microsecs)的百分比 Excl. CPU(microsec) 函数执行本身花费的cpu时间,不包括子树执行时间,以微秒算(一百万分之一秒)。 ECPU% Excl. CPU(microsec)的百分比 Incl.MemUse(bytes) 包括子函数执行使用的内存。 IMemUse% Incl.MemUse(bytes)的百分比 Excl.MemUse(bytes) 函数执行本身内存,以字节算 EMemUse% Excl.MemUse(bytes)的百分比 Incl.PeakMemUse(bytes) Incl.MemUse的峰值 IPeakMemUse% Incl.PeakMemUse(bytes) 的峰值百分比 Excl.PeakMemUse(bytes) Excl.MemUse的峰值 EPeakMemUse% EMemUse% 峰值百分比
版权声明:除非注明,本文由( blogdaren )原创,转载请保留文章出处。
发表评论: