xdebug部署线上服务,消耗太厉害,所以用xhprof 安装
安装前说明 我php装在/usr/local/php下,虚机建在/usr/local/www/uusnn.com.cn下
1.下载安装
<pre> 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 /usr/local/www/uusnn.com.cn
cd extension
/usr/local/php/bin/phpize
./configure
make
make install</pre>
2.php.ini配置
<pre>[xhprof]
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/xhprof.so
;
; directory used by default implementation of the iXHProfRuns
; interface (namely, the XHProfRuns_Default class) for storing
; XHProf runs.
;
xhprof.output_dir=/var/log/xhprof/</pre>
3.测试代码,用自带的这个简单的php函数
如果想知道cpu和内存使用情况可以这样写就行了
<pre><span style="color: #ff0000;">xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
</span></pre>
<pre>
< ?php
// start profiling
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
// run program
function bar($x) {
if ($x > 0) {
bar($x - 1);
}
}
function foo() {
for ($idx = 0; $idx < 2; $idx++) {
bar($idx);
$x = strlen("abc");
}
}
foo();
// stop profiler
$xhprof_data = xhprof_disable();
//
// Saving the XHProf run
// using the default implementation of iXHProfRuns.
//
include_once "xhprof_lib/utils/xhprof_lib.php";
include_once "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_html\index.php?run=$run_id&source=xhprof_foo\n".
"---------------\n";
</pre>
4.写个文件看看他
现在执行上面的文件,可保存为xhprof.php
http://www.uusnn.com.cn/xhprof/xhprof.php
4d1b0c6fe2456.xhprof_foo 为生成的文件,是php的序列化数字
<pre>< ?php
$html = file_get_contents('/var/log/xhprof/4d1b0c6fe2456.xhprof_foo');
print_r( unserialize($html));</pre>
< 原创,转载请说明出处www.uusnn.com.cn>
<!--more-->
4.打印结果
Array
(
[foo==>bar] => Array
(
[ct] => 2
[wt] => 81
)
[foo==>strlen] => Array
(
[ct] => 2
[wt] => 1
)
[bar==>bar@1] => Array
(
[ct] => 1
[wt] => 1
)
[main()==>foo] => Array
(
[ct] => 1
[wt] => 201
)
[main()==>xhprof_disable] => Array
(
[ct] => 1
[wt] => 0
)
[main()] => Array
(
[ct] => 1
[wt] => 219
)
)
安装前说明 我php装在/usr/local/php下,虚机建在/usr/local/www/uusnn.com.cn下
1.下载安装
<pre> 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 /usr/local/www/uusnn.com.cn
cd extension
/usr/local/php/bin/phpize
./configure
make
make install</pre>
2.php.ini配置
<pre>[xhprof]
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/xhprof.so
;
; directory used by default implementation of the iXHProfRuns
; interface (namely, the XHProfRuns_Default class) for storing
; XHProf runs.
;
xhprof.output_dir=/var/log/xhprof/</pre>
3.测试代码,用自带的这个简单的php函数
如果想知道cpu和内存使用情况可以这样写就行了
<pre><span style="color: #ff0000;">xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
</span></pre>
<pre>
< ?php
// start profiling
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
// run program
function bar($x) {
if ($x > 0) {
bar($x - 1);
}
}
function foo() {
for ($idx = 0; $idx < 2; $idx++) {
bar($idx);
$x = strlen("abc");
}
}
foo();
// stop profiler
$xhprof_data = xhprof_disable();
//
// Saving the XHProf run
// using the default implementation of iXHProfRuns.
//
include_once "xhprof_lib/utils/xhprof_lib.php";
include_once "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_html\index.php?run=$run_id&source=xhprof_foo\n".
"---------------\n";
</pre>
4.写个文件看看他
现在执行上面的文件,可保存为xhprof.php
http://www.uusnn.com.cn/xhprof/xhprof.php
4d1b0c6fe2456.xhprof_foo 为生成的文件,是php的序列化数字
<pre>< ?php
$html = file_get_contents('/var/log/xhprof/4d1b0c6fe2456.xhprof_foo');
print_r( unserialize($html));</pre>
< 原创,转载请说明出处www.uusnn.com.cn>
<!--more-->
4.打印结果
Array
(
[foo==>bar] => Array
(
[ct] => 2
[wt] => 81
)
[foo==>strlen] => Array
(
[ct] => 2
[wt] => 1
)
[bar==>bar@1] => Array
(
[ct] => 1
[wt] => 1
)
[main()==>foo] => Array
(
[ct] => 1
[wt] => 201
)
[main()==>xhprof_disable] => Array
(
[ct] => 1
[wt] => 0
)
[main()] => Array
(
[ct] => 1
[wt] => 219
)
)