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. }

,

  1. No comments yet.
(will not be published)