public function inprint(){
xhprof_enable();//开始
}
public function outprint(){
$xhprof_data = xhprof_disable();//结束,然后写入文件,注意目录
define("XHPROF_ROOT", substr(dirname(__FILE__), 0, strpos(__FILE__, "mobileapi")));
include_once XHPROF_ROOT . "/xhprof/xhprof_lib/utils/xhprof_lib.php";
include_once XHPROF_ROOT . "/xhprof/xhprof_lib/utils/xhprof_runs.php";
// save raw data for this profiler run using default
// implementation of iXHProfRuns.
$xhprof_runs = new XHProfRuns_Default();
// save the run under a namespace "xhprof_foo"
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");
$runtag = "http://192.168.3.119/xhprof/xhprof_html/?run=$run_id&source=xhprof_foo";
return $runtag;
// echo "<a href='http://192.168.3.119/xhprof/xhprof_html/?run=$run_id&source=xhprof_foo'>分析</a>";
//这里的www.du52.com换成你自己的域名就好了,本地就localhost
}
<?php
/*
* Set the absolute paths on your system
*/
define('ERROR_FILE', 'd:/wamp/logs/xhprof_dot_errfile.log');
define('TMP_DIRECTORY', 'd:/wamp/tmp/xhprof/tmp/');
//define('DOT_BINARY', 'c:/Program Files/Grafika/Graphviz/bin/dot.exe');
define('DOT_BINARY', 'd:/Program Files (x86)/Graphviz2.38/bin/dot.exe');
?>
报错:
1、点击[View Full Callgraph]查看图片的时候报错:failed to execute cmd:" dot -Tpng". stderr:`sh: dot:command not found`。
原因:原因:未安装图形化工具
解决:
//红帽系列
yum install graphviz
//Ununtu
apt-get install graphviz
//OS X
brew install graphviz
Transaction Check Error:
file /usr/bin/gxl2dot conflicts between attempted installs of graphviz-2.22.0-4.el5.rf.x86_64 and graphviz-2.12-10.el5.i386
yum install graphviz-2.22.0-4.el5.rf.x86_64
3. 嵌入代码
<?php
// cpu:XHPROF_FLAGS_CPU 内存:XHPROF_FLAGS_MEMORY
// 如果两个一起:XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
//要测试的php代码
$data = xhprof_disable(); //返回运行数据
// xhprof_lib在下载的包里存在这个目录,记得将目录包含到运行的php代码中
include_once "xhprof_lib/utils/xhprof_lib.php";
include_once "xhprof_lib/utils/xhprof_runs.php";
$objXhprofRun = new XHProfRuns_Default();
// 第一个参数j是xhprof_disable()函数返回的运行信息
// 第二个参数是自定义的命名空间字符串(任意字符串),
// 返回运行ID,用这个ID查看相关的运行结果
$run_id = $objXhprofRun->save_run($data, "xhprof");
var_dump($run_id);
4. 页面展示
将xhprof_lib&&xhprof_html相关目录copy到可以访问到的地址
访问 xxx/xhprof_html/index.php?run=$run_id&source=xhprof 就可经看到你的php代码运行的相关情况
下面是一些参数说明
Inclusive Time 包括子函数所有执行时间。
Exclusive Time/Self Time 函数执行本身花费的时间,不包括子树执行时间。
Wall Time 花去了的时间或挂钟时间。
CPU Time 用户耗的时间+内核耗的时间
Inclusive CPU 包括子函数一起所占用的CPU
Exclusive CPU 函数自身所占用的CPU