1190000007288664 (original address)
For the local development environment, performance analysis xdebug
is sufficient, but if it is an online environment, the xdebug
consumption is large, the configuration is not flexible enough, so the online environment is recommended to use XHPROF for PHP performance tracking and analysis.
Let's briefly introduce the simple installation and use of xhprof today.
Installation of Xhprof
Download Xhprof, we are here to choose through the way git clone, of course you can also from http://pecl.php.net/package/x ... Download here.
cd /usr/local/src# 我自己汉化的版本git clone https://github.com/maxincai/xhgui.git# 你可以clone原版git clone https://github.com/phacility/xhprof.git
Attention:
php5.4 and above versions cannot be downloaded in pecl, not supported. Need to download hhttps://github.com/phacility/on GitHub ....
Another xhprof has not been updated for a long time, up to now does not support PHP7,PHP7 can try to use https://github.com/tideways/p ....
Installing Xhporof
cd xhprof/extension/usr/local/php5.6/bin/phpize./configure --with-php-config=/usr/local/php5.6/bin/php-config --enable-xhprofmakemake install
Finally, if a similar message appears, the compilation installation is successful
stalling shared extensions: /usr/local/php-5.6.14/lib/php/extensions/no-debug-non-zts-20131226/
Modify the configuration file /etc/php5.6.ini
and add the following configuration at the end
[xhprof]extension=xhprof.soxhprof.output_dir=/data/www/xhprof/output
Reboot php-fpm
after phpinfo
viewing, or at the command line by php -m | grep xhprof
checking whether the installation was successful.
Attention:
Need to create output_dirmkdir-p/data/www/xhprof/output
Simple usage of xhprof
Copy the downloaded xhprof to the Webroot directory, I'll take this /data/www/project-xhprof
as an example
mkdir /data/www/project-xhprofcp -R /usr/local/src/xhprof/* /data/www/project-xhprof/cd /data/www/project-xhprof
Add site Configuration in Nginx
server {Listen80; server_name Xhprof.dev; ROOT/DATA/WWW/PROJECT-XHPROF; index index.php index.html; access_log/var/log/nginx/xhprof.dev.log main; error_log/var/log/nginx/xhprof.dev.log.err DEBUG; rewrite_log on; location ~* \.php$ {include Fastcgi_params; fastcgi_param script_filename $document _root $fastcgi _script_name; fastcgi_pass unix:/var/run/php5.6-fpm.sock; fastcgi_index index.php;}
Restart Nginx, then use http://xhprof.dev/examples/sample.php
, can see some output, and prompt by access to Http://<xhprof-ui-address& View the results. Next visit http://xhprof.dev/xhprof_html/to see the saved results, listing the calls to all functions and the time spent.
Analyze the sample code sample.php
, the key code:
<?php// 开始分析xhprof_enable();// 运行一些函数foo();// 停止分析,得到分析数据$xhprof_data = xhprof_disable();
$xhprof_data
All the function call time and CPU memory consumption are recorded in the program running, the parameters which can be controlled by xhprof_enable
the parameter are recorded, and the parameters currently supported are:
Hprof_flags_no_builtins skips all built-in (internal) functions.
Adds CPU data to the performance data of the XHPROF_FLAGS_CPU output.
Adds memory data to the performance data of the xhprof_flags_memory output.
The subsequent processing has nothing to do with the xhprof extension, which is to write a storage class XHProfRuns_Default
, $xhprof_data
serialize and save to a directory, you can output the XHProfRuns_Default(__DIR__)
results to the current directory, if not specified will be read php.ini
in the configuration file xhprof.output_dir
, still not specified will be output to /tmp
.
xhprof_html/index.php
Organize and visualize the results of the records, which are listed in the default UI:
Funciton Name: Name of function
Calls: Number of calls
Incl. Wall time (MICROSEC): function runtime (including child functions)
iwall%: Function run time (including sub-function) percentage
Excl. Wall time (MICROSEC): function runtime (excluding child functions)
ewall%: Function run time (excluding child functions)
In the xhprof_html/index.php
can also see [View Full Callgraph]
links, click can draw a visualization of the performance analysis diagram, if you click on the error, may be a lack of dependency graphviz
,
Graphviz is a tool for drawing graphics that gives you a more intuitive view of performance bottlenecks.
http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.24.0.tar.gzcd graphviz-2.24.0./configuremake && make install
or install it directly using Yum
install -y libpngyum install -y graphviz
At this point, you can see an effect similar to the following
Elegant access to existing projects
With these introductions, we can actually integrate the xhprof into any of our existing projects, and most of the MVC frameworks now have a unique portal file that only needs to be injected with the XHPROF code at the beginning of the portal file:
<?php//开启xhprofxhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);//在程序结束后收集数据register_shutdown_function(function() { $xhprof_data = xhprof_disable(); //让数据收集程序在后台运行 if (function_exists(‘fastcgi_finish_request‘)) { fastcgi_finish_request(); } //保存xhprof数据 ...});
We can't add all the places we need to analyze, but it's unavoidable to modify the source code of the project, in fact, PHP itself provides a better way to inject, such as to save the above logic /data/www/xhprof/inject.php
, and then modify the configuration in the PHP configuration file auto_prepend_file
auto_prepend_file = /data/www/xhprof/inject.php
For Apache servers, add the following code:
php_admin_value auto_prepend_file "/data/www/xhprof/inject.php"
For Nginx server, add the following code to the server configuration:
fastcgi_param PHP_VALUE "auto_prepend_file=/data/www/xhprof/inject.php";
This allows all PHP request files to be automatically injected into /data/www/xhprof/inject.php
the file, which is less intrusive and enables site-based injection.
More beautiful data show Xhgui
After injecting the code we also need to implement the UI to save the XHPROF data and display the data, the existing two more popular two wheels are Xhprof.io and Xhgui two items are similar, but in a comprehensive comparison xhprof.io
of years of disrepair, but xhgui
also more active, the UI interface is relatively more beautiful , I chose xhgui
to show the data here.
Installing Xhgui
/data/www/project-xhguicd /data/www/project-xhguigit clone https://github.com/perftools/xhgui.git ./
chmod -R 777
Launch the mongodb
instance, if MongoDB does not use the default port and configuration, or has the use of security authentication, just need to modify config/config.php
the relevant configuration, we use the default configuration, it is not modified.
To improve MongoDB performance, you can run the following command to add an index:
$/usr/local/mongodb/bin/mongo> Use Xhprofdb. Results.ensureindex ({ "Meta.) SERVER. Request_time ':-1}) Db.results.ensureindex ({" Profile.main (). WT ':-1} ) Db.results.ensureindex ({" Profile.main (). Mu ':-1}) Db.results< Span class= "Hljs-selector-class" >.ensureindex ({ ' Profile.main (). CPU ':-1}" Db.results.ensureIndex ({ Span class= "hljs-string" > ' Meta.url ': 1})
wget http://pecl.php.net/get/mongodb-1.1.9.tgztar zxvf Mongodb-1.1.9.tgzcd mongodb-1.1< Span class= "Hljs-number" >.9/usr/local/php5.6/bin/phpize./configure--with-php-config=/usr/local/ Php5.6/bin/php-configmake && make Installvim Span class= "Hljs-meta-keyword" >/etc/php5.6.ini# added at the end of the file [MONGO] Extension=mongo.so# see if installation succeeded Php-m | grep mongo# Restart Php-fpm/etc/init.d/php5.6-fpm restart
install.php# 如果上面的命令执行报错,则执行下面的命令composer install
server {Listen80;server_name Xhgui.dev;Root/data/www/project-xhgui/webroot;Index index.php index.html;Access_log/var/log/nginx/xhgui.dev.log main;error_log/var/log/nginx/xhgui.dev.log.err DEBUG; rewrite_log on; location/{try_files $uri Span class= "hljs-variable" > $uri//index.php? $uri & $args;} location ~* \.php$ {include Fastcgi_params; fastcgi_param script_filename $document _root $fastcgi _script_name; fastcgi_pass unix:/var/run/php5.6-fpm.sock; fastcgi_index index.php;}
After the configuration is complete, restart Nginx, http://xhgui.dev/
then open, you can see a page similar to the following
Modify /etc/php5.6.ini
The auto_prepend_file
configuration to complete the Xhgui access
auto_prepend_file = /data/www/project-xhgui/external/header.php
Specific use of Xhgui recently run
You can also search by time range and the requested address
Can be sorted by execution time, CPU time, memory consumption
You can click on an address to see the latest trends in this address
A run-time detail
Run details of a function
One-run function call graph
One-run flame diagram
Compare multiple Requests
Add Observation function
PHP Performance Tracking and analysis tools Xhprof installation and use