用xhprof来分析PHP代码性能已经一段时间了,之前都按默认的配置,发现存放日志都要在同一个地方,文件名还是很不好识别的,极不方便,今天看了看xhprof的代码,发现这都可以配置的,罪过呀。
先POST一下原来的调用代码:
- $xhprof_random = mt_rand();
- if (function_exists('xhprof_enable') && $xhprof_random === 1) {
- xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
- }
- define('ROOT', dirname(__FILE__));
- // Your code here
- if (function_exists('xhprof_disable') && $xhprof_random === 1) {
- $xhprof_data = xhprof_disable();
- if (file_exists(ROOT . '/xhprof')) {
- require_once ROOT . '/xhprof_lib/utils/xhprof_lib.php';
- require_once ROOT . '/xhprof_lib/utils/xhprof_runs.php';
- $xhprof_runs = new XHProfRuns_Default();
- $xhprof_runs->save_run($xhprof_data, 'xhprof');
- }
- }
查看/xhprof_lib/utils/xhprof_runs.php,可以看到在new XHProfRuns_Default()时,可以传一个参数,该参数即是日志存放路径,在调用save_run方法时,可以传第三个参数来指定文件名,这样一来,在分析日志时,通过日志文件名就知道是哪个请求的,方便多了,新的代码:
- $xhprof_random = mt_rand();
- if (function_exists('xhprof_enable') && $xhprof_random === 1) {
- xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
- }
- define('ROOT', dirname(__FILE__));
- // Your code here
- if (function_exists('xhprof_disable') && $xhprof_random === 1) {
- $xhprof_data = xhprof_disable();
- if (file_exists(ROOT . '/xhprof')) {
- require_once ROOT . '/xhprof_lib/utils/xhprof_lib.php';
- require_once ROOT . '/xhprof_lib/utils/xhprof_runs.php';
- $xhprof_runs = new XHProfRuns_Default('../xhprof');
- $file_name = 'gateway_' . $_GET['action'] . '_' . microtime(TRUE);
- $xhprof_runs->save_run($xhprof_data, 'xhprof', $file_name);
- }
- }
近期评论