views:

1658

answers:

9

I'd like to find a way to determine how long each function in PHP, and each file in PHP is taking to run. I've got an old legacy PHP application that I'm trying to find the "rough spots" in and so I'd like to locate which routines and pages are taking a very long time to load, objectively.

Are there any pre-made tools that allow for this, or am I stuck using microtime, and building my own profiling framework?

+4  A: 

take a look into xdebug, which allows in-depth profiling. And here's an explanation of how to use xdebug.

Xdebug's Profiler is a powerful 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. The profiler in Xdebug 2 outputs profiling information in the form of a cachegrind compatible file.

Kudos to SchizoDuckie for mentioning Webgrind. This is the first I've heard of it. Very useful (+1).

Otherwise, you can use kcachegrind on linux or its lesser derivative wincachegrind. Both of those apps will read xdebug's profiler output files and summarize them for your viewing pleasure.

enobrev
+1  A: 

If you install the xdebug extension you can set it up to export run profiles, that you can read in WinCacheGrind (on Windows). I can't recall the name of the app that reads the files on Linux.

mabwi
+2  A: 

I once saw a screen-cast for Zend Core. Looks pretty good, but it actually costs money, I don't know if that's an issue for you.

jakemcgraw
+1  A: 

xdebug's profiling functions are pretty good. If you get it to save the output in valgrind-format, you can then use something like KCachegrind or Wincachegrind to view the call-graph and, if you're a visual kind of person, work out more easily what's happening.

Greg
A: 

XDebug is nice but its not that easy to use or setup IMO.

The profiler built into Zend Studio is very easy to use. You just hit a button on a browser toolbar and BAM you have your code profile. ts perhaps not as indepth as a CacheGrind dump, but its always been good enough for me.

You do need to setup Zend Platform too, but thats straightforward and free for development use - you'd still have to pay for the Zend Studio licence though.

Marc Gear
+20  A: 

I have actually done some optimisation work last week. XDebug is indeed the way to go.

Just enable it as an extension (for some reason it wouldn't work with ze_extension on my windows machine) , setup your php.ini with xdebug.profiler_enable_trigger=On and call your normal urls with XDEBUG_PROFILE=1 as either a get or a post variable to profile that very request. There's nothing easier!

Also, i can really reccommend webgrind , a webbased (php) google Summer Of Code project that can read and parse your debug output files!

SchizoDuckie
A: 

The easiest solution is to use Zend Profiler, you don't need Zend Platform to use is, you can run it directly from your browser, it's quite accurate and has the most features you need and it's integrated in the Zend Studio

andy.gurin
+1  A: 

In addition to having seriously powerful real-time debugging capabilities, PhpED from NuSphere (www.nusphere.com) has a built-in profiler that can be run with a single click from inside the IDE.

A: 

Here is a nice tip.

When you use XDebug to profile your PHP, set up the profiler_trigger and use this in a bookmarklet to trigger the XDebug profiler ;)

javascript:if(document.URL.indexOf('XDEBUG_PROFILE')<1){var%20sep=document.URL.indexOf('?');sep%20=%20(sep<1)?'?':'&';window.location.href=document.URL+sep+'XDEBUG_PROFILE';}
petsagouris