php性能监测模块XHProf

weialemon 2015-09-15

一,什么是XHProf

XHProf是一个分层PHP性能分析工具。它报告函数级别的请求次数和各种指标,包括阻塞时间,CPU时间和内存使用情况。一个函数的开销,可细分成调用者和被调用者的开销,XHProf数据收集阶段,它记录调用次数的追踪和包容性的指标弧在动态callgraph的一个程序。它独有的数据计算的报告/后处理阶段。在数据收集时,XHProfd通过检测循环来处理递归的函数调用,并通过给递归调用中每个深度的调用一个有用的命名来避开死循环。XHProf分析报告有助于理解被执行的代码的结构,它有一个简单的HTML的用户界面( PHP写成的)。基于浏览器的性能分析用户界面能更容易查看,或是与同行们分享成果。也能绘制调用关系图。

二,安装XHProf扩展模块

1,安装

  1. <span class="pln">wget http</span><span class="pun">:</span><span class="com">//pecl.php.net/get/xhprof-0.9.2.tgz </span>
  2. <span class="pln">tar zxvf xhprof</span><span class="pun">-</span><span class="lit">0.9</span><span class="pun">.</span><span class="lit">2.tgz</span>
  3. <span class="pln">cp </span><span class="pun">./</span><span class="pln">xhprof</span><span class="pun">-</span><span class="lit">0.9</span><span class="pun">.</span><span class="lit">2.tgz</span><span class="pun">./</span><span class="pln">www </span><span class="com">//xhprof自身带有一个web版的分析页面,放到我的web服务器下面 </span>
  4. <span class="pln">cd xhprof</span><span class="pun">-</span><span class="lit">0.9</span><span class="pun">.</span><span class="lit">2</span><span class="pun">/</span><span class="pln">extension </span>
  5. <span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">php</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">phpize </span>
  6. <span class="pun">./</span><span class="pln">configure </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">xhprof </span><span class="pun">--</span><span class="kwd">with</span><span class="pun">-</span><span class="pln">php</span><span class="pun">-</span><span class="pln">config</span><span class="pun">=</span><span class="str">/usr/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">php</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">php</span><span class="pun">-</span><span class="pln">config </span>
  7. <span class="pln">make </span><span class="pun">&&</span><span class="pln"> make install </span>

2,配置

  1. <span class="pun">[</span><span class="pln">xhprof</span><span class="pun">]</span>
  2. <span class="pln">extension</span><span class="pun">=</span><span class="pln">xhprof</span><span class="pun">.</span><span class="pln">so </span>
  3. <span class="pln">xhprof</span><span class="pun">.</span><span class="pln">output_dir</span><span class="pun">=</span><span class="str">/home/</span><span class="pln">yicheng</span><span class="pun">/</span><span class="pln">xhprof </span><span class="com">//如果不加存放目录的话,默认是放在/tmp下面 </span>

三,XHProf测试

前面我们说过了,XHProf自身带有一个web版的测试工具,里面还有一个小例子。看一下这个例子,我做了一点修改和注释

  1. <span class="pun"><?</span><span class="pln">php </span>
  2. <span class="kwd">function</span><span class="pln"> bar</span><span class="pun">(</span><span class="pln">$x</span><span class="pun">)</span><span class="pun">{</span>
  3. <span class="kwd">if</span><span class="pun">(</span><span class="pln">$x </span><span class="pun">></span><span class="lit">0</span><span class="pun">)</span><span class="pun">{</span>
  4. <span class="pln">bar</span><span class="pun">(</span><span class="pln">$x </span><span class="pun">-</span><span class="lit">1</span><span class="pun">);</span>
  5. <span class="pun">}</span>
  6. <span class="pun">}</span>
  7. <span class="kwd">function</span><span class="pln"> foo</span><span class="pun">()</span><span class="pun">{</span>
  8. <span class="kwd">for</span><span class="pun">(</span><span class="pln">$idx </span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><span class="pln"> $idx </span><span class="pun"><</span><span class="lit">5</span><span class="pun">;</span><span class="pln"> $idx</span><span class="pun">++)</span><span class="pun">{</span>
  9. <span class="pln">bar</span><span class="pun">(</span><span class="pln">$idx</span><span class="pun">);</span>
  10. <span class="pln">$x </span><span class="pun">=</span><span class="pln"> strlen</span><span class="pun">(</span><span class="str">"abc"</span><span class="pun">);</span>
  11. <span class="pun">}</span>
  12. <span class="pun">}</span>
  13. <span class="com">//启动xhprof </span>
  14. <span class="pln">xhprof_enable</span><span class="pun">(</span><span class="pln">XHPROF_FLAGS_CPU </span><span class="pun">+</span><span class="pln"> XHPROF_FLAGS_MEMORY</span><span class="pun">);</span>
  15. <span class="com">//调用foo函数,也是我们要分析的函数 </span>
  16. <span class="pln">foo</span><span class="pun">();</span>
  17. <span class="com">//停止xhprof </span>
  18. <span class="pln">$xhprof_data </span><span class="pun">=</span><span class="pln"> xhprof_disable</span><span class="pun">();</span>
  19. <span class="com">//取得统计数据 </span>
  20. <span class="pln">print_r</span><span class="pun">(</span><span class="pln">$xhprof_data</span><span class="pun">);</span>
  21. <span class="pln">$XHPROF_ROOT </span><span class="pun">=</span><span class="pln"> realpath</span><span class="pun">(</span><span class="pln">dirname</span><span class="pun">(</span><span class="pln">__FILE__</span><span class="pun">)</span><span class="pun">.</span><span class="str">'/..'</span><span class="pun">);</span>
  22. <span class="pln">include_once $XHPROF_ROOT </span><span class="pun">.</span><span class="str">"/xhprof_lib/utils/xhprof_lib.php"</span><span class="pun">;</span>
  23. <span class="pln">include_once $XHPROF_ROOT </span><span class="pun">.</span><span class="str">"/xhprof_lib/utils/xhprof_runs.php"</span><span class="pun">;</span>
  24. <span class="com">//保存统计数据,生成统计ID和source名称 </span>
  25. <span class="pln">$xhprof_runs </span><span class="pun">=</span><span class="kwd">new</span><span class="typ">XHProfRuns_Default</span><span class="pun">();</span>
  26. <span class="pln">$run_id </span><span class="pun">=</span><span class="pln"> $xhprof_runs</span><span class="pun">-></span><span class="pln">save_run</span><span class="pun">(</span><span class="pln">$xhprof_data</span><span class="pun">,</span><span class="str">"xhprof_foo"</span><span class="pun">);</span><span class="com">//source名称是xhprof_foo </span>
  27. <span class="com">//弹出一个统计窗口,查看统计信息 </span>
  28. <span class="pln">echo </span><span class="str">"<script language='javascript'>window.open('../xhprof_html/index.php?run="</span><span class="pun">.</span><span class="pln"> $run_id </span><span class="pun">.</span><span class="str">"&source=xhprof_foo');</script>"</span><span class="pun">;</span>
  29. <span class="pun">?></span>

以下是部分的结果:

  1. <span class="pun">[</span><span class="pln">foo</span><span class="pun">==></span><span class="pln">bar</span><span class="pun">]</span><span class="pun">=></span><span class="typ">Array</span>
  2. <span class="pun">(</span>
  3. <span class="pun">[</span><span class="pln">ct</span><span class="pun">]</span><span class="pun">=></span><span class="lit">5</span><span class="com">//bar()这个函数被调用了5次 </span>
  4. <span class="pun">[</span><span class="pln">wt</span><span class="pun">]</span><span class="pun">=></span><span class="lit">63</span><span class="com">//每次运行bar()所要的时间,不知道这个是不是平均值 </span>
  5. <span class="pun">[</span><span class="pln">cpu</span><span class="pun">]</span><span class="pun">=></span><span class="lit">0</span><span class="com">//每次运行bar(),cpu运算时间 </span>
  6. <span class="pun">[</span><span class="pln">mu</span><span class="pun">]</span><span class="pun">=></span><span class="lit">2860</span><span class="com">//每次运行bar(),php所使用内存的改变 </span>
  7. <span class="pun">[</span><span class="pln">pmu</span><span class="pun">]</span><span class="pun">=></span><span class="lit">0</span><span class="com">//每次运行bar(),php在内存使用最高峰时,所使用内存的改变 </span>
  8. <span class="pun">)</span>

个人觉得,这个工具是给变态人用的,有谁真正测试过,php代码执行效率?我估计没有,只要不随心所欲的写代码,差别不是很大,花时间在上面,还不如想想怎么提高数据库的处理能力。如果自认为其他方面都做的很好了,在这方面做做也没关系。

相关推荐