Installation use and precautions of PHP performance analysis tool xhprof
- 2021-08-28 19:31:11
- OfStack
Preface
xhprof is an PHP performance monitoring tool open source by facebook, which occupies very little resources and can even be deployed in production environment.
It can be used in conjunction with graphviz, and can intuitively show the time-consuming code execution in the form of pictures.
The following mainly talks about the installation and use process
1. Installation
(1) Download and unzip
wget http://pecl.php.net/get/xhprof-0.9.4.tgz
tar zxvf xhprof-0.9.4.tgz
(2) Compile and run
cd xhprof-0.9.4/extension/
phpize // This statement compiles PHP Extended tools, mainly according to the system information to generate the corresponding configure Documents, 1 As stored in /usr/local/php/bin/ Directory
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
mkdir /tmp/xhprof
(3) Edit php. ini:
[xhprof]
extension = xhprof.so
xhprof.output_dir=/tmp/xhprof
xhprof. output_dir is the save path for the analysis build log
(4) Installing plug-ins
Finally, the array is returned, which means that it is installed. Never mind what the values mean, because the configuration of UI is below. It will be very intuitive!
yum -y install libjpeg freetype freetype-devel libjpeg-devel liberation-sans-fonts.noarch
Automatic installation
yum -y install graphviz
(5) Insert code
// Find the code you want to analyze, add it at the beginning of the code, start profiling The memory usage will be counted
xhprof_enable(XHPROF_FLAGS_MEMORY);
// Specific code
// Add at the end of the code
$xhprof_data = xhprof_disable(); // stop profiler, display raw xhprof data for the profiler run
include_once ("/usr/local/src/xhprof-0.9.4/xhprof_lib/utils/xhprof_lib.php"); # Please pay attention to setting up the site include_path Authority
include_once ("/usr/local/src/xhprof-0.9.4/xhprof_lib/utils/xhprof_runs.php");
$xhprof_runs = new \XHProfRuns_Default();
// Save the run under a namespace "xhprof_foo".
// **NOTE**:
// By default save_run() will automatically generate a unique
// run id for you. [You can override that behavior by passing
// a run id (optional arg) to the save_run() method instead.]
$xhprof_runs->save_run($xhprof_data, "xhprof_foo");
(6) View
Configure an accessible site for xhprof-0. 9.4/xhprof_html in (2), and use server built into php simply
cd xhprof-0.9.4/xhprof_html
php -S 0.0.0.0:8990
Then visit the ip + port to report.
2. Instructions for use
Note:
1. Before it is officially activated, 1 must confirm that it will not affect the normal data output. Confirm that the output content is the same, and then go online.
2. Do not set max_time of each url too small.
3. xhprof will affect the performance of online services, so it is best to monitor on only one machine, or modify xhprof. php code to randomly monitor requests.
Summarize