安装xhprof

/usr/local/php/bin/pecl install xhprof

配置php.ini文件

echo "extension=xhprof.so" >> /usr/local/php/conf/php.ini
echo "xhprof.output_dir=/tmp" >> /usr/local/php-7.4.24/conf/php.ini

验证xhprof模块

/usr/local/php/bin/php -m |grep xhprof
xhprof

下载xhprof代码

cd /data/apps
git clone https://github.com/patrickallaert/xhprof

范例业务代码

cat /data/apps/index.php
<?php
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

equire __DIR__ . '/../vendor/autoload.php';
use app\_;
_::handle()->bootWebApp();

$xhprof_data = xhprof_disable();

$XHPROF_ROOT = "/tools/xhprof/";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";

$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_testing");

echo "http://xhprof.wgs.com/index.php?run={$run_id}&source=xhprof_testing\n";

?>

结合nginx

server {

listen 80;

server_name xhprof.wgs.com ;
root /data/apps/xhprof/xhprof_html; #xhprof代码路径
access_log logs/xhprof_access.log main;
error_log logs/xhprof_error.log;

index index.html index.htm index.php;


location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}



}

查看结果

 

php性能分析工具xhprof_git