PHP performance analysis article 1: Xhprof & amp; Xhgui introduction note: This is the first article in our PHP application performance analysis series. read Article 2 to learn more about xhgui, article 3 focuses on performance tuning practices.
What is performance analysis?
Performance analysis is to measure the relative performance of an application at the code level. Performance analysis captures the following events: CPU usage, memory usage, function call length and times, and call diagram. Performance analysis also affects application performance.
General causes of performance degradation
Some of the general causes of performance degradation are quite unexpected. Even in advanced languages like PHP, the quality of code is rarely the root cause of the problem. In today's hardware configuration conditions, CPU is rarely the cause of performance restrictions. The common cause is:
Data storage
PostgreSQL
MySQL
Oracle
MSSQL
MongoDB
Riak
Cassandra
Memcache
CouchDB
Redis
External resources
APIs
File system
Network interface
External process
Bad Code
Which performance analyzer is selected?
In the PHP world, there are two completely different performance analyzers ?? Active and passive.
Active VS passive performance analysis
Active analyzer is used during development and is enabled by developers. The active analyzer collects more information than the passive analyzer, which has a greater impact on performance. Generally, the active analyzer cannot be used in the production environment. Xdebug is an active analyzer.
Because the active analyzer cannot be used in the production environment, Facebook launched a passive analyzer ?? XHprof. XHprof is created for use in the production environment. It has minimal impact on performance and collects enough information to diagnose performance problems. Both XHprof and OneAPM are passive analyzer.
Generally, the additional information collected by Xdebug is not necessary for analyzing general performance problems. This means that the passive analyzer is a better choice for uninterrupted performance analysis, even in the development environment.
Xhprof + Xhgui
Xhprof, developed by Facebook, contains a basic user interface for viewing performance data. In addition, Paul Reinheim developed Xhgui and an enhanced user interface (UI) for viewing, comparing, and analyzing performance data.
Install and install XHPROF
Xhprof can be installed through PECL. The steps are as follows:
$pecl install xhprof-beta
The pecl command will automatically update your php. ini settings. To update the pecl file, run the following command:
$ pecl config-getphp_ini
It will add a new configuration line at the top of the specified file (if any. You may want to move them to a more appropriate location.
Once you compile the extension, you must enable it. To do this, you need to add the following code in the php ini file:
[xhprof]extension=xhprof.so
Then, with Xhgui, you can easily perform performance analysis and check.
Install XHGUI
To install Xhgui, you must obtain it directly from git. This project can be found on github at https://github.com/perftools/xhgui
Xhgui requirements:
First, clone the project to any location. In Debian-based Linux systems (such as Ubuntu), it may be/var/www. In Mac OS X, it may be/Library/WebServer/Documents.
$cd /var/www$ git clone https://github.com/perftools/xhgui.git$ cd xhgui$ php install.php
The Last Command is to run composer to install dependencies and check the xhgui cache directory permissions. If it fails, you can manually run composer install.
Next, you may need to create a configuration file. This step is easy to implement. you can use the default configuration file in/path/to/xhgui/config. default. php.
If you are running mongodb locally without authentication, you may not need to do so. Because it will be rolled back to the default value. In a multi-server environment, you need a remote mongodb server that can be stored on all servers for proper configuration.
To improve MongoDB performance, run the following command to add an index:
$ mongo
use xhprof
Other configurations
If you do not want to install mongo in the production environment, or you cannot allow the Web server to access the mongo server, you can save the performance analysis data on the disk and import it to the local MongoDB for later analysis.
To do this, make the following changes in config. php:
'/path/to/xhgui/xhprof-' .uniqid("", true). '.dat',?>
Change the save. handler in the file, unannotate save. handler. filename, and assign it an appropriate value.
Note: By default, only one analysis file is saved every day.
Once the analysis data is ready, you can use the script that comes with xhgui to import it:
$ php /path/to/xhgui/external/import.php /path/to/file.dat
The subsequent steps are the same.
Run Xhgui
Xhgui is a PHP-based Web application. you can use/path/to/xhgui/webroot as the root file to set up a standard VM.
Alternatively, you can simply use PHP 5.4 + cli-server for example:
$cd/path/to/xhgui$ php -S0:8080-t webroot/
This enables Xhgui to communicate with all network interfaces through port 8080.
Performance Analyzer
When running the analyzer, you need to include the external/header. php script on all pages to be analyzed. Therefore, you can set auto_prepend_file in the PHP ini file. You can either set it directly in the public INI file or restrict it to a single virtual host.
For the Apache server, add the following code:
php_admin_value auto_prepend_file "/path/to/xhgui/external/header.php"
For Nginx servers, add the following code in server configuration:
fastcgi_param PHP_VALUE "auto_prepend_file=/path/to/xhgui/external/header.php";
If you use PHP 5.4 + cli-server (PHP-S ),RequiredSet through the command line flag:
$ php -S 0:8080 -dauto_prepend_file=/path/to/xhgui/external/header.php
By default, Analyzer only analyzes (about) 1% of requests at runtime. This is controlled by the following external/header. php code:
If you want to analyze every request (for example, in the development stage), you can comment out this code. If you want to analyze 10% of requests, you can make the following changes:
This allows you to analyze a small number of user requests, but does not affect a single user or too many users.
If you want to manually control the performance during performance analysis, you can do this:
This code checks a random GET/POST/COOKIE variable (A9v3XUsnKX3aEiNsUDZzV in this example) and creates a Cookie with the same name to analyze the entire request process. for example: redirection after form submission, Ajax request, and so on.
In addition, it allows a GET/POST variable named no-A9v3XUsnKX3aEiNsUDZzV to delete the cookie and stop the analysis.
Of course, we welcome you to try OneAPM for free performance analysis for your PHP and Java applications. OneAPM's exclusive probe can go deep into all PHP and Java applications to complete application performance management and monitoring, including visibility of code-level performance problems, fast identification and tracing of performance bottlenecks, Real User Experience Monitoring, server monitoring, and end-to-end application performance management. OneAPM can trace to SQL statement Traces records with poor performance, third-party APIs with poor performance, Web services, and Cache.
In the next article, we will go deep into Xhgui and the user interface for displaying and comparing xhprof data.