Xhprof 的配置和使用方法

2016-03-17  本文已影响1942人  Justin的实验室

Xhprof 的配置和使用方法

Xhprof是一个分层PHP性能分析工具。

环境

依赖

下载和安装

wget http://pecl.php.net/get/xhprof-0.9.4.tgz
tar zxf xhprof-0.9.4.tgz
cd xhprof-0.9.4/extension/
sudo phpize
./configure
sudo make
sudo make install
cp xhprof.so [path_to_php_extension]/xhprof.so

配置php.ini

[xhprof]
extension=xhprof.so
xhprof.output_dir=/var/www/xhprof/xhprof_runs/

搭建Xhprof运行环境

cp -a [path_to_xhprof]/xhprof_html /var/www/xhprof/
cp -a [path_to_xhprof]/xhprof_lib /var/www/xhprof/

注入PHP代码

xhprof_enable();
/**
...
要检查的php代码
...
**/
$xhprof_data = xhprof_disable();

// 引入xhprof_lib
include_once "/var/www/xhprof/xhprof_lib/utils/xhprof_lib.php";
include_once "/var/www/xhprof/xhprof_lib/utils/xhprof_runs.php";

$instance = new XHProfRuns_Default();
$run_id = $instance->save_run($xhprof_data, "xhprof");
var_dump($run_id);

查看检查结果

server {
    listen 80;
    server_name xhprof.test.com;
    root /var/www/xhprof/xhprof_html;
    index index.html index.php;
    location ~ \.php$ {
        fastcgi_pass php_pool;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
}

更优雅的方式

xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);
register_shutdown_function(function() {
    $xhprof_data = xhprof_disable();
    if (function_exists('fastcgi_finish_request')){
        fastcgi_finish_request();
    }
    include_once "/var/www/xhprof/xhprof_lib/utils/xhprof_lib.php";
    include_once "/var/www/xhprof/xhprof_lib/utils/xhprof_runs.php";
    $xhprof_runs = new XHProfRuns_Default();
    $run_id = $xhprof_runs->save_run($xhprof_data, 'xhprof');
    echo 'save xhprof data';
});
auto_prepend_file = /var/www/xhprof/xhprof.inc.php
上一篇下一篇

猜你喜欢

热点阅读