silly / package
PHP 性能 xhprof
dev-master
2018-05-28 15:51 UTC
This package is not auto-updated.
Last update: 2024-09-21 00:09:15 UTC
README
PHP 代码性能插件
配置
1. 安装 xhprof
获取源代码
您可以从网站 https://github.com/longxinH/xhprof 获取源代码,并在我的机器(centos 6.5)上安装。
cd xhprof-master/extension/
sudo /usr/bin/phpize
sudo ./configure --with-php-config=/usr/bin/php-config --enable-xhprof
sudo make && make install
作为 PHP 扩展加载
您将获得 xhprof.so 的路径,在执行 make install 命令后,使用 vim 打开 php.ini 文件,
vim /etc/php.ini
extension=/usr/lib64/php/modules/xhprof.so
xhprof.output_dir=/tmp/xhprof // this config is used to analyze the xhprof data
sudo service php-fpm restart
您可以使用 php -m | grep xhprof 或 phpinfo 检查是否正确安装了 xhprof。
2. 配置 xhprof-html 以供 nginx 访问
配置 nginx,以便可以通过表格和图形访问 xhprof 数据。
server {
listen 13000;
location / {
root xhprof_html directory path;
try_files $uri $uri/ /index.php?$query_string;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
3. 将 xhprof.so 和 xhprof 文件输出添加到 php.ini
将 xhprof.so 和输出目标文件添加到 php.ini
extension=/usr/lib64/php/modules/xhprof.so
xhprof.output_dir=/tmp/xhprof
4. 如何使用此插件
执行以下命令
composer require silly/package dev-master
然后您将在下面的 composer.json 中的 require 部分找到项目
"silly/package": "dev-master"
在您的代码中使用 Xhprof 类
use silly\package\Xhprof;
$xf = new Xhprof();
$xh->xhprof_start();
....your code....
$data = $xh->xhprof_end();
$host = 'xxxx/index.php?run='; //the path you visit xhprof_html directory
$xf->xhprof_display($data, $host);
然后会打印出 URL,例如(http://your_host/index.php?run=5b0c1bf8a8875&source=xhprof),只需在浏览器中打开它,它将显示表格中的数据,并点击 [查看完整调用图] 链接,您将看到函数调用链接。只需关注带有黄色和红色颜色的链接并对其进行优化。
一些有用的信息
您可能遇到以下问题。
Error: either we can not find profile data for run_id 4d7f0bd99a12f or
the threshold 0.01 is too small or you do not have ‘dot’ image
generation utility installed
这是因为 xhprof 绘制 png 图形,您应该升级 dot 版本,只需安装 graphviz。
sudo yum install -y graphviz
请确保 graphviz 的版本正确,我的版本是 graphviz-2.26.0,并且图形可以正确显示。