Original article: http://www.phpwebgo.com/2012/04/29/243.html
 
1. Install xhprof:
 
 
 
wget http://pecl.php.net/get/xhprof-0.9.2.tgz tar zxvf xhprof-0.9.2.tgz cd xhprof-0.9.2 mkdir /home/wwwroot/xhprof cp -r xhprof_html xhprof_lib /home/wwwroot/xhprof/ cd extension/ /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config make && make install vim /usr/local/php/etc/php.ini extension = "xhprof.so" xhprof.output_dir=/home/wwwlogs/xhprof  mkdir /home/wwwlogs/xhprof  chmod 777 -R /home/wwwlogs/xhprof/ /etc/init.d/php-fpm restart /etc/init.d/nginx restart
 
Ii. Install graphviz
 
 
wget http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.24.0.tar.gz  tar zxvf graphviz-2.24.0.tar.gz cd graphviz-2.24.0 ./configuremake && make install
 
Iii. How to Use
 
Xhprof_enable (); // added before the Statistical Code section. If you want to display CPU usage, you can add the xhprof_flags_cpu parameter. The memory is xhprof_flags_memory. If the two are used together: xhprof_flags_cpu + Memory: xhprof_enable (xhprof_flags_cpu + xhprof_flags_memory );
 
Xhprof_disable (); // Add
 
In this way, xhprof can calculate the function performance of the current page. Xhprof will generate a file in the specified directory for the performance statistics result of each page that is added to the Statistics Code. The file name is named as the System ID of this access. A new file is generated every time you refresh the page. Each System ID is different.
 
 
<? Phpxhprof_enable (xhprof_flags_cpu + xhprof_flags_memory); $ xhprof_on = true; $ OBJ = new getlocationip ('. /qqwry. dat '); For ($ I = 0; $ I <1000; $ I ++) {echo $ IP = rand (10,255 ). '. '. rand (1,255 ). '. '. rand (1,255 ). '. '. rand (1,255); $ address = $ obj-> Find ($ IP); $ STR = $ Address ['0']. $ Address ['1']; echo $ Str. '<HR>';} if ($ xhprof_on) {$ xhprof_data = xhprof_disable (); $ xhprof_root = dirname (_ file __). '/xhprof/'; include_onc E $ xhprof_root. "xhprof_lib/utils/xhprof_lib.php"; include_once $ xhprof_root. "xhprof_lib/utils/xhprof_runs.php"; $ xhprof_runs = new xhprofruns_default (); $ run_id = $ xhprof_runs-> save_run ($ xhprof_data, "HX "); echo '<a href = "http://xxx.xxx.xxx.xxx/xhprof/xhprof_html/index.php? Run = '. $ run_id.' & source = HX "target =" _ blank "> Statistics </a> ';} 
 
 
 
Terms:
1. Implicit Time: including all execution times of sub-functions.
2. Exclusive time/self time: the time consumed by the function execution, excluding the sub-tree execution time.
3. Wall time: the time spent or the wall clock.
4. CPU time: User time + Kernel Time
5. Balanced CPU: the CPU used by sub-functions.
6. Exclusive CPU: the CPU occupied by the function itself
 
 
 
Then, you can view the result through the HTTP access URL in the xhprof_html directory. For example, http: // APP/xhprof_html /? Run = run ID & source = namespace (the run ID and namespace can be determined based on the file name generated by xhprof)
 
 
In the production environment, you can enable it based on the number of users you have accessed.
 
 
Follow-up:
 
For example, the xhprof Profile Log is saved to the server as a file and needs to be cleared regularly. It is not convenient to view it one by one. Because the profile generated by xhprof is a large array, therefore, the standard PHP serialize is used for saving to the file. The log file is too large, and the disk space and serialize are inefficient (CPU is very high)
 
Some ideas: You can put the logs on a single log server or store them to the persistent storage of the MySQL database, which will be more convenient for future statistics :)