原文链接:http://blog.snsgou.com/post-816.html
1、下载XHProf
到这里 http://dev.freshsite.pl/php-extensions/xhprof.html 下载Windows版本的XHProf,我这里选择下载
XHProf 0.10.3 for PHP 5.3 vc9 和 xhprof_html
2、安装XHProf
把压缩包里的 xhprof_0.10.3_php53_vc9.dll 拷贝到PHP的ext目录下,然后在php.ini配置里面加入配置(不要忘记创建对应的文件夹)
[xhprof]extension=xhprof_0.10.3_php53_vc9.dll; directory used by default implementation of the iXHProfRuns; interface (namely, the XHProfRuns_Default class) for storing; XHProf runs.xhprof.output_dir="d:/PHP/xhprof/log"
3、使用XHProf
将 xhprof_html.zip 解压到你想测试的网站根目录,如我放在了网站目录的 /public/xhprof/windows 下。
测试文件:
<?phpfunction bar($x){ if ($x > 0) { bar($x - 1); }}function foo(){ for ($idx = 0; $idx < 5; $idx++) { bar($idx); $x = strlen("abc"); }}// 启动xhprofxhprof_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
到这里 http://www.graphviz.org/Download_windows.php 下载Windows版本的Graphviz,我这里选择下载
5、安装Graphviz
解压后,把Graphviz拷贝到某个目录,如 d:/PHP/xhprof/graphviz-2.38/
6、配置Graphviz
找到上述提到网站目录 /public/xhprof/windows/ 下的 config.php 文件,调整如下:
<?php/** * Set the absolute paths on your system */define('ERROR_FILE', 'd:/PHP/xhprof/log/xhprof_dot_errfile.log');define('TMP_DIRECTORY', 'd:/PHP/xhprof/tmp');define('DOT_BINARY', 'd:/PHP/xhprof/graphviz-2.38/release/bin/dot.exe');
重新点击链接 [View Full Callgraph],一张期待已久的效果图出来啦: