php 性能分析工具 xhprof

php 安装 xhprof 扩展 (兼容php5、7)

https://github.com/longxinH/xhprof

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

扩展编译完成,确定一下

ls /usr/local/php7/lib/php/extensions/no-debug-non-zts-20180731/xhprof.so

添加切片文件 vi /tmp/xhprof.prepend.php

<?php
xhprof_enable(XHPROF_FLAGS_NO_BUILTINS | XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY);
define('XHPROF_START_TIME', microtime(true));

register_shutdown_function(function () {
    $xhprof_time = microtime(true) - XHPROF_START_TIME;
    $xhprof_data = xhprof_disable();
    if ($xhprof_time > 2 && !empty($xhprof_data)) { // 采样大于两秒的请求
        if (!is_dir('/tmp/xhprof/')) {
            mkdir('/tmp/xhprof/', 0777, true);
        }
        $xhprof_file = '/tmp/xhprof/' . date('YmdHis0000') . uniqid() . '.'
            . str_replace('.', '_', strtolower((isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'NOHOST')
            . '-' . (isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'NOMETHOD')))
            . '.xhprof';
        file_put_contents($xhprof_file, serialize($xhprof_data));
    }
});

vi /usr/local/php7/lib/php.ini

[xhprof]
extension=xhprof.so 
xhprof.output_dir = /tmp/xhprof
xhprof.collect_additional_info = 1
auto_prepend_file ="/tmp/xhprof.prepend.php";

配置 xhprof.com 根目录为 xhprof 仓库

重启 apache or php-fpm

查看性能报告

访问 http://xhprof.com/xhprof_html/ 就可以看到性能报告拉 :)

查看性能报告中的 [View Full Callgraph],安装 graphviz 即可

yum install graphviz

发表新评论