1. public function inprint(){
    2. xhprof_enable();//开始
    3. }
    4. public function outprint(){
    5. $xhprof_data = xhprof_disable();//结束,然后写入文件,注意目录
    6. define("XHPROF_ROOT", substr(dirname(__FILE__), 0, strpos(__FILE__, "mobileapi")));
    7. include_once XHPROF_ROOT . "/xhprof/xhprof_lib/utils/xhprof_lib.php";
    8. include_once XHPROF_ROOT . "/xhprof/xhprof_lib/utils/xhprof_runs.php";
    9. // save raw data for this profiler run using default
    10. // implementation of iXHProfRuns.
    11. $xhprof_runs = new XHProfRuns_Default();
    12. // save the run under a namespace "xhprof_foo"
    13. $run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");
    14. $runtag = "http://192.168.3.119/xhprof/xhprof_html/?run=$run_id&source=xhprof_foo";
    15. return $runtag;
    16. // echo "<a href='http://192.168.3.119/xhprof/xhprof_html/?run=$run_id&source=xhprof_foo'>分析</a>";
    17. //这里的www.du52.com换成你自己的域名就好了,本地就localhost
    18. }
    19. <?php
    20. /*
    21. * Set the absolute paths on your system
    22. */
    23. define('ERROR_FILE', 'd:/wamp/logs/xhprof_dot_errfile.log');
    24. define('TMP_DIRECTORY', 'd:/wamp/tmp/xhprof/tmp/');
    25. //define('DOT_BINARY', 'c:/Program Files/Grafika/Graphviz/bin/dot.exe');
    26. define('DOT_BINARY', 'd:/Program Files (x86)/Graphviz2.38/bin/dot.exe');
    27. ?>
    28. 报错:
    29. 1、点击[View Full Callgraph]查看图片的时候报错:failed to execute cmd" dot -Tpng". stderr`sh: dot:command not found`
    30. 原因:原因:未安装图形化工具
    31. 解决:
    32. //红帽系列
    33. yum install graphviz
    34. //Ununtu
    35. apt-get install graphviz
    36. //OS X
    37. brew install graphviz
    38. Transaction Check Error:
    39. 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
    40. yum install graphviz-2.22.0-4.el5.rf.x86_64
    41. 3. 嵌入代码
    42. <?php
    43. // cpu:XHPROF_FLAGS_CPU 内存:XHPROF_FLAGS_MEMORY
    44. // 如果两个一起:XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY
    45. xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
    46. //要测试的php代码
    47. $data = xhprof_disable(); //返回运行数据
    48. // xhprof_lib在下载的包里存在这个目录,记得将目录包含到运行的php代码中
    49. include_once "xhprof_lib/utils/xhprof_lib.php";
    50. include_once "xhprof_lib/utils/xhprof_runs.php";
    51. $objXhprofRun = new XHProfRuns_Default();
    52. // 第一个参数j是xhprof_disable()函数返回的运行信息
    53. // 第二个参数是自定义的命名空间字符串(任意字符串),
    54. // 返回运行ID,用这个ID查看相关的运行结果
    55. $run_id = $objXhprofRun->save_run($data, "xhprof");
    56. var_dump($run_id);
    57. 4. 页面展示
    58. xhprof_lib&&xhprof_html相关目录copy到可以访问到的地址
    59. 访问 xxx/xhprof_html/index.php?run=$run_id&source=xhprof 就可经看到你的php代码运行的相关情况
    60. 下面是一些参数说明
    61. Inclusive Time 包括子函数所有执行时间。
    62. Exclusive Time/Self Time 函数执行本身花费的时间,不包括子树执行时间。
    63. Wall Time 花去了的时间或挂钟时间。
    64. CPU Time 用户耗的时间+内核耗的时间
    65. Inclusive CPU 包括子函数一起所占用的CPU
    66. Exclusive CPU 函数自身所占用的CPU