【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时间。
/data/software # wget http://pecl.php.net/get/xhprof-0.9.2.tgz/data/software # tar zxvf xhprof-0.9.2.tgz/data/software # cd xhprof-0.9.2//data/software/xhprof-0.9.2 # ls -alp总计 968drwxr-xr-x 6 root root 4096 2013-03-18 09:09 ./drwxr-xr-x 47 root root 8192 2013-03-18 09:09 ../1 1425 users 3359 2009-06-02 01:52 CHANGELOG1 1425 users 266 2009-06-02 01:52 CREDITSdrwxr-xr-x 2 root root 4096 2013-03-18 09:09 examples/ # demodrwxr-xr-x 8 root root 4096 2013-03-18 09:09 extension/ # pecl 1 1425 users 10174 2009-06-02 01:52 LICENSE1 1425 users 186 2009-06-02 01:52 README1 root root 931660 2009-06-02 01:55 xhprofdrwxr-xr-x 6 root root 4096 2013-03-18 09:09 xhprof_html/ # 自带的界面包drwxr-xr-x 4 root root 4096 2013-03-12 15:40 xhprof_lib/ # 函数库/data/software/xhprof-0.9.2 # cd extension//data/software/xhprof-0.9.2/extension # phpize /data/software/xhprof-0.9.2/extension # ./configure --with-php-config=/usr/local/services/php/bin/php-config --enable-xhprof/data/software/xhprof-0.9.2/extension # make/data/software/xhprof-0.9.2/extension # make installInstalling shared extensions: /usr/local/services/php/lib/php/extensions/no-debug-non-zts-20090626//data/software/xhprof-0.9.2/extension # cp /usr/local/services/php/lib/php/extensions/no-debug-non-zts-20090626/xhprof.so /usr/local/services/php/extensions/# 将生成的xhprof.so复制到extension_dir目录下,配置php.ini,重启php-fpm或服务器即可 // php.ini[xhprof]extension=xhprof.soxhprof.output_dir="/data/logs/servers/xhprof" # 检查xhprof是否加载/data/software/xhprof-0.9.2/extension # php -m|grep xhprofxhprof
首先添加如下代码,以便记录十分之一的请求的性能分析日志(日志记录在/data/logs/servers/xhprof):
$xhprofEnabled = FALSE;if(extension_loaded() && mt_rand(1, 10) === 1) { // 分析十分之一的请求性能 // 开启性能监测 xhprof_enable(/*XHPROF_FLAGS_CPU + */XHPROF_FLAGS_MEMORY + XHPROF_FLAGS_NO_BUILTINS ); $xhprofEnabled = TRUE;}
接着注册请求结束处理器,以便请求结束时记录下xhprof性能分析日志:
function profile_record() { if(isset($GLOBALS['xhprofEnabled']) && $GLOBALS['xhprofEnabled']) { $xhprof_data = xhprof_disable(); $path = $_SERVER['PHP_SELF']; if($pos = strpos($path, '/htdocs')) { $path = substr($path, $pos+7); } $basename = str_replace(array('.', '/'), '_', ltrim($path, '/')); /* 官方Demo,请参考下载包里的xhprof-0.9.2/examples/sample.php * // * // 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(); */ $xhprof_runs = new HWSL_XhProfRuns(); // 记录日志时引入时间截,方便比较不同版本间的性能差异 $run_id = $xhprof_runs->save_run($xhprof_data, 'xhprof_itravel_'.$basename.'_'.date('YmdHis')); }} register_shutdown_function('profile_record');
生成的日志以文件保存在:xhprof.output_dir=”/data/logs/servers/xhprof”
生成的日志文件命名规则为:<run_id>.<namespace>
其中命名空间<namespace>的命名规则定义为:'xhprof_itravel_'.$basename.'_'.date('YmdHis')
$basename记录相应的文件位置,date(‘YmdHis’)记录当前时间戳,以便比较同一文件不同版本间的性能差异。
然后,通过官方提供的用户界面包xhprof-0.9.2/xhprof_html/来查看运行报告:
要查看run_id是<run_id>和命名空间是<namespace>的报告,访问URL:
http://<xhprof-ui-address>/index.php?run=<run_id>&source=<namespace>
其中各列含义如下:
也可以查看图形化的性能分析报告(其中高亮显示的路径直观反映了函数调用栈中最大的性能损耗路径):
http://<xhprof-ui-address>/callgraph.php?run=<run_id>&source=<namespace>