您的位置:首页 > 编程语言 > PHP开发

php生产环境XHProf 性能分析工具

2013-11-21 11:35 651 查看
1. PHP 编译安装XHProf 模块,这个很简单网上大把资料,我这里就不说了。

2. 在你代码入口增加以下代码

if (mt_rand(1, 100) == 1) {
if(function_exists('xhprof_enable'))
{
xhprof_enable();
$code='$xhprof_data = xhprof_disable();
$xhprof_root = "/data/web/data/xh/";
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://{$_SERVER["HTTP_HOST"]}/data/xh/xhprof_html/index.php?run=$run_id&source=hx\" target=\"_blank\">统计</a>";
';
register_shutdown_function(create_function('', $code));
}
}


3. 下载生产环境的生产的日志到本地,具体的路径为php.ini里面配置的(为了安全以及不影响线上环境,所以建议下载到本地来分析日志);

4. 编写代码来遍历日志里面的所有文件,找出最慢的日志,然后来分析。

function tree($directory,$time)
{
$mydir = dir($directory);
echo "<ul>\n";
$filenum=$wtnum=0;
while($file = $mydir->read()){
if((is_dir("$directory/$file")) AND ($file!=".") AND ($file!=".."))
{
tree("$directory/$file");
}else if(is_file("$directory/$file")){
$filenum++;
$_file="$directory/$file";
$data=file($_file);
$data=unserialize($data[0]);// debug($data);
if($data['main()']['wt']>$time*1000)
{
$wtnum++;
list($run_id,$ext)=explode('.', $file);
$dir=$directory;
$url="http://{$_SERVER["HTTP_HOST"]}/data/xh/xhprof_html/index.php?run=$run_id&source=hx&dir=$dir";
echo "<li>$file";
echo "<a href=\"{$url}\" target=\"_blank\">统计</a>";
echo '</li>';
}
}
}
echo "</ul>\n";
echo '文件总数:'.$filenum;
echo '总执行时间大于'.$time.' ms 总数:'.$wtnum;
$mydir->close();
}
//开始运行
$dir="D:/data/xh/logs/pay";
$time=2000;
tree($dir,$time);
运行后的效果为:


5. 修改 xh/xhprof_html/index.php 文件 ,方便我们传入参数可以指定日志目录。

http://{$_SERVER["HTTP_HOST"]}/data/xh/xhprof_html/index.php?run=$run_id&source=hx&dir=$dir


xh/xhprof_html/index.php 加入如下代码

$GLOBALS['XHPROF_LIB_ROOT'] = dirname(__FILE__) . '/../xhprof_lib';

session_start();
$dir=@$_GET['dir'];
if($dir){
$_SESSION['xhdir']=$dir;
}else{
$dir=$_SESSION['xhdir'];
}


修改

$xhprof_runs_impl = new XHProfRuns_Default();




$xhprof_runs_impl = new XHProfRuns_Default($dir);


大功告成,你可以分析你的代码了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: