不灭的焱

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2021-01-15 17:46:51  修改时间:2024-04-11 12:09:00  分类:PHP基础  编辑

常用的PHP环境:PHP5.6 + Nginx,但是Windows系统下,xhprof没有对应的PHP5.6的版本,所以本地开发就降级到了PHP5.5,这个对目前的工作差别不大!

1、下载XHProf

下载Windows版本的 xhprof for php5.5非线程安全版本(nts)64位(x64)点击下 载

其他的版本可查看:http://pecl.php.net/package/xhprof

下载 xhprof_html(提取码:i7w1 )

2、安装XHProf

把压缩包里的 php_xhprof.dll 拷贝到PHP的ext目录下,然后在php.ini配置里面加入配置(不要忘记创建对应的文件夹)

[xhprof]
xhprof.output_dir="D:/php/phpstudy/Extensions/php/xhprof/log"

重启Nginx,看看phpinfo()里面有没有xhprof相关信息!

3、使用XHProf

将 xhprof_html.zip 解压到你想测试的网站根目录,如我放在了网站目录的 /public/xhprof/windows 下。

测试文件:

<?php
function bar($x) {
	if ($x > 0) {
		bar($x - 1);
	}
}

function foo() {
	for ($idx = 0; $idx < 5; $idx++) {
		bar($idx);
		$x = strlen("abc");
	}
}

// 启动xhprof
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

// 调用foo函数,也是我们要分析的函数
foo();

// 停止xhprof

$xhprof_data = xhprof_disable();

// 取得统计数据
//print_r($xhprof_data);

$os = 'windows';

$XHPROF_ROOT = dirname(__FILE__) . '/public/xhprof/' . $os;
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";

// 保存统计数据,生成统计ID和source名称
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo"); // source名称是xhprof_foo

// 查看统计信息
echo "<div style='margin: 50px auto; padding :10px; width: 92px; font-size: 16px; background: #ff0000;'><a style='color:#ffff00;' href='/public/xhprof/" . $os . "/xhprof_html/index.php?run=" . $run_id . "&source=xhprof_foo' target='_blank'>XHProf view</a></div>";

 

当点击链接“[View Full Callgraph]”时,会报错:

failed to execute cmd: " dot -Tpng"

所以还需要下载图形工具 Graphviz

 

4、下载Graphviz

注意:只能下载 graphviz-2.38 版本,太高版本的不起作用,点击下载

 

5、安装Graphviz

把Graphviz安装到某个目录,如 D:\software\Graphviz

 

6、配置Graphviz

找到上述提到网站目录 /public/xhprof/windows/ 下的 config.php 文件,调整如下:

<?php
/**
 * Set the absolute paths on your system
 */
define('ERROR_FILE', 'D:/php/phpstudy/Extensions/php/xhprof/log/xhprof_dot_errfile.log');
define('TMP_DIRECTORY', 'D:/php/phpstudy/Extensions/php/xhprof/tmp');
define('DOT_BINARY', 'D:/software/Graphviz/bin/dot.exe');

 

重新点击链接 [View Full Callgraph],一张期待已久的效果图出来啦:

 

 


使用xhprof会在nginx下报502 Bad Gateway错误

xhprof_enable()
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

修改为:

xhprof_enable(XHPROF_FLAGS_NO_BUILTINS | XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY);