php-xhprof安装-使用

介绍

官网的我直接拿过来了

XHProf 是一个轻量级的分层性能测量分析器。 在数据收集阶段,它跟踪调用次数与测量数据,展示程序动态调用的弧线图。 它在报告、后期处理阶段计算了独占的性能度量,例如运行经过的时间、CPU 计算时间和内存开销。 函数性能报告可以由调用者和被调用者终止。 在数据搜集阶段 XHProf 通过调用图的循环来检测递归函数,通过赋予唯一的深度名称来避免递归调用的循环。

XHProf 包含了一个基于 HTML 的简单用户界面(由 PHP 写成)。 基于浏览器的用户界面使得浏览、分享性能数据结果更加简单方便。 同时也支持查看调用图。

XHProf 的报告对理解代码执行结构常常很有帮助。 比如此分层报告可用于确定在哪个调用链里调用了某个函数。

XHProf 对两次运行进行比较(又名 “diff” 报告),或者多次运行数据的合计。 对比、合并报告,很像针对单次运行的“平式视图”性能报告,就像“分层式视图”的性能报告

更多额外文档可以在 » facebook xhprof上找到。

安装

安装xhprof

下载地址:https://pecl.php.net/package/xhprof

由于我这里下载xhprof

/var/www/html # pecl list
Installed packages, channel pecl.php.net:
=========================================
Package Version State
redis   4.2.0   stable
xhprof  0.9.4   beta

先进行卸载

/var/www/html # pecl uninstall xhprof
Unable to remove "extension=xhprof.so" from php.ini
uninstall ok: channel://pecl.php.net/xhprof-0.9.
/var/www/html # pecl list
Installed packages, channel pecl.php.net:
=========================================
Package Version State
redis   4.2.0   stable

选择对应的版本
在这里插入图片描述
我这里下载的是0.9.4

/var/www/html # wget https://pecl.php.net/get/xhprof-0.9.4.tgz
--2021-07-15 10:28:07--  https://pecl.php.net/get/xhprof-0.9.4.tgz
Resolving pecl.php.net... 104.236.228.160
Connecting to pecl.php.net|104.236.228.160|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 842077 (822K) [application/octet-stream]
Saving to: 'xhprof-0.9.4.tgz'

xhprof-0.9.4.tgz                              100%[==============================================================================================>] 822.34K   246KB/s    in 3.3s

2021-07-15 10:28:13 (246 KB/s) - 'xhprof-0.9.4.tgz' saved [842077/842077]

