Home  >  Article  >  Backend Development  >  Xhprof php性能测试用具用法小结

Xhprof php性能测试用具用法小结

WBOY
WBOYOriginal
2016-06-13 13:16:171115browse

Xhprof php性能测试工具用法小结

            Xhprof 是facebook推出的轻量级的php性能分析工具,和Xdebug相比,XHProf更加易用和可控,尤其是生成流程图和调试数据对比的功能很好很强大。  下面简单介绍下Xhprof的安装及使用。

           1、安装

          可以从页面 下载相应版本及了解各个版本依赖条件。 安装前,先假设php的bin目录是:/usr/local/php/bin

[root@localhost extension]# ln -s /usr/local/php/bin/phpize /usr/bin/phpize
[root@localhost tool]# wget http://pecl.php.net/get/xhprof-0.9.2.tgz
[root@localhost tool]# tar -zxvf xhprof-0.9.2.tgz 
[root@localhost tool]# cd xhprof-0.9.2/extension/
[root@localhost tool]# ./configure --with-php-config=/usr/local/php/bin/php-config
[root@localhost tool]# make
[root@localhost tool]# make install

           通过上面的操作,如果运气不是非常糟糕的话,你应该看到类似这样的提示:Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/

          进入到php扩展库目录/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/ ,你将看到xhprof.so 共享库文件。

        2、配置

         在php.ini文件中,添加如下配置:

[xhprof]
extension=xhprof.so;
; directory used by default implementation of the iXHProfRuns
; interface (namely, the XHProfRuns_Default class) for storing
; XHProf runs.
;
;xhprof.output_dir=<directory_for_storing_xhprof_runs>
xhprof.output_dir=/tmp/xhprof

        重启下php,就可以在使用xhprof了。

       3、拷贝文件

       把xhprof-0.9.2目录下的目录xhprof_html 和 xhprof_lib 下的所有文件拷贝到你网站的主目录下,假设网站主目录:/opt/wordpress

[root@localhost xhprof-0.9.2]# cp -R xhprof_* /opt/wordpress

       4、使用

       参考下面的解释说明:

<?php
//cpu: XHPROF_FLAGS_CPU
//内存: XHPROF_FLAGS_MEMORY
//如果都监控用XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

//需要性能分析的php代码

//define('WP_USE_THEMES', true);


//require('./wp-blog-header.php');

//停止性能分析
$data = xhprof_disable(); 


//显示性能分析数据
include_once "xhprof_lib/utils/xhprof_lib.php"; 
include_once "xhprof_lib/utils/xhprof_runs.php"; 
$objXhprofRun = new XHProfRuns_Default(); 

//第一个参数 是xhprof_disable()的返回值
//第二个参数 是自定义命名空间字符串 
//返回运行id,用这个id查看相关运行结果
$run_id = $objXhprofRun->save_run($data, "xhprof");
 
//查看运行结果的url
//run的值来源于save_run的返回值
//source值来源于save_run的第二个参数                                 
//www.abc.com 需要用自己的域名替换
echo "http://www.abc.com/xhprof_html/index.php?run=".$run_id."&source=xhprof";

      在浏览器中打开输出的url,及可看到相关的性能信息。

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn