安装 XHGui对 PHP 应用进行性能诊断

本文介绍了如何通过XHGui这一强大的PHP性能诊断工具进行程序性能分析。首先,文章列出了系统需求,包括安装mongodb、mongo、xhprof扩展以及开启相关PHP扩展。接着,详细阐述了XHGui的安装步骤,包括下载源码、配置权限、安装过程、开启rewrite以及在Apache和Nginx环境下设置自动预加载header.php。此外,还提供了一种在没有mongodb环境下的使用方法,通过xhprof_enable和xhprof_end进行手动性能统计。最后,指导如何配置一个可访问的虚拟机来查看XHGui的性能报告。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Profiling是一项用来观察程序性能的技术,非常适用于发现程序的瓶颈或者紧张的资源。Profiling能够深入程序的内部,展现request处理过程中每一部分代码的性能;同时,也可以确定有问题的请求(request);对于有问题的请求,我们还可以确定性能问题发生在请求内部的位置。对于PHP,我们有多种Profiling工具,本文主要集中在——XHGui,一款非常优秀的工具。XHGui构建在XHProf之上(XHProf由Facebook发布),但是对于剖析结果增加了更好的存储,同时增加了更加良好的信息获取接口。从这方面来说,XHGui更像是一个全新的工具。

一: 系统需求

1 安装mongodb https://www.mongodb.org/downloads
2 下载mongo扩展  http://pecl.php.net/package/mongo
3 下载xhprof 扩展  http://pecl.php.net/package/xhprof 
4 开启mcrypt dom 扩展
5 composer支持

二: 安装XHGui

1 下载 https://github.com/perftools/xhgui  (git clone https://github.com/perftools/xhgui.git)
2 将cache目录改为777权限 chmod -R 0777 cache
3 开启mongodb 如果mongodb需要密码登陆  则需要更改config/config.php
4 安装 cd path/to/xhgui; php install.php 或者 composer update --prefer-dist --no-dev
5 开启rewrite 
6 将header.php文件引入需要记录的虚拟机配置中
apache 配置:
<VirtualHost *:80>
    php_admin_value auto_prepend_file "/Users/markstory/Sites/xhgui/external/header.php"
    DocumentRoot "/Users/markstory/Sites/awesome-thing/app/webroot/"
    ServerName site.localhost
    <Directory /var/www/xhgui/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
nginx:
server {
    listen   80;
    server_name example.com;

    # root directive should be global
    root   /var/www/example.com/public/xhgui/webroot/;
    index  index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass  unix:/var/run/php5-fpm.api.sock;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param PHP_VALUE "auto_prepend_file=/Users/markstory/Sites/xhgui/external/header.php";
        fastcgi_index index.php;
        include fastcgi_params;
    }
}
或者:
<?php
require '/path/to/xhgui/external/header.php';
// Rest of script.
或者:
php -d auto_prepend_file=/path/to/xhgui/external/header.php do_work.php

7 为xhgui配置一个可访问的虚拟机
server
{
    listen 80;
    server_name    xhgui.test.com;

    root   /home/nginx/www/xhgui/webroot/;
    index index.html index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    #try_files $uri $uri/ $uri/index.php $uri/index.php?$query_string;

    location ~ \.php$ {
        fastcgi_pass    127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}
echo "127.0.0.1 xhgui.test.com" >> /etc/hosts
pkill -HUP nginx

另一种情况是 没有mongodb 或者是公司服务器没有权限什么的 可以像下面这样使用:

//统计开始
xhprof_enable(XHPROF_FLAGS_CPU+XHPROF_FLAGS_MEMORY);

/**  code  **/

//统计结束
xhprof_end();

function xhprof_end()
{
    $data = xhprof_disable();
    
    $xhprof_root = "/usr/local/php/xhprof";
    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($data, "domain");//第二个参数在接下来的地方作为命名空间一样的概念来使用
    setcookie("xhprof_url", "$run_id", $_SERVER['REQUEST_TIME'] + 3600, "/", "btime.com", false, true);//不影响正常的输出结果 将本次的run_id 写到cookie
    //访问domain/index.php?run=$run_id&source=domain就能够看到一个统计列表了。
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值