php performance xhprof
PHP code performance plugin, (*1)
Get source code, (*2)
You can get the source code from website https://github.com/longxinH/xhprof, and install on centos 6.5(my machine)., (*3)
cd xhprof-master/extension/ sudo /usr/bin/phpize sudo ./configure --with-php-config=/usr/bin/php-config --enable-xhprof sudo make && make install
Load as php extentsion, (*4)
You will get the path of xhprof.so, for install /usr/lib64/php/modules/xhprof.so after command make install open the php.ini file by vim,, (*5)
vim /etc/php.ini extension=/usr/lib64/php/modules/xhprof.so xhprof.output_dir=/tmp/xhprof // this config is used to analyze the xhprof data sudo service php-fpm restart
you can use php -m | grep xhprof or phpinfo to check whether xhprof is install rightly., (*6)
Config the nginx so that you can visit the xhprof data by table and graph., (*7)
server { listen 13000; location / { root xhprof_html directory path; try_files $uri $uri/ /index.php?$query_string; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
Add xhprof.so and output destination file to php.ini, (*8)
extension=/usr/lib64/php/modules/xhprof.so xhprof.output_dir=/tmp/xhprof
Execute the command below, (*9)
composer require silly/package dev-master
then you will find the item in composer.json below require, (*10)
"silly/package": "dev-master"
Use the Xhprof class in your code, (*11)
use silly\package\Xhprof; $xf = new Xhprof(); $xh->xhprof_start(); ....your code.... $data = $xh->xhprof_end(); $host = 'xxxx/index.php?run='; //the path you visit xhprof_html directory $xf->xhprof_display($data, $host);
Then will print the url, for instance (http://your_host/index.php?run=5b0c1bf8a8875&source=xhprof), just open it in browser, it will show the data in tablelist and click the [View Full Callgraph] link, you will see the function call links. Just care about the links with yellow and red color and optimize them., (*12)
Something useful, (*13)
Maybe you have some problem like below., (*14)
Error: either we can not find profile data for run_id 4d7f0bd99a12f or the threshold 0.01 is too small or you do not have ‘dot’ image generation utility installed
that's because xhprof draw the png graph, you should upgrate the dot version, just install the graphviz., (*15)
sudo yum install -y graphviz
please make sure the version of graphviz is ok, mine is graphviz-2.26.0 and the graph can be show right., (*16)