PHP可视化性能分析工具Tideways和Xhgui
2018年的今天充斥着大数据(Big Data)、人工智能(Ai)、物联网(Iot)。连人都要被替代,我们难道在做性能分析的时候还一段一段增加断点,手打log进行调试么?
当然了,我们又希望可以对代码进行无侵入式还可以友好的看到我们的各项指标。
准备工作
- PHP7+
- MongodbServer
- php-mongodb
- php-mcrypt
- Composer
- Git
基础环境部署就不再这里进行叙述了。
在以上环境准备好之后开始安装xhgui和tideways。
安装步骤
1.安装xhgui
git clone https://github.com/maxincai/xhgui
cd xhgui
php install.php
更改xhgui配置文件 /xhgui/config/config.default.php
<?php
/**
* Default configuration for Xhgui
*/
return array(
'debug' => true,
'mode' => 'development',
/*
* support extension: uprofiler, tideways_xhprof, tideways, xhprof
* default: xhprof
*/
'extension' => 'tideways_xhprof',//如果是php7+则需要修正为这个
// 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' => 'mongodb://127.0.0.1:27017',
'db.db' => 'tideways_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' => 'Y-m-d 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,10) === 5;
return true;//rand(1, 100) === 42; //全量采样还是 没多少采多少~
},
'profiler.simple_url' => function($url) {
return preg_replace('/\=\d+/', '', $url);
},
//过滤的目录
'profiler.filter_path' => array(
//'/home/admin/www/xhgui/webroot','F:/phpPro'
)
);
xhgui为一个php应用,需要配置一个域名/ip供外网进行访问。(域名指向目录为/xhgui/webroot)
注意:需要给xhgui/cache 目录0777权限
2.为mongodb添加索引
可以根据各自需要合理调整
mongo
use xhprof#db名称 可以在xhgui中自己选择/xhgui/config/config.default.php中自己定义
db.results.ensureIndex( { 'meta.SERVER.REQUEST_TIME' : -1 } )
db.results.ensureIndex( { 'profile.main().wt' : -1 } )
db.results.ensureIndex( { 'profile.main().mu' : -1 } )
db.results.ensureIndex( { 'profile.main().cpu' : -1 } )
db.results.ensureIndex( { 'meta.url' : 1 } )
3.安装tideways
个人比较喜欢编译安装 Git地址:php-xhprof-extension.git
phpize
./configure
sudo make
sudo make install
#修改php.ini增加
[tideways]
extension=tideways_xhprof.so
php -m #看是否安装上了tideways_xhprof
用php脚本测试是否安装成功
<?php
tideways_xhprof_enable();
my_application();
file_put_contents(
sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid() . '.myapplication.xhprof',
serialize(tideways_xhprof_disable())
);






