xhprof安装记录

选择一个工具分析PHP函数调用的资源耗用明细,以图表化的形式展现,方便优化代码。

安装xhprof

$ pecl install xhprof-beta

在php.ini引用的extension中添加extension=xhprof.so

GUI

这里选择了xhgui,它的原理是在需要测试性能的脚本前加上PHP的一段代码,将收集到的性能数据存储到文件或者mongodb等存储介质中去。

MongoDB

$ apt-get install mongodb


前端


cd /var/www

git clone https://github.com/perftools/xhgui.git

cd xhgui

php install.php
  • 如果不能以root身份运行,那么sudo -u www-data php install.php


  • 安装的时候出现
the requested PHP extension mongodb is missing from your system
问题是平台的拓展名为mongo.so,而composer检查的是mongodb.so,只要加上--ignore-platform-reqs


  • 如果composer问题不清楚,建议单独跑composer命令,加上-vvv打开调试模式

使用

如前面说的原理是在头部添加一段PHP代码,这里通过在Nginx里配置,或者PHP ini auto_prepend_file
在php.ini中添加auto_prepend_file
。

location ~ .php {

    include fastcgi_params;

    fastcgi_buffers 128 4k;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  PHP_VALUE "auto_prepend_file="/opt/htdocs/xhgui/external/header.php"";
}

配置

在config目录下添加config.php配置

<?php
return array(
    'debug' => false,
    'mode' => 'development',

    // Can be either mongodb or file.
    /*
    'save.handler' => 'file',
    'save.handler.filename' => dirname(__DIR__) . '/cache/' . 'xhgui.data.' . microtime(true) . '_' . substr(md5($url), 0, 6),
    */
    'save.handler' => 'mongodb',

    // Needed for file save handler. Beware of file locking. You can adujst this file path
    // to reduce locking problems (eg uniqid, time ...)
    //'save.handler.filename' => __DIR__.'/../data/xhgui_'.date('Ymd').'.dat',
    'db.host' => '127.0.0.1:27017',
    'db.db' => 'xhprof',

    // Allows you to pass additional options like replicaSet to MongoClient.
    // 'username', 'password' and 'db' (where the user is added)
    'db.options' => array(),
    'templates.path' => dirname(__DIR__) . '/src/templates',
    'date.format' => 'M jS H:i:s',
    'detail.count' => 6,
    'page.limit' => 25,

    // Profile 1 in 100 requests.
    // You can return true to profile every request.
    'profiler.enable' => function() {
        return rand(1, 100) === 3;
    },

    'profiler.simple_url' => function($url) {
        return preg_replace('/=d+/', '', $url);
    }

);
原文地址:https://www.cnblogs.com/pier2/p/5967759.html