Xhprof分析网站运行性能

Summary: Author: 张亚飞 | 阅读时间: 2 minute read | Published: 2015-08-08
Filed under Categories: ServerTags: Linux, Server, Software,


Tideways和xhgui打造PHP非侵入式监控平台 perftools/xhgui Self-hosted XHProf (free & opensource)

安装 Xhprof so 扩展

/opt/data

wget http://pecl.php.net/get/xhprof-0.9.4.tgz
tar zxvf xhprof-0.9.4.tgz
cd xhprof-0.9.4/extension
phpize
./configure --with-php-config=/usr/bin/php-config
make && make install

注意: 以上不适用于 php7 可用以下替代方式

/opt/data

git clone https://github.com/longxinH/xhprof.git ./xhprof
cd xhprof/extension/
phpize
./configure --with-php-config=/usr/bin/php-config
make && sudo make install

以上步骤将会在 /usr/lib/php/20151012/ 下生成 xhprof.so 文件

  • 分别添加到两个配置文件
sudo vi /etc/php/7.2/fpm/conf.d/xhprof.ini
sudo vi /etc/php/7.2/cli/conf.d/xhprof.ini
extension = xhprof.so;
xhprof.output_dir = /data/home/coam/Server/Run/Xhprof/logs/
  • 注意:output_dir 必须存在且有写权限写

重启服务器


下载 https://github.com/phacility/xhprof 到网站根目录

这里 /data/home/coam/www/www/public/Xhprof

////////////////////////////////////////////////运行/////////////////////////////////////////////////

/data/home/coam/www/www/public/index.php

开始添加

xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

最后添加

$xhprof_data = xhprof_disable('/tmp');
$XHPROF_ROOT = "/data/home/coam/www/www/public/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");
$XhprofRunHerf =  "http://www.syam.cc/Xhprof/xhprof_html/index.php?run={$run_id}&source=xhprof_testing\n";
lg($XhprofRunHerf);

访问 http://www.syam.cc

可以看到 /data/home/coam/www/www/LogXhprof 下生成的日志文件 55a2090749e2c.xhprof_testing.xhprof

根据生成的链接 访问打印的查看运行结果:

http://www.syam.cc/Xhprof/xhprof_html/index.php?run=55a2090a197df&source=xhprof_testing


安装 graphviz 查看系统运行导向图

官网

安装libpng

wget -c http://sourceforge.net/projects/libpng/files/libpng16/1.6.27/libpng-1.6.27.tar.gz
wget https://jaist.dl.sourceforge.net/project/libpng/libpng16/1.6.29/libpng-1.6.29.tar.xz
tar -xzvf ./libpng-1.6.29.tar.gz
cd ./libpng-1.6.29
./configure
make && make install

GitHub

# wget http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.38.0.tar.gz
#git clone https://github.com/ellson/graphviz.git
wget https://github.com/ellson/graphviz/releases/download/Nightly/graphviz-2.41.20170103.1755.tar.gz
tar zxf graphviz-xxx.tar.gz
cd graphviz-xxx
./configure --with-png=yes
make
make install
  • 注意一定要在 github 下载发布的 [graphviz-2.41.20170103.1755.tar.gz] 版本,否则报错:
failed to execute cmd: " dot -Tpng". stderr: `Format: "png" not recognized. Use one of: canon cmap cmapx cmapx_np dot eps fig gd gd2 gv imap imap_np ismap pic plain plain-ext pov ps ps2 svg svgz tk vml vmlz xdot xdot1.2 xdot1.4 '
plugin libraries:
  gd:            No (gd headers not found)

可以在终端输入如下命令判断 graphviz 是否安装成功

Fri Mar 17 20:44:41 coam@ms:~/Server/Run/Docs/zz$ graphml2gv -h
graphml2gv: option -h unrecognized
Usage: graphml2gv [-gd?] [-o<file>] [<graphs>]
 -g<name>  : use <name> as template for graph names
 -o<file>  : output to <file> (stdout)
 -v        : verbose mode
 -?        : usage

Reference

在ubuntu,lampp下使用xhprof 给CentOS.6.3 + PHP5.3 安装PHP性能测试工具 XHProf-0.9.2 Graphviz安装失败的一个尝试 编译xhprof时的一个小意外

Comments

Cor-Ethan, the beverage → www.iirii.com