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 配置:
7 为xhgui配置一个可访问的虚拟机
另一种情况是 没有mongodb 或者是公司服务器没有权限什么的 可以像下面这样使用:
一: 系统需求
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就能够看到一个统计列表了。
}