xhprof安装记录

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

安装xhprof

1
$ pecl install xhprof-beta

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

GUI

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

MongoDB

1
$ apt-get install mongodb

前端

1
2
3
4
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。

1
2
3
4
5
6
7
8
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配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?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);
}

);

Comments

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×