In: , , , , ,
On: 2006 / 04 / 14 Viewed: 96875 times
Shorter URL for this post: http://ozh.in/cm

I've started to play a bit with Xdebug, a PHP extension that's designed to provide profiling information for PHP scripts and script execution analysis, amongst other things. This is fun, this is interesting, and this is invaluable when you really want to optimize the last bits of a script or a function.

My test install is running XAMPP on Windows, and activation is dead easy since the precompiled php_xdebug.dll is bundled with the PHP distribution. Installation on any other system shouldn't be really harder anyway, assuming you have access to the php.ini file and can add some extensions to PHP

With XAMPP for Windows, you just have to add the following to your php.ini (usually located in xampp/apache/bin)

CODE:
  1. zend_extension_ts = "C:\path\to\xampp\php\extensions\php_xdebug.dll"

Then add something like the following section. I haven't really tuned this yet, these are mostly default and "do everything you can do" options.

CODE:
  1. [xdebug]
  2. xdebug.auto_trace = 1
  3. xdebug.collect_includes = 1
  4. xdebug.collect_params = 1
  5. xdebug.collect_return = 1
  6. xdebug.default_enable = 1
  7. xdebug.extended_info = 1
  8. xdebug.show_local_vars = 0
  9. xdebug.show_mem_delta = 1
  10. xdebug.trace_format = 1
  11. xdebug.trace_options = 0
  12. xdebug.trace_output_dir ="C:\path\to\xampp\tmp"
  13. ; Remote
  14. xdebug.remote_enable=1
  15. xdebug.remote_mode="req"
  16. xdebug.remote_host=127.0.0.1
  17. xdebug.remote_port=17869
  18. xdebug.idekey=<idekey>
  19.  
  20. ; Mode 1
  21. xdebug.remote_handler="gdb"
  22. xdebug.auto_profile = 1
  23. xdebug.auto_profile_mode = 2
  24. xdebug.output_dir = "C:\path\to\xampp\tmp"
  25.  
  26. ; Mode 2
  27. xdebug.remote_handler="DBGp"
  28. xdebug.profiler_enable = 1
  29. xdebug.profiler_output_dir = "C:\path\to\xampp\tmp"
  30. xdebug.profiler_output_name = "debug.out"
  31.  
  32. xdebug.dump.SERVER = REMOTE_ADDR,REQUEST_METHOD

Now, don't forget to restart or reload the Apache so changes can apply. Result of this ? Whenever you run a PHP script, a trace file is created in the tmp directory, containing informations about how many times a function was called, how many milliseconds it took, and such.

The file itself is not very human readable, and on Windows you need WinCacheGrind, a freeware viewer for files generated by xdebug.

For instance, check this screenshot for the kind of results that a front page call on WordPress produces. It makes spotting suspiciously CPU greedy functions a trivial task, can help you understand the code flow and can give hints on how optimize your code.

Related posts

Shorter URL

Want to share or tweet this post? Please use this short URL: http://ozh.in/cm

Metastuff

This entry "Profiling PHP Scripts with Xdebug" was posted on 14/04/2006 at 11:10 pm and is tagged with , , , , ,
Watch this discussion : Comments RSS 2.0.

7 Blablas

  1. 1
    Mike Smullin United States »
    wrote, on 14/May/06 at 12:17 am # :

    This is just what I was looking for! Thanks.

    They should post something like this on apachefriends.org AND xdebug.org.

  2. 2
    dyson India »
    replied, on 30/Aug/06 at 2:36 pm # :

    i ve installed xampp , now i need to know how to start using php and mysql , where do i need to store the php code typed on any editor. i have had a hard time figuring out what to do ,

  3. 3
    kodi Republic of Serbia »
    said, on 06/Jan/07 at 10:29 am # :

    I wrote similar article few months ago, you can read it here:
    http://hosaka.blogspot.com/2006/12/profiling-php-with-xdebug-and.html

  4. 4
    Forummekan.org Turkey »
    said, on 28/Jul/08 at 11:34 pm # :

    This is just what I was looking for! Thanks.

  5. 5
    MegaS Russia »
    thought, on 02/Aug/08 at 9:09 pm # :

    Nice article! It is still useful (i was looking for xdebug options list) for me even after 2 years:)

  6. 6
    Alan S United Kingdom »
    thought, on 12/Aug/10 at 5:10 pm # :

    Nice post, great to see something about xdebug written in plain English not pure jargon! Particular thanks for the full php.ini

    One question: what's your reason for using the port setting xdebug.remote_port=17869 over the xdebug default port 9000? Is this a XAMPP thing?

    I'm trying to figure out if this might have anything to do with the problems I'm having getting it to work.

  7. 7
    Ozh France »
    commented, on 12/Aug/10 at 5:16 pm # :

    Alan S » yeah, probably the default in XAMPP, I don't think I would have bothered changing that

Leave a Reply

Comment Guidelines or Die

  • HTML: You can use these tags: <a href=""> <em> <i> <b> <strong> <blockquote>
  • Posting code: Post raw code (no <> &lt; etc) within appropriate tags : [php][/php], [css][/css], [html][/html], [js][/js], [sql][/sql], [xml][/xml], or generic [code][code]
  • Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar.
  • Spam: Various spam plugins on patrol. I'll put pins in a Voodoo doll if you spam me.
  • I will mark as Spam test comments, all comments with SEO names (ie "My Cool Online Shop" instead of "Joe") or containing forum-like signatures.

Read more ?

Close
E-mail It