Thursday, June 19, 2008

Do Trace - Debug ( Profiling ) of PHP script by xdebug & kcachegrind


Step 1 : Install XDebug

xdebug is an open source debugger available for PHP. xdebug can be used to display more information in error traces. It can also be used to collect detailed code coverage & profiling information.

Installation

You need to install following packages to prepare environment for installation of pecl module xdebug.

sudo apt-get -y install php-pear php5-dev build-essential

Now we install xdebug using pecl.

sudo pecl install xdebug

Above command will download, compile & install xdebug on your system.

Configuration

Open usr/local/lib/php.ini and append following line.

zend_extension=/usr/lib/php5/20060613/xdebug.so

Be careful about that number part in the path as it could be different for you. Now restart apache by issuing following command.

sudo apache2ctl restart

Now try to view the output of your phpinfo() function. If you find xdebug word in that then it means you have successfully installed xdebug.

Stacktrace

xdebug.collect_params accepts values from 1 to 4. Where 1 refers to less verbosity & 4 refers to maximum verbosity while displaying stack traces. The easiest way of setting this option is using function ini_set in PHP.

ini_set("xdebug.collect_params", )

Get to know more stack trace options of xdebug.

var_dump

Once after installing xdebug when ever you call var_dump you are actually calling xdebug version of var_dump instead of built in one. By default xdebug version of var_dump gives you friendly info on the variables you are trying to inspect.

See more options on controlling display of var_dump information.

Debugging

xdebug uses DBGp protocol to communicate with external debuggers. Vim already has a plugin to do that. So you can debug php scripts from within vim.

Download php debugger plugin for vim.

Step 2: Enable Proiling

You need to add following line to /usr/local/lib/php.ini to enable profiling of php scripts.

xdebug.profiler_enable=1
Now restart the apache server by issuing following command.
sudo apache2ctl restart

When ever you access a php page through apache, xdebug will create a file something like cachegrind.out.15093. xdebug by default uses /tmp directory to dump files which contain profiling information. You can change this target directory by using xdebug option xdebug.profiler_output_dir and you can change result file name by using the option xdebug.profiler_output_name.

See more xdebug profiling options.

Some times you don't want to profile all requests. xdebug provides a selective mechanism to trigger profiling of specific requests. In order to enable this option you have to add following configuration option to php.ini.
xdebug.profiler_enable_trigger=On

Now you can trigger profiling of specific requests by adding XDEBUG_PROFILE to the query part of the GET request.

Analyzing Profiling results

We need to use kcachegrind to analyse profile file results. We can install kcachegrind by issuing following command.

sudo apt-get -y install kcachegrind

Now open your profle result file in kcachegrind and you can visuall inspect which part of your script is eating cpu resources. Callmap & Callgrap provide easy to understand visualizations to find bottlenecks in your script.

6 comments:

Anonymous said...

Thank you so much!!polo shirt men'ssweate,cheap polo shirts cheap columbia jackets, lacoste sweater, ralph lauren polo shirts,ski clothing. Free Shipping, PayPal Payment. Enjoy your shopping

experience on mensclothingus.com。We have mens polo shirts.

Anonymous said...

http://www.seriousblogging.com/pumabasket
http://www.seriousblogging.com/pumabasket1
http://nikeshox.blog.shinobi.jp
http://pumabasket.blog.co.uk
http://fashionuggboots.arekao.jp
http://blog.goo.ne.jp/pumacat

Anonymous said...

I like your blog . They are really great. Ermunterung ++ .
some new style Air shoes is in fashion this year.Do you know Air Shoes is a best . another kinds of nike air rift is better . the Puma basket will make you feel very relaxed when you play basketball.If you want to buy the Cheap puma shoes shoes ,you can buy them online. Very high-caliber and cheap puma shoes as same as you buy from the authorized store..My younger sister's long hair is always chaotic. so i presented a hair straighteners to her for Christmas last year .she were very pleasantly surprised .

Anonymous said...

ralph lauren polo shirts
chaussure puma
puma CAT

ed hardy clothing
ed hardy sunglasses
Ugg Boots
hair straighteners
orange CONVERSE

Anonymous said...

Thank you for sharing such a nice article.
chaussures puma
puma speed cat
Nike Tn Chaussures
requin tn
nike shox
puma shoes
puma CAT
puma basket
puma speed
baskets puma
puma sport
puma femmes
puma shox r4 torch
nike air max requin
nike shox r3
shox rival r3
tn plus
chaussures shox
nike shox r4 torch
air max tn requin
nike tn femme
pas cher nike
tn chaussures
nike rift
nike shox nz
chaussures shox
nike shox rival
shox rival
chaussures requin
jeans online
cheap armani jeans
cheap G-star jeans

Unknown said...

He has in his office hours TEU,chi flat iron work schedule and social balance - juggling many students there.

If forced to cut short nap before go.chi flat irons Many students begin to fight, some Z, and often counting sheep a thing of the past.

"Sometimes cheap chi Straighteners I get only five hours sleep a night," said Goldin.