Heim  >  Artikel  >  php教程  >  facebook工具xhprof的安装与使用-分析php执行性能

facebook工具xhprof的安装与使用-分析php执行性能

WBOY
WBOYOriginal
2016-06-13 11:31:53709Durchsuche

下载源码包的网址
 
http://pecl.php.net/package/xhprof



上面说了,每个版本适用的php版本。


规划(预先搞清楚思路)



一、这是一个php扩展的形式。我们安装gd2,curl都是php的扩展形式。只不过有的时候编译的时候就安装进去了。
像操作mysql数据库,也是一个mysql.so这样的扩展,安装了扩展,就能调用mysql_query()这些函数。
要操作oracle数据库,也有对应的oracle扩展加到php引擎中去。



现在要把xhprof扩展加到php中去。


很久没使用phpize安装扩展了。我自己忘得差不多了。于是重新去自己的博客找到以前写的文章复习一下。

http://www.cnblogs.com/wangtao_20/archive/2011/03/16/1986508.html




ps:我也在思考,这个东西怎么这么容易忘记。我只知道他的作用。但是完全不记得他的操作步骤。要注意的细节。

看我得想办法以通俗的方式来理解记住它。





二、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=
;储存 XHProf 运行数据的默认目录
xhprof.output_dir=/tmp/xhprof



三、有了这个扩展后,就能在自己的php代码中调用这个扩展内置的函数来做性能监控了,像下面这样子


xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

................这里是要被监控的代码块


$data = xhprof_disable();

include_once "xhprof_lib/utils/xhprof_lib.php";  
include_once "xhprof_lib/utils/xhprof_runs.php";  
$objXhprofRun = new XHProfRuns_Default();//数据会保存在php.ini中xhprof.output_dir设置的目录去中
$run_id = $objXhprofRun->save_run($data, "test");


=====================================================

几个扩展函数如下




步骤实施


1、先找到我服务器上php的安装目录,phpize一般都是在安装目录中,如下:

/usr/local/php/bin/phpize

2、找出php-config在哪个目录(下面会用到),我的服务器在:

/usr/local/php/bin/php-config

目的:在下面进行编译的时候,会用到这个文件

./configure --with-php-config=/usr/local/php/bin/php-config



3、找到我服务器上php扩展在哪个目录,不确定的话,我觉得去php.ini中也能看到,如下


extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/" 这个路径感觉有点长。不用去改了php.ini中的设置,目前我觉得没必要(商业与时间成本,这部分还不是制约因素)。按原来的继续放扩展。




现在知道扩展目录为:/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/

也就是说,我要把xhprof的源码包解压到这个目录下去(解压后会生成一个新的文件夹)。


那我就要去这个目录下运行phpize(这样方便在这个目录下面生成configure文件),

phpize的特点:在a目录下运行phpize,就会在a目录下生成configure。











得到上面路径,shell命令实践



cd  /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/

解压下载到的xhprof压缩包(我不是通过wget下载的,我是把这个压缩直接通过ftp上传到no-debug-non-zts-20060613目录中去)。



tar zxf xhprof-0.9.3.tgz               #解压后,里面有个extension文件夹,进入里面去(目的是进入里面去运行phpize),解压后的目录结构如下

 




cd xhprof-0.9.3/extension/            #切换到这个扩展的源码目录去

在这个目录下面运行phpize,就会在extension目录下生成一个configure文件(这是phpize的机制)


/usr/local/php/bin/phpize
 
去看一下扩展目录,会发现在extension目录生成了一个configure文件。运行它


======================================


./configure --with-php-config=/usr/local/php/bin/php-config #用到了前面找到的php-config文件。

make && make install

make test
======================================




运行成功后,会提示生成的xhprof.so文件在哪个位置,提示信息:

Libraries have been installed in:
   /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/xhprof-0.9.3/extension/modules


这个目录下已经存在一个文件:
/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/xhprof.so

 

 



刚才生成了xhprof.so这个模块文件,现在要在php.ini中加载刚才生成xhprof.so模块才能生效:

[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=
xhprof.output_dir=/tmp/xhprof






平滑重新加载php.ini文件:/usr/local/php/sbin/php-fpm reload
提示:

Reload service php-fpm  done

说明成功。

现在去phpinfo中看xhprof扩展是否加载成功了






安装作图工具(选填,可以后续再安装)

yum install -y graphviz





================================================

工具的使用实践

================================================

 

index.php中的代码:

 

error_reporting(-1);


xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
 
$begin_time = microtime_float();




///////////////统计执行速度代码
$end_time = microtime_float();

$exec_time  = $end_time-$begin_time;

//@save_stat($exec_time);

for($i=0;$i

for($j=0;$j
}

}



$data = xhprof_disable();

include_once "xhprof_lib/utils/xhprof_lib.php";//
include_once "xhprof_lib/utils/xhprof_runs.php";  
$objXhprofRun = new XHProfRuns_Default();//数据会保存在php.ini中xhprof.output_dir设置的目录去中
$run_id = $objXhprofRun->save_run($data, "test");
//第二个参数是定义文件名称。名称如果为"xhprof",则在xhprof.output_dir设置的目录生成的文件:522ab85f40229.xhprof.xhprof。
//格式为:"id标识.名称.xhprof",id标识就是$run_id得到的结果。





var_dump($run_id);

echo "http://www.xxxx.com/xhprof_html/index.php?run={$run_id}&source=test\n";//source的值必须是save_run中指定的名称。这个其实就是根据编号和名称才能定位到对应的文件"522ab85f40229.xhprof.xhprof"

//将这个地址输出来,是为了直接可以去查看分析结果。




function save_stat($time)
{
    static $call_count=1;
    $call_limit = 10;
    if(!$time) return ;
        $date = date("Y-m-d");//暂时按照天来生成文件。方便查阅
    $exec_stat_file = './exec_stat'.DIRECTORY_SEPARATOR."exec_stat_file-".$date.".txt";
    $fp = fopen($exec_stat_file,'ab');
    if(flock($fp,LOCK_EX))
    {
        $s = 'access:'.date("Y-m-d H:i:s").',execute time:'.$time.'s,request_url:http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."||".PHP_EOL;
        fwrite($fp,$s);
        flock($fp,LOCK_UN);
    }else{
        usleep(1000);
        if($call_count        {
            $call_count++;
            save_stat($time);
        }
    }

    @fcolse($fp);
    //var_dump($fp);

}
function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}
?>

 

加粗的黑体,就是分析用到的代码。

其中用到了源码包中的xhprof_lib中的代码,代码中"XHProfRuns_Default"这个类就是里面的。

 

 

生成了分析结果后,现在可以去使用源码包中提供的web版的工具查看了。

 

xhprof_html这个文件夹随便放到哪里,只要放到能够通过web访问的目录下即可,不过这个里面会用到xhprof_lib中的类,所以还是一起复制过去,跟源码包中保持一样的结构才好。xhprof_html与xhprof_lib要保持平行

 

我这里,这个工具的访问方式是:http://www.xxxx.com/xhprof_html/index.php?run=xxx&source=xxx\n

source的值必须是save_run中指定的名称。这个其实就是根据编号和名称才能定位到对应的文件"522ab85f40229.xhprof.xhprof"

访问看到的结果如下

 

 

 

 

图中,红色的表示最耗费性能的,黄色的其次。

 

 

只了解这么多了。具体的分析结果怎么看,在文件夹xhprof_html中有个docs目录,里面是说明文档。说了一些专用术语的定义

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn