2012. 4. 4. 12:11

[XHProf]php profiling tool




XHProf (Facebook, Apache 2.0 license)

설치---------------------------------------

wget http://pecl.php.net/get/xhprof-0.9.2.tgz 

tar xvf xhprof-0.9.2.tgz 

cd ./xhprof-0.9.2/extension/ 

phpize 

./configure --with-php-config=/usr/local/bin/php-config 

make 

make install 


설정---------------------------------------

php.ini 

[xhprof] 

extension=xhprof.so 

xhprof.output_dir="/var/tmp/xhprof" 


적용---------------------------------------

header 에 추가 

if (extension_loaded('xhprof')) { 

    include_once '/usr/local/lib/php/xhprof_lib/utils/xhprof_lib.php'; // xhprof 소스위치 수정 

    include_once '/usr/local/lib/php/xhprof_lib/utils/xhprof_runs.php'; // xhprof 소스위치 수정 

    xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY); 

footer 에 추가 

if (extension_loaded('xhprof')) { 

    $profiler_namespace = 'myapp';  // namespace for your application 

    $xhprof_data = xhprof_disable(); 

    $xhprof_runs = new XHProfRuns_Default(); 

    $run_id = $xhprof_runs->save_run($xhprof_data, $profiler_namespace); 

  

    // xhprof 소스위치 및 URL 수정 

    $profiler_url = sprintf('http://myhost.com/xhprof/xhprof_html/index.php?run=%s&source=%s', $run_id, $profiler_namespace); 

    echo '<a href="'. $profiler_url .'" target="_blank">Profiler output</a>'; 


기타---------------------------------------

1. 그래프로 확인하려면 graphviz 필요 (http://www.graphviz.org/) 

2. 사이트 전체 profile를 위해서는 php.ini 에서 

    auto_prepend_file, auto_append_file 이용 (개발서버에서만 붙이세요) 

3. XHprof 확장(mysql에 기록 및 확장된 UI) - 강력추천 

    git clone https://github.com/preinheimer/xhprof.git 

    설치 방법은 위와 거의 비슷 (https://github.com/preinheimer/xhprof/blob/master/INSTALL)