Posts Tagged xhprof

xhprof自定义日志存放目录和文件名

用xhprof来分析PHP代码性能已经一段时间了,之前都按默认的配置,发现存放日志都要在同一个地方,文件名还是很不好识别的,极不方便,今天看了看xhprof的代码,发现这都可以配置的,罪过呀。
先POST一下原来的调用代码:

  1. $xhprof_random = mt_rand();
  2. if (function_exists('xhprof_enable') && $xhprof_random === 1) {
  3.     xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
  4. }
  5.  
  6. define('ROOT', dirname(__FILE__));
  7.  
  8. // Your code here
  9.  
  10. if (function_exists('xhprof_disable') && $xhprof_random === 1) {
  11.     $xhprof_data = xhprof_disable();
  12.     if (file_exists(ROOT . '/xhprof')) {
  13.         require_once ROOT . '/xhprof_lib/utils/xhprof_lib.php';
  14.         require_once ROOT . '/xhprof_lib/utils/xhprof_runs.php';
  15.         $xhprof_runs = new XHProfRuns_Default();
  16.         $xhprof_runs->save_run($xhprof_data, 'xhprof');
  17.     }
  18. }

查看/xhprof_lib/utils/xhprof_runs.php,可以看到在new XHProfRuns_Default()时,可以传一个参数,该参数即是日志存放路径,在调用save_run方法时,可以传第三个参数来指定文件名,这样一来,在分析日志时,通过日志文件名就知道是哪个请求的,方便多了,新的代码:

  1. $xhprof_random = mt_rand();
  2. if (function_exists('xhprof_enable') && $xhprof_random === 1) {
  3.     xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
  4. }
  5.  
  6. define('ROOT', dirname(__FILE__));
  7.  
  8. // Your code here
  9.  
  10. if (function_exists('xhprof_disable') && $xhprof_random === 1) {
  11.     $xhprof_data = xhprof_disable();
  12.     if (file_exists(ROOT . '/xhprof')) {
  13.         require_once ROOT . '/xhprof_lib/utils/xhprof_lib.php';
  14.         require_once ROOT . '/xhprof_lib/utils/xhprof_runs.php';
  15.         $xhprof_runs = new XHProfRuns_Default('../xhprof');
  16.         $file_name = 'gateway_' . $_GET['action'] . '_' . microtime(TRUE);
  17.         $xhprof_runs->save_run($xhprof_data, 'xhprof', $file_name);
  18.     }
  19. }

,

No Comments

用xhprof替代xdebug

以前一直在用xdebug作php的性能分析,但这东西太耗性能,只能在开发机上跑跑。前几天在网上看到xhprof这东西,是facebook开源的,别人都用上一年了,我才知道,唉,落伍了,废话少说,亡羊补牢,还未晚。
一、安装xhprof

  1. wget http://pecl.php.net/get/xhprof-0.9.2.tgz
  2. tar zxvf xhprof-0.9.2.tgz
  3. cd xhprof-0.9.2
  4. cp -r xhprof_html xhprof_lib <directory_for_htdocs> # 应用程序所在目录
  5. cd extension
  6. /usr/local/php/bin/phpize
  7. ./configure  --with-php-config=/usr/local/php/bin/php-config
  8. make
  9. make install
  10. </directory_for_htdocs>

Read the rest of this entry »

,

No Comments