Posts Tagged xhprof
xhprof自定义日志存放目录和文件名
用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);
- }
- }
用xhprof替代xdebug
以前一直在用xdebug作php的性能分析,但这东西太耗性能,只能在开发机上跑跑。前几天在网上看到xhprof这东西,是facebook开源的,别人都用上一年了,我才知道,唉,落伍了,废话少说,亡羊补牢,还未晚。
一、安装xhprof
- wget http://pecl.php.net/get/xhprof-0.9.2.tgz
- tar zxvf xhprof-0.9.2.tgz
- cd xhprof-0.9.2
- cp -r xhprof_html xhprof_lib <directory_for_htdocs> # 应用程序所在目录
- cd extension
- /usr/local/php/bin/phpize
- ./configure --with-php-config=/usr/local/php/bin/php-config
- make
- make install
- </directory_for_htdocs>
近期评论