Tuesday, February 28, 2012

Xdebug Profiler Doc


Xdebug's Profiler

Xdebug's Profiler is a tool that gives you the ability to analyze your PHP code and determine bottlenecks or generally see which parts of your code are slow and could use a speed boost.

Installing Xdebug on windows:

Using WAMP:
1.       Go to the installation directory of WAMP
2.       Inside "bin/php/" you can see the actual PHP directory, named something similar to php5.2.8. The first two major versions are what is important to us, i.e., 5.2 in this case. Please make a note of this number.

Get a copy of XDebug library
1.       Click http://www.xdebug.org/ to go to XDebug homepage
2.       Once you're at XDebug homepage, click the link "Obtaining" on top and look for the release that matches with your PHP version. At the time of writing this tutorial, I have Xdebug 2.0.4 that works with my PHP version 5.2. You might see two variants, thread-safe and non-thread-safe. If thread-safe is available (usually it's not mentioned in the name if it's thread safe), download that one. I chose 5.2 VC6 (php_xdebug-2.0.4-5.2.8.dll) i.e. http://www.xdebug.org/files/php_xdebug-2.0.4-5.2.8.dll
3.       Save it to the "ext" directory inside your PHP directory. If you are not sure where "ext" comes, go to the PHP directory as per the steps mentioned above, and look for the "ext" directory inside it.

Or Download the latest version of wamp it already has the debug.

After following above steps or installing the new version of wamp, you need to configure it.
Just open the php.ini. If you have installed new version of wamp then you have already some configuration related to xdeug at the end of php.ini. Just comment those & add the following lines. For older version of wamp, you also need to add the followings lines at the end of php.ini

zend_extension = "c:/wamp/bin/php/php5.3.5/zend_ext/php_xdebug-2.1.0-5.3-vc6.dll"

[xdebug]
xdebug.remote_enable = on
xdebug.remote_handler = dbgp
xdebug.remote_host = localhost
xdebug.remote_port = 9001
xdebug.profiler_enable = on
xdebug.profiler_enable_trigger = off
xdebug.profiler_output_name = cachegrind.out.%t.%p
xdebug.profiler_output_dir = "c:/wamp/tmp"


Php.ini file preview



Please correct the path of zend_extesnion. & output dir. If you have installed the wamp in C drives then you don’t need to change the output dir path. Just change the zend_extension to correct path.
Now go the c:/wamp/tmp folder. Clear everything from there & run any page in the browser that you want to analyze. You will see a file in tmp folder something like cachegrind.out


Cachegrind generated file preview



Each request will generate the new file so you have to be aware which page you want to analyze & which files has generated for that request.
Now go to http://sourceforge.net/projects/wincachegrind/ & download the wincachegrind to analyze the page. After downloading the software just run file file.
Cachegrind Software Preview

Click on the file & click open. Select the cachegrind.out file to analyze.



After opening the file you will able so see the following screen.


Here you can see total file execution time, time taken by each function, included file execution time etc.

You can also search how many times a function has been called & how much time it has taken. If the below screen you can see I have search for get_temp & get_template_dir has been called 102 times & total run time is 5220milisec that means around 5sec. If you reduced the no fo calls then we can save 2-3 secs here.

No comments:

Post a Comment