環(huán)境
Mac + PHP7 + Nginx
安裝
這里采用的 pecl 方式安裝
// 下載
wget https://pecl.php.net/get/xhprof-2.1.3.tgz
// 安裝
pecl install xhprof-2.1.3.tgz
配置 php.ini
[xhprof]
extension=xhprof.so
# 報告文件存儲位置,推薦配置一下。不配的話會使用'系統(tǒng)臨時目錄' - 從源碼的 `$dir = sys_get_temp_dir();` 得知
xhprof.output_dir=/var/www/xhprof/xhprof_runs/
檢查
-
php -m
-
phpinfo()
(記得重啟php-fpm)
使用示例
xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);
/**
...
被檢查的php代碼
...
**/
$xhprof_data = xhprof_disable();
// 引入xhprof | 這里的絕對路徑可以自己 `find / -name xhprof_runs.php` 搜索下
include_once "/usr/local/share/pear/xhprof_lib/utils/xhprof_lib.php";
include_once "/usr/local/share/pear/xhprof_lib/utils/xhprof_runs.php";
$instance = new XHProfRuns_Default(); // 有命名空間的項目可能需要這樣寫 $instance = new \XHProfRuns_Default();
$run_id = $instance->save_run($xhprof_data, "xhprof");
var_dump($run_id);
配置查看頁面
- nginx 配置
server {
listen 80;
server_name xhprof.test.com;
root /usr/local/share/pear/xhprof_html;
index index.html index.php;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
-
查看
相關鏈接
- package xhprof : https://pecl.php.net/package/xhprof
- github xhprof :https://github.com/longxinH/xhprof
- 參考鏈接 :http://www.reibang.com/p/38e3ae81970c
- XHProf 報告字段含義解析 :https://www.jb51.net/article/84456.htm