/var/www/html # rm -rf xhprof-0.9.4*
/var/www/html # pecl install https://pecl.php.net/get/xhprof-0.9.4.tgz
downloading xhprof-0.9.4.tgz ...
Starting to download xhprof-0.9.4.tgz (842,077 bytes)
........................................................................................................................................................................done: 842,077 bytes
11 source files, building
running: phpize
Configuring for:
PHP Api Version:         20131106
Zend Module Api No:      20131226
Zend Extension Api No:   220131226
building in /tmp/pear/temp/pear-build-defaultuserFBmoKa/xhprof-0.9.4
running: /tmp/pear/temp/xhprof/extension/configure --with-php-config=/usr/local/bin/php-config
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for a sed that does not truncate output... /bin/sed
expr: syntax error
sh: 0: unknown operand
expr: syntax error
sh: 0: unknown operand
expr: syntax error
sh: 0: unknown operand
checking for cc... cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether cc accepts -g... yes
checking for cc option to accept ISO C89... none needed
checking how to run the C preprocessor... cc -E
checking for icc... no
checking for suncc... no
checking whether cc understands -c and -o together... yes
checking for system library directory... lib
checking if compiler supports -R... no
checking if compiler supports -Wl,-rpath,... yes
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
checking for PHP prefix... /usr/local
checking for PHP includes... -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib
checking for PHP extension directory... /usr/local/lib/php/extensions/no-debug-non-zts-20131226
checking for PHP installed headers prefix... /usr/local/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for re2c... re2c
checking for re2c version... 0.14.3 (ok)
checking for gawk... no
checking for nawk... no
checking for awk... awk
checking if awk is broken... no
checking whether to enable xhprof support... yes, shared
checking for ld used by cc... /usr/x86_64-alpine-linux-musl/bin/ld
checking if the linker (/usr/x86_64-alpine-linux-musl/bin/ld) is GNU ld... yes
checking for /usr/x86_64-alpine-linux-musl/bin/ld option to reload object files... -r
checking for BSD-compatible nm... /usr/bin/nm -B
checking whether ln -s works... yes
checking how to recognize dependent libraries... pass_all
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking dlfcn.h usability... yes
checking dlfcn.h presence... yes
checking for dlfcn.h... yes
checking the maximum length of command line arguments... 98304
checking command to parse /usr/bin/nm -B output from cc object... ok
checking for objdir... .libs
checking for ar... ar
checking for ranlib... ranlib
checking for strip... strip
checking if cc supports -fno-rtti -fno-exceptions... no
checking for cc option to produce PIC... -fPIC
checking if cc PIC flag -fPIC works... yes
checking if cc static flag -static works... yes
checking if cc supports -c -o file.o... yes
checking whether the cc linker (/usr/x86_64-alpine-linux-musl/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no

creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h
running: make
/bin/sh /tmp/pear/temp/pear-build-defaultuserFBmoKa/xhprof-0.9.4/libtool --mode=compile cc  -I. -I/tmp/pear/temp/xhprof/extension -DPHP_ATOM_INC -I/tmp/pear/temp/pear-build-defaultuserFBmoKa/xhprof-0.9.4/include -I/tmp/pear/temp/pear-build-defaultuserFBmoKa/xhprof-0.9.4/main -I/tmp/pear/temp/xhprof/extension -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib  -DHAVE_CONFIG_H  -g -O2   -c /tmp/pear/temp/xhprof/extension/xhprof.c -o xhprof.lo
mkdir .libs
 cc -I. -I/tmp/pear/temp/xhprof/extension -DPHP_ATOM_INC -I/tmp/pear/temp/pear-build-defaultuserFBmoKa/xhprof-0.9.4/include -I/tmp/pear/temp/pear-build-defaultuserFBmoKa/xhprof-0.9.4/main -I/tmp/pear/temp/xhprof/extension -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /tmp/pear/temp/xhprof/extension/xhprof.c  -fPIC -DPIC -o .libs/xhprof.o
/tmp/pear/temp/xhprof/extension/xhprof.c:825:10: warning: 'hp_globals' is static but used in inline function 'hp_ignore_entry' which is not static
   return hp_globals.ignored_function_names != NULL &&
          ^
/bin/sh /tmp/pear/temp/pear-build-defaultuserFBmoKa/xhprof-0.9.4/libtool --mode=link cc -DPHP_ATOM_INC -I/tmp/pear/temp/pear-build-defaultuserFBmoKa/xhprof-0.9.4/include -I/tmp/pear/temp/pear-build-defaultuserFBmoKa/xhprof-0.9.4/main -I/tmp/pear/temp/xhprof/extension -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib  -DHAVE_CONFIG_H  -g -O2   -o xhprof.la -export-dynamic -avoid-version -prefer-pic -module -rpath /tmp/pear/temp/pear-build-defaultuserFBmoKa/xhprof-0.9.4/modules  xhprof.lo
cc -shared  .libs/xhprof.o   -Wl,-soname -Wl,xhprof.so -o .libs/xhprof.so
creating xhprof.la
(cd .libs && rm -f xhprof.la && ln -s ../xhprof.la xhprof.la)
/bin/sh /tmp/pear/temp/pear-build-defaultuserFBmoKa/xhprof-0.9.4/libtool --mode=install cp ./xhprof.la /tmp/pear/temp/pear-build-defaultuserFBmoKa/xhprof-0.9.4/modules
cp ./.libs/xhprof.so /tmp/pear/temp/pear-build-defaultuserFBmoKa/xhprof-0.9.4/modules/xhprof.so
cp ./.libs/xhprof.lai /tmp/pear/temp/pear-build-defaultuserFBmoKa/xhprof-0.9.4/modules/xhprof.la
PATH="$PATH:/sbin" ldconfig -n /tmp/pear/temp/pear-build-defaultuserFBmoKa/xhprof-0.9.4/modules
----------------------------------------------------------------------
Libraries have been installed in:
   /tmp/pear/temp/pear-build-defaultuserFBmoKa/xhprof-0.9.4/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,--rpath -Wl,LIBDIR' linker flag

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

Build complete.
Don't forget to run 'make test'.

running: make INSTALL_ROOT="/tmp/pear/temp/pear-build-defaultuserFBmoKa/install-xhprof-0.9.4" install
Installing shared extensions:     /tmp/pear/temp/pear-build-defaultuserFBmoKa/install-xhprof-0.9.4/usr/local/lib/php/extensions/no-debug-non-zts-20131226/
running: find "/tmp/pear/temp/pear-build-defaultuserFBmoKa/install-xhprof-0.9.4" | xargs ls -dils
1576398      4 drwxr-xr-x    3 root     root          4096 Jul 15 10:28 /tmp/pear/temp/pear-build-defaultuserFBmoKa/install-xhprof-0.9.4
1576421      4 drwxr-xr-x    3 root     root          4096 Jul 15 10:28 /tmp/pear/temp/pear-build-defaultuserFBmoKa/install-xhprof-0.9.4/usr
1576422      4 drwxr-xr-x    3 root     root          4096 Jul 15 10:28 /tmp/pear/temp/pear-build-defaultuserFBmoKa/install-xhprof-0.9.4/usr/local
1576423      4 drwxr-xr-x    3 root     root          4096 Jul 15 10:28 /tmp/pear/temp/pear-build-defaultuserFBmoKa/install-xhprof-0.9.4/usr/local/lib
1576424      4 drwxr-xr-x    3 root     root          4096 Jul 15 10:28 /tmp/pear/temp/pear-build-defaultuserFBmoKa/install-xhprof-0.9.4/usr/local/lib/php
1576425      4 drwxr-xr-x    3 root     root          4096 Jul 15 10:28 /tmp/pear/temp/pear-build-defaultuserFBmoKa/install-xhprof-0.9.4/usr/local/lib/php/extensions
1576426      4 drwxr-xr-x    2 root     root          4096 Jul 15 10:28 /tmp/pear/temp/pear-build-defaultuserFBmoKa/install-xhprof-0.9.4/usr/local/lib/php/extensions/no-debug-non-zts-20131226
1576419     92 -rwxr-xr-x    1 root     root         91160 Jul 15 10:28 /tmp/pear/temp/pear-build-defaultuserFBmoKa/install-xhprof-0.9.4/usr/local/lib/php/extensions/no-debug-non-zts-20131226/xhprof.so

Build process completed successfully
Installing '/usr/local/lib/php/extensions/no-debug-non-zts-20131226/xhprof.so'
install ok: channel://pecl.php.net/xhprof-0.9.4
configuration option "php_ini" is not set to php.ini location
You should add "extension=xhprof.so" to php.ini

安装出错提示:Connection to `pecl.php.net:443‘ failed

提示最终安装路径为/usr/local/lib/php/extensions/no-debug-non-zts-20131226/xhprof.so
也可以通过命令查看安装目录

/var/www/html # php -i |grep extension_dir
extension_dir => /usr/local/lib/php/extensions/no-debug-non-zts-20131226 => /usr/local/lib/php/extensions/no-debug-non-zts-20131226
sqlite3.extension_dir => no value => no value

查看加载的ini文件

/var/www/html # php --ini
Configuration File (php.ini) Path: /usr/local/etc/php
Loaded Configuration File:         /usr/local/etc/php/php.ini
Scan for additional .ini files in: /usr/local/etc/php/conf.d
Additional .ini files parsed:      /usr/local/etc/php/conf.d/docker-php-ext-gd.ini,
/usr/local/etc/php/conf.d/docker-php-ext-mysqli.ini,
/usr/local/etc/php/conf.d/docker-php-ext-pdo_mysql.ini,
/usr/local/etc/php/conf.d/docker-php-ext-redis.ini,
/usr/local/etc/php/conf.d/docker-php-ext-shmop.ini,
/usr/local/etc/php/conf.d/docker-php-ext-zip.ini

编辑加载的文件

vi /usr/local/etc/php/php.ini

添加配置

extension=xhprof.so
xhprof.output_dir=”/data/xhprof/xhprof_data“

xhprof.output_dir string
储存 XHProf 运行数据的默认目录,用于接口 iXHProfRuns(即 XHProfRuns_Default 类)。

验证

/var/www/html # php --ri xhprof

xhprof

xhprof => 0.9.2
CPU num => 2

配置

安装完后/usr/local/lib/php/目录下会有两个目录 xhprof_lib,xhprof_html/ xhprof_lib为工具的依赖目录,xhprof_html为web展示的项目目录,把xhprof_html 配置在Nginx server下 可以通过web地址的形式的方式访问

cp -r /usr/local/lib/php/xhprof_html/ /data/xhprof/
cp -r /usr/local/lib/php/xhprof_lib/ /data/xhprof/

Nginx配置

server{
    server_name mydata.cn;
    listen 80;
    root /data;
    index       index.php;
    autoindex on;
    location / {
            try_files $uri $uri/ /index.php$is_args$args;
    }
    location ~ \.php$ {
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass php5-4-33-fpm:9000;
            try_files $uri =404;
    }
}

测试

编辑一个PHP文件测试

<?php
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

for ($i = 0; $i <= 1000; $i++) {
    $a = $i * $i;
}

$xhprof_data = xhprof_disable();

$XHPROF_ROOT = "./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 "<a href='http://mydata.cn/xhprof/xhprof_html/index.php?run={$run_id}&source=xhprof_testing'>goto</a>:http://mydata.cn/xhprof/xhprof_html/index.php?run={$run_id}&source=xhprof_testing\n";

运行php文件得到结果如下
在这里插入图片描述
goto
在这里插入图片描述

图形界面所需工具

apk add graphviz

不安装点击[View Full Callgraph]查看图形界面会提示

failed to execute cmd: " dot -Tpng". stderr: `sh: dot: not found 

在这里插入图片描述

问题记录

在运行过程中php-fpm老是退出重启

[23-Jul-2021 08:42:33] WARNING: [pool www] child 31 exited on signal 11 (SIGSEGV) after 235.186179 seconds from start
[23-Jul-2021 08:42:33] NOTICE: [pool www] child 35 started

解决方案参考
这是一个已知问题,请参阅xhprof 问题
#45和PHP 错误 #67997。

没有官方修复,该项目似乎处于非活动状态,4 年多没有发布。

然而,你可以通过调用变通办法,xhprof_enable()以XHPROF_FLAGS_NO_BUILTINS:

xhprof_enable(XHPROF_FLAGS_NO_BUILTINS);

xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

改为

xhprof_enable(XHPROF_FLAGS_NO_BUILTINS);

图像乱码

在这里插入图片描述

分析

文件格式

运行完测试文件PHP会根据扩展配置的xhprof.output_dir将分析结果生产到指定的目录,文件如下

/data/xhprof # ls xhprof_data/
60fa70ba8140e.xhprof_testing.xhprof

文件名称组成格式:
唯一值.类型.xhprof
文件名称可以由save_run函数改变

 public function save_run($xhprof_data, $type, $run_id = null)

run_id参数不指定,默认用uniqid函数生产一个基于微秒的唯一id

总体报告

根据goto链接来到结果页面,可以看到一个总体报告,里面统计了总的耗时、以及内存的使用情况

Overall Summary	
Total Incl. Wall Time (microsec):	218 microsecs //执行后总共用的时间 单位(微秒)
Total Incl. CPU (microsecs):	0 microsecs // 执行后CPU占用的时间(微秒)
Total Incl. MemUse (bytes):	2,144 bytes //执行后不包含内部函数所占内存占用的容量
Total Incl. PeakMemUse (bytes):	0 bytes //执行后包含内部函数占用内存的容量
Number of Function Calls:	2 //执行后调用方法(函数)的次数

功能分析

显示前100个功能:按时间排序
在这里插入图片描述

说明
Function Name函数名称
Calls函数调用次数
Calls%函数调用次数

版权声明:本文为qq_36738861原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。