Home > Article > Backend Development > 【PHP】替项目引入函数级的性能监控
【PHP】为项目引入函数级的性能监控
作者:zhanhailiang 日期:2013-03-18
使用PHP扩展Xhprof来实现函数级的性能监控。
引用官方对Xhprof的描述:
XHProf is a function-level hierarchical profiler for PHP and has a simple HTML based navigational interface. The raw data collection component is implemented in C (as a PHP extension). The reporting/UI layer is all in PHP. It is capable of reporting function-level inclusive and exclusive wall times, memory usage, CPU times and number of calls for each function. Additionally, it supports ability to compare two runs (hierarchical DIFF reports), or aggregate results from multiple runs.提供函数级别的性能信息,例如调用次数,其中包括阻塞时间,内存使用情况和CPU时间。1. 安装与配置
<span class="sy0" style="color:rgb(102,204,102)">/</span>data<span class="sy0" style="color:rgb(102,204,102)">/</span>software <span class="co0" style="color:rgb(128,128,128); font-style:italic"># wget http://pecl.php.net/get/xhprof-0.9.2.tgz</span> <span class="sy0" style="color:rgb(102,204,102)">/</span>data<span class="sy0" style="color:rgb(102,204,102)">/</span>software <span class="co0" style="color:rgb(128,128,128); font-style:italic"># tar zxvf xhprof-0.9.2.tgz</span> <span class="sy0" style="color:rgb(102,204,102)">/</span>data<span class="sy0" style="color:rgb(102,204,102)">/</span>software <span class="co0" style="color:rgb(128,128,128); font-style:italic"># cd xhprof-0.9.2/</span> <span class="sy0" style="color:rgb(102,204,102)">/</span>data<span class="sy0" style="color:rgb(102,204,102)">/</span>software<span class="sy0" style="color:rgb(102,204,102)">/</span>xhprof-0.9.2 <span class="co0" style="color:rgb(128,128,128); font-style:italic"># ls -alp</span> 总计 <span class="nu0" style="color:rgb(204,102,204)">968</span> drwxr-xr-x <span class="nu0" style="color:rgb(204,102,204)">6</span> root root <span class="nu0" style="color:rgb(204,102,204)">4096</span> <span class="nu0" style="color:rgb(204,102,204)">2013</span>-03-<span class="nu0" style="color:rgb(204,102,204)">18</span> 09:09 .<span class="sy0" style="color:rgb(102,204,102)">/</span> drwxr-xr-x <span class="nu0" style="color:rgb(204,102,204)">47</span> root root <span class="nu0" style="color:rgb(204,102,204)">8192</span> <span class="nu0" style="color:rgb(204,102,204)">2013</span>-03-<span class="nu0" style="color:rgb(204,102,204)">18</span> 09:09 ..<span class="sy0" style="color:rgb(102,204,102)">/</span> <span class="re5">-rw-r--r--</span> <span class="nu0" style="color:rgb(204,102,204)">1</span> <span class="nu0" style="color:rgb(204,102,204)">1425</span> <span class="kw2" style="color:rgb(0,0,0); font-weight:bold">users</span> <span class="nu0" style="color:rgb(204,102,204)">3359</span> <span class="nu0" style="color:rgb(204,102,204)">2009</span>-06-02 01:<span class="nu0" style="color:rgb(204,102,204)">52</span> CHANGELOG <span class="re5">-rw-r--r--</span> <span class="nu0" style="color:rgb(204,102,204)">1</span> <span class="nu0" style="color:rgb(204,102,204)">1425</span> <span class="kw2" style="color:rgb(0,0,0); font-weight:bold">users</span> <span class="nu0" style="color:rgb(204,102,204)">266</span> <span class="nu0" style="color:rgb(204,102,204)">2009</span>-06-02 01:<span class="nu0" style="color:rgb(204,102,204)">52</span> CREDITS drwxr-xr-x <span class="nu0" style="color:rgb(204,102,204)">2</span> root root <span class="nu0" style="color:rgb(204,102,204)">4096</span> <span class="nu0" style="color:rgb(204,102,204)">2013</span>-03-<span class="nu0" style="color:rgb(204,102,204)">18</span> 09:09 examples<span class="sy0" style="color:rgb(102,204,102)">/</span> <span class="co0" style="color:rgb(128,128,128); font-style:italic"># demo</span> drwxr-xr-x <span class="nu0" style="color:rgb(204,102,204)">8</span> root root <span class="nu0" style="color:rgb(204,102,204)">4096</span> <span class="nu0" style="color:rgb(204,102,204)">2013</span>-03-<span class="nu0" style="color:rgb(204,102,204)">18</span> 09:09 extension<span class="sy0" style="color:rgb(102,204,102)">/</span> <span class="co0" style="color:rgb(128,128,128); font-style:italic"># pecl </span> <span class="re5">-rw-r--r--</span> <span class="nu0" style="color:rgb(204,102,204)">1</span> <span class="nu0" style="color:rgb(204,102,204)">1425</span> <span class="kw2" style="color:rgb(0,0,0); font-weight:bold">users</span> <span class="nu0" style="color:rgb(204,102,204)">10174</span> <span class="nu0" style="color:rgb(204,102,204)">2009</span>-06-02 01:<span class="nu0" style="color:rgb(204,102,204)">52</span> LICENSE <span class="re5">-rw-r--r--</span> <span class="nu0" style="color:rgb(204,102,204)">1</span> <span class="nu0" style="color:rgb(204,102,204)">1425</span> <span class="kw2" style="color:rgb(0,0,0); font-weight:bold">users</span> <span class="nu0" style="color:rgb(204,102,204)">186</span> <span class="nu0" style="color:rgb(204,102,204)">2009</span>-06-02 01:<span class="nu0" style="color:rgb(204,102,204)">52</span> README <span class="re5">-rw-r--r--</span> <span class="nu0" style="color:rgb(204,102,204)">1</span> root root <span class="nu0" style="color:rgb(204,102,204)">931660</span> <span class="nu0" style="color:rgb(204,102,204)">2009</span>-06-02 01:<span class="nu0" style="color:rgb(204,102,204)">55</span> xhprof drwxr-xr-x <span class="nu0" style="color:rgb(204,102,204)">6</span> root root <span class="nu0" style="color:rgb(204,102,204)">4096</span> <span class="nu0" style="color:rgb(204,102,204)">2013</span>-03-<span class="nu0" style="color:rgb(204,102,204)">18</span> 09:09 xhprof_html<span class="sy0" style="color:rgb(102,204,102)">/</span> <span class="co0" style="color:rgb(128,128,128); font-style:italic"># 自带的界面包</span> drwxr-xr-x <span class="nu0" style="color:rgb(204,102,204)">4</span> root root <span class="nu0" style="color:rgb(204,102,204)">4096</span> <span class="nu0" style="color:rgb(204,102,204)">2013</span>-03-<span class="nu0" style="color:rgb(204,102,204)">12</span> <span class="nu0" style="color:rgb(204,102,204)">15</span>:<span class="nu0" style="color:rgb(204,102,204)">40</span> xhprof_lib<span class="sy0" style="color:rgb(102,204,102)">/</span> <span class="co0" style="color:rgb(128,128,128); font-style:italic"># 函数库</span> <span class="sy0" style="color:rgb(102,204,102)">/</span>data<span class="sy0" style="color:rgb(102,204,102)">/</span>software<span class="sy0" style="color:rgb(102,204,102)">/</span>xhprof-0.9.2 <span class="co0" style="color:rgb(128,128,128); font-style:italic"># cd extension/</span> <span class="sy0" style="color:rgb(102,204,102)">/</span>data<span class="sy0" style="color:rgb(102,204,102)">/</span>software<span class="sy0" style="color:rgb(102,204,102)">/</span>xhprof-0.9.2<span class="sy0" style="color:rgb(102,204,102)">/</span>extension <span class="co0" style="color:rgb(128,128,128); font-style:italic"># phpize </span> <span class="sy0" style="color:rgb(102,204,102)">/</span>data<span class="sy0" style="color:rgb(102,204,102)">/</span>software<span class="sy0" style="color:rgb(102,204,102)">/</span>xhprof-0.9.2<span class="sy0" style="color:rgb(102,204,102)">/</span>extension <span class="co0" style="color:rgb(128,128,128); font-style:italic"># ./configure --with-php-config=/usr/local/services/php/bin/php-config --enable-xhprof</span> <span class="sy0" style="color:rgb(102,204,102)">/</span>data<span class="sy0" style="color:rgb(102,204,102)">/</span>software<span class="sy0" style="color:rgb(102,204,102)">/</span>xhprof-0.9.2<span class="sy0" style="color:rgb(102,204,102)">/</span>extension <span class="co0" style="color:rgb(128,128,128); font-style:italic"># make</span> <span class="sy0" style="color:rgb(102,204,102)">/</span>data<span class="sy0" style="color:rgb(102,204,102)">/</span>software<span class="sy0" style="color:rgb(102,204,102)">/</span>xhprof-0.9.2<span class="sy0" style="color:rgb(102,204,102)">/</span>extension <span class="co0" style="color:rgb(128,128,128); font-style:italic"># make install</span> Installing shared extensions: <span class="sy0" style="color:rgb(102,204,102)">/</span>usr<span class="sy0" style="color:rgb(102,204,102)">/</span>local<span class="sy0" style="color:rgb(102,204,102)">/</span>services<span class="sy0" style="color:rgb(102,204,102)">/</span>php<span class="sy0" style="color:rgb(102,204,102)">/</span>lib<span class="sy0" style="color:rgb(102,204,102)">/</span>php<span class="sy0" style="color:rgb(102,204,102)">/</span>extensions<span class="sy0" style="color:rgb(102,204,102)">/</span>no-debug-non-zts-<span class="nu0" style="color:rgb(204,102,204)">20090626</span><span class="sy0" style="color:rgb(102,204,102)">/</span> <span class="sy0" style="color:rgb(102,204,102)">/</span>data<span class="sy0" style="color:rgb(102,204,102)">/</span>software<span class="sy0" style="color:rgb(102,204,102)">/</span>xhprof-0.9.2<span class="sy0" style="color:rgb(102,204,102)">/</span>extension <span class="co0" style="color:rgb(128,128,128); font-style:italic"># cp /usr/local/services/php/lib/php/extensions/no-debug-non-zts-20090626/xhprof.so /usr/local/services/php/extensions/</span> <span class="co0" style="color:rgb(128,128,128); font-style:italic"># 将生成的xhprof.so复制到extension_dir目录下,配置php.ini,重启php-fpm或服务器即可</span> <span class="sy0" style="color:rgb(102,204,102)">//</span> php.ini <span class="br0" style="color:rgb(102,204,102)">[</span>xhprof<span class="br0" style="color:rgb(102,204,102)">]</span> <span class="re2" style="color:rgb(0,0,255)">extension</span>=xhprof.so xhprof.output_dir=<span class="st0" style="color:rgb(255,0,0)">"/data/logs/servers/xhprof"</span> <span class="co0" style="color:rgb(128,128,128); font-style:italic"># 检查xhprof是否加载</span> <span class="sy0" style="color:rgb(102,204,102)">/</span>data<span class="sy0" style="color:rgb(102,204,102)">/</span>software<span class="sy0" style="color:rgb(102,204,102)">/</span>xhprof-0.9.2<span class="sy0" style="color:rgb(102,204,102)">/</span>extension <span class="co0" style="color:rgb(128,128,128); font-style:italic"># php -m|grep xhprof</span> xhprof2.性能分析与日志记录
首先添加如下代码,以便记录十分之一的请求的性能分析日志(日志记录在/data/logs/servers/xhprof):
<span class="re0" style="color:rgb(0,0,255)">$xhprofEnabled</span> <span class="sy0" style="color:rgb(102,204,102)">=</span> <span class="kw4" style="color:rgb(153,51,51)">FALSE</span><span class="sy0" style="color:rgb(102,204,102)">;</span> <span class="kw1" style="color:rgb(177,177,0)">if</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="kw3" style="color:rgb(0,0,102)">extension_loaded</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="st_h">'xhprof'</span><span class="br0" style="color:rgb(102,204,102)">)</span> <span class="sy0" style="color:rgb(102,204,102)">&&</span> <span class="kw3" style="color:rgb(0,0,102)">mt_rand</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="nu0" style="color:rgb(204,102,204)">1</span><span class="sy0" style="color:rgb(102,204,102)">,</span> <span class="nu0" style="color:rgb(204,102,204)">10</span><span class="br0" style="color:rgb(102,204,102)">)</span> <span class="sy0" style="color:rgb(102,204,102)">===</span> <span class="nu0" style="color:rgb(204,102,204)">1</span><span class="br0" style="color:rgb(102,204,102)">)</span> <span class="br0" style="color:rgb(102,204,102)">{</span> <span class="co1" style="color:rgb(128,128,128); font-style:italic">// 分析十分之一的请求性能</span> <span class="co1" style="color:rgb(128,128,128); font-style:italic">// 开启性能监测</span> xhprof_enable<span class="br0" style="color:rgb(102,204,102)">(</span><span class="coMULTI" style="color:rgb(128,128,128); font-style:italic">/*XHPROF_FLAGS_CPU + */</span>XHPROF_FLAGS_MEMORY <span class="sy0" style="color:rgb(102,204,102)">+</span> XHPROF_FLAGS_NO_BUILTINS <span class="br0" style="color:rgb(102,204,102)">)</span><span class="sy0" style="color:rgb(102,204,102)">;</span> <span class="re0" style="color:rgb(0,0,255)">$xhprofEnabled</span> <span class="sy0" style="color:rgb(102,204,102)">=</span> <span class="kw4" style="color:rgb(153,51,51)">TRUE</span><span class="sy0" style="color:rgb(102,204,102)">;</span> <span class="br0" style="color:rgb(102,204,102)">}</span>接着注册请求结束处理器,以便请求结束时记录下xhprof性能分析日志:
<span class="kw2" style="color:rgb(0,0,0); font-weight:bold">function</span><span style="color:#333333"> profile_record</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333"> </span><span class="br0" style="color:rgb(102,204,102)">{</span><span style="color:#333333"> </span><span class="kw1" style="color:rgb(177,177,0)">if</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="kw3" style="color:rgb(0,0,102)">isset</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="re0" style="color:rgb(0,0,255)">$GLOBALS</span><span class="br0" style="color:rgb(102,204,102)">[</span><span class="st_h" style="color:rgb(51,51,51)">'xhprofEnabled'</span><span class="br0" style="color:rgb(102,204,102)">]</span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333"> </span><span class="sy0" style="color:rgb(102,204,102)">&&</span><span style="color:#333333"> </span><span class="re0" style="color:rgb(0,0,255)">$GLOBALS</span><span class="br0" style="color:rgb(102,204,102)">[</span><span class="st_h" style="color:rgb(51,51,51)">'xhprofEnabled'</span><span class="br0" style="color:rgb(102,204,102)">]</span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333"> </span><span class="br0" style="color:rgb(102,204,102)">{</span><span style="color:#333333"> </span><span class="re0" style="color:rgb(0,0,255)">$xhprof_data</span><span style="color:#333333"> </span><span class="sy0" style="color:rgb(102,204,102)">=</span><span style="color:#333333"> xhprof_disable</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="br0" style="color:rgb(102,204,102)">)</span><span class="sy0" style="color:rgb(102,204,102)">;</span><span style="color:#333333"> </span><span class="re0" style="color:rgb(0,0,255)">$path</span><span style="color:#333333"> </span><span class="sy0" style="color:rgb(102,204,102)">=</span><span style="color:#333333"> </span><span class="re0" style="color:rgb(0,0,255)">$_SERVER</span><span class="br0" style="color:rgb(102,204,102)">[</span><span class="st_h" style="color:rgb(51,51,51)">'PHP_SELF'</span><span class="br0" style="color:rgb(102,204,102)">]</span><span class="sy0" style="color:rgb(102,204,102)">;</span><span style="color:#333333"> </span><span class="kw1" style="color:rgb(177,177,0)">if</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="re0" style="color:rgb(0,0,255)">$pos</span><span style="color:#333333"> </span><span class="sy0" style="color:rgb(102,204,102)">=</span><span style="color:#333333"> </span><span class="kw3" style="color:rgb(0,0,102)">strpos</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="re0" style="color:rgb(0,0,255)">$path</span><span class="sy0" style="color:rgb(102,204,102)">,</span><span style="color:#333333"> </span><span class="st_h" style="color:rgb(51,51,51)">'/htdocs'</span><span class="br0" style="color:rgb(102,204,102)">)</span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333"> </span><span class="br0" style="color:rgb(102,204,102)">{</span><span style="color:#333333"> </span><span class="re0" style="color:rgb(0,0,255)">$path</span><span style="color:#333333"> </span><span class="sy0" style="color:rgb(102,204,102)">=</span><span style="color:#333333"> </span><span class="kw3" style="color:rgb(0,0,102)">substr</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="re0" style="color:rgb(0,0,255)">$path</span><span class="sy0" style="color:rgb(102,204,102)">,</span><span style="color:#333333"> </span><span class="re0" style="color:rgb(0,0,255)">$pos</span><span class="sy0" style="color:rgb(102,204,102)">+</span><span class="nu0" style="color:rgb(204,102,204)">7</span><span class="br0" style="color:rgb(102,204,102)">)</span><span class="sy0" style="color:rgb(102,204,102)">;</span><span style="color:#333333"> </span><span class="br0" style="color:rgb(102,204,102)">}</span><span style="color:#333333"> </span><span class="re0" style="color:rgb(0,0,255)">$basename</span><span style="color:#333333"> </span><span class="sy0" style="color:rgb(102,204,102)">=</span><span style="color:#333333"> </span><span class="kw3" style="color:rgb(0,0,102)">str_replace</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="kw3" style="color:rgb(0,0,102)">array</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="st_h" style="color:rgb(51,51,51)">'.'</span><span class="sy0" style="color:rgb(102,204,102)">,</span><span style="color:#333333"> </span><span class="st_h" style="color:rgb(51,51,51)">'/'</span><span class="br0" style="color:rgb(102,204,102)">)</span><span class="sy0" style="color:rgb(102,204,102)">,</span><span style="color:#333333"> </span><span class="st_h" style="color:rgb(51,51,51)">'_'</span><span class="sy0" style="color:rgb(102,204,102)">,</span><span style="color:#333333"> </span><span class="kw3" style="color:rgb(0,0,102)">ltrim</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="re0" style="color:rgb(0,0,255)">$path</span><span class="sy0" style="color:rgb(102,204,102)">,</span><span style="color:#333333"> </span><span class="st_h" style="color:rgb(51,51,51)">'/'</span><span class="br0" style="color:rgb(102,204,102)">)</span><span class="br0" style="color:rgb(102,204,102)">)</span><span class="sy0" style="color:rgb(102,204,102)">;</span><span style="color:#333333"> </span><span class="coMULTI" style="color:rgb(128,128,128); font-style:italic">/* 官方Demo,请参考下载包里的</span><span class="coMULTI" style="font-style:italic"><span style="color:#ff0000">xhprof-0.9.2/examples/sample.php</span></span><span class="coMULTI" style="color:rgb(128,128,128); font-style:italic"> * // * // Saving the XHProf run * // using the default implementation of iXHProfRuns. * // * include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php"; * include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php"; * $xhprof_runs = new XHProfRuns_Default(); */</span><span style="color:#333333"> </span><span class="re0" style="color:rgb(0,0,255)">$xhprof_runs</span><span style="color:#333333"> </span><span class="sy0" style="color:rgb(102,204,102)">=</span><span style="color:#333333"> </span><span class="kw2" style="color:rgb(0,0,0); font-weight:bold">new</span><span style="color:#333333"> HWSL_XhProfRuns</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="br0" style="color:rgb(102,204,102)">)</span><span class="sy0" style="color:rgb(102,204,102)">;</span><span style="color:#333333"> </span><span class="co1" style="color:rgb(128,128,128); font-style:italic">// 记录日志时引入时间截,方便比较不同版本间的性能差异</span><span style="color:#333333"> </span><span class="re0" style="color:rgb(0,0,255)">$run_id</span><span style="color:#333333"> </span><span class="sy0" style="color:rgb(102,204,102)">=</span><span style="color:#333333"> </span><span class="re0" style="color:rgb(0,0,255)">$xhprof_runs</span><span class="sy0" style="color:rgb(102,204,102)">-></span><span class="me1" style="color:rgb(0,102,0)">save_run</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="re0" style="color:rgb(0,0,255)">$xhprof_data</span><span class="sy0" style="color:rgb(102,204,102)">,</span><span style="color:#333333"> </span><span class="st_h" style="color:rgb(51,51,51)">'xhprof_itravel_'</span><span class="sy0" style="color:rgb(102,204,102)">.</span><span class="re0" style="color:rgb(0,0,255)">$basename</span><span class="sy0" style="color:rgb(102,204,102)">.</span><span class="st_h" style="color:rgb(51,51,51)">'_'</span><span class="sy0" style="color:rgb(102,204,102)">.</span><span class="kw3" style="color:rgb(0,0,102)">date</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="st_h" style="color:rgb(51,51,51)">'YmdHis'</span><span class="br0" style="color:rgb(102,204,102)">)</span><span class="br0" style="color:rgb(102,204,102)">)</span><span class="sy0" style="color:rgb(102,204,102)">;</span><span style="color:#333333"> </span><span class="br0" style="color:rgb(102,204,102)">}</span><span style="color:#333333"> </span><span class="br0" style="color:rgb(102,204,102)">}</span><span style="color:#333333"> </span><span class="kw3" style="color:rgb(0,0,102)">register_shutdown_function</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="st_h" style="color:rgb(51,51,51)">'profile_record'</span><span class="br0" style="color:rgb(102,204,102)">)</span><span class="sy0" style="color:rgb(102,204,102)">;</span>生成的日志以文件保存在:xhprof.output_dir=”/data/logs/servers/xhprof”
生成的日志文件命名规则为:
. 其中命名空间
的命名规则定义为:'xhprof_itravel_'.$basename.'_'.date('YmdHis') $basename记录相应的文件位置,date(‘YmdHis’)记录当前时间戳,以便比较同一文件不同版本间的性能差异。
3.日志分析
然后,通过官方提供的用户界面包xhprof-0.9.2/xhprof_html/来查看运行报告:
要查看run_id是
和命名空间是 的报告,访问URL: http://
/ index.php?run=&source= 其中各列含义如下:
Inclusive Time (或子树执行时间) :包括子树执行时间的所有执行时间。 Exclusive Time/Self Time :函数执行本身的时间花费。不包括子树执行时间。 Wall时间 :又名经过的时间或挂钟时间。 CPU时间 : CPU时间在用户空间+ CPU时间在内核空间。也可以查看图形化的性能分析报告(其中高亮显示的路径直观反映了函数调用栈中最大的性能损耗路径):
http://
/ callgraph.php?run=&source= 4.扩展阅读
在生产环境中使用php性能测试工具xhprof 关于改善xhprof使用情况的设想 XHProf文件(草稿)