In the previous article, we showed you how to analyze PHP performance based on the xhprof extension, log in to the logs, and finally use the Xhprof extended UI to show up on the web. This article covers 2 points of knowledge:
- Use Xhgui instead of Xhprof's default UI interface for easier analysis
- Replacing xhprof extensions with tideways extensions
Use the more beautiful Ui:xhgui
Xhgui supports Xhprof, Uprofiler, or tideways extensions, that is, just one of these extensions is installed.
In this test, the actual use of the tideways extension (switch to xhprof extension after the web does not see the data, the reason is unknown. Switching to Uprofiler also has no data. )。
Xhprof Although from Facebook but has not been updated for a long time, the official source has shown this package is abandoned and no longer maintained (the packet is obsolete, no longer maintained). Tideways on the contrary, has been a commercial company in the maintenance, and actively support the PHP7. Two extensions are open source, in summary I suggest you choose Tideways to analyze the code.
To install the tideways extension:
wget https://github.com/tideways/php-xhprof-extension/archive/v4.1.5.tar.gz -O php-xhprof-extension-4.1.5.tar.gztarcdphpize ./configuremake&&make
Installing Xhgui
cd /work/git clone https://github.com/perftools/xhgui.git xhgui
If you need to install the Chinese language interface, you can:
git clone https://github.com/laynefyc/xhgui-branch.git xhgui
Then install Xhgui dependencies:
cd xhguiphp install.php
Wait a few minutes for the installation, please be patient.
Set permissions on the cache directory to allow Nginx to create files:
chmod -R 777
Xhgui has already written the injected entry file, which is located external/header.php
, without the need for us to manually write an injection file similar to the one in the previous article xhprof.inc.php
.
Installing MongoDB and clients
Xhgui writes the logs to MongoDB, so using Xhgui requires the MongoDB server to be installed. The installation and start of the MongoDB service-side process is omitted here.
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.ensureIndex'profile.main().cpu' : -1 } ) db.results.ensureIndex'meta.url' : 1 } )
Similarly, because Xhgui is written in PHP, you also need to read the data in MongoDB and need to install the MongoDB PHP client:
pecl install mongodb
Then add the configuration in the php.ini file:
[mongo]extension=mongo.so
To see if the extension was installed successfully:
php|grep mongo
Then restart the PHP-FPM service.
Configure Xhgui
The config directory for Xhgui has a config.default.php, copied to config.php
, if the MongoDB address is not the default, modify:
'db.host''mongodb://127.0.0.1:27017',
There are also changes to the sampling frequency, the default is 1/100, the test is changed to true:
'profiler.enable'function() { //return rand(1, 100) === 42; returntrue; },
Configure Project Injection
In the previous article, we introduced that the injected entry file can be written to php.ini or Nginx, I suggest to write in Nginx configuration, this will only affect the nginx corresponding project, not all the projects that use the PHP environment. The portal file uses Xhgui's own injected files:
Jifen.cc.conf
location ~ \.php$ { 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=/work/xhgui/external/header.php"; include fastcgi_params;}
Configure Xhgui Web
We modified xhprof.test.com.conf to:
server { listen 80; server_name xhprof.test.com; #root /work/xhprof/xhprof_html; root /work/xhgui/webroot/; index index.php index.html; location / { if (!-e $request_filename) { rewrite . /index.php last; } } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
Restart the Nginx service.
We request the interface of the application several times, open the browser input http://xhprof.test.com/, can see:
Click on a request to go to see the details:
Reference
1, PHP performance tracking and analysis tools Xhprof installation and use-Ma Sing's technical blog-Segmentfault think No
1190000007288664#articleheader4
2, Tideways and Xhgui build php non-intrusive monitoring platform | I'm a big bear.
http://blog.it2048.cn/article-tideways-xhgui/
Using XHPROF to analyze PHP performance bottlenecks (ii)