We’ve been using PHP for a lot of CPU intensive / critical tasks. Whether it’s data crunching or responding to a request in 20 milliseconds this tools can help to find time consuming bottlenecks.

Take it easy, start with microtime

The simplest way to detect bottlenecks, is to measure the time it takes to execute a code block. microtime() returns the current Unix timestamp with microseconds. However the result depends of gettimeofday() call. If system time changes, result of this function can be unpredictable (much greater or less than zero).You aren’t going to get total microsecond accuracy.
Most systems keep their sub-second system clock with a resolution of something in the range of 1-33 milliseconds. It’s system dependent.

Global view with XHProf graphs

XHProf is a light-weight hierarchical and instrumentation based profiler. During the data collection phase, it keeps track of call counts and inclusive metrics for arcs in the dynamic callgraph of a program. It computes exclusive metrics in the reporting/post processing phase, such as wall (elapsed) time, CPU time and memory usage. A functions profile can be broken down by callers or callees. XHProf handles recursive functions by detecting cycles in the callgraph at data collection time itself and avoiding the cycles by giving unique depth qualified names for the recursive invocations.

XHProf includes a simple HTML based user interface (written in PHP). The browser based UI for viewing profiler results makes it easy to view results or to share results with peers.
A callgraph image view is also supported. XHProf reports can often be helpful in understanding the structure of the code being executed. The hierarchical nature of the reports can be used to determine, for example, what chain of calls led to a particular function getting called. XHProf supports ability to compare two runs (a.k.a. “diff” reports) or aggregate data from multiple runs. Diff and aggregate reports, much like single run reports, offer “flat” as well as “hierarchical” views of the profile.

http://www.php.net/manual/en/book.xhprof.php

Debugging and profiling with 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. This allows you to use the excellent KCacheGrind tool (Linux/Windows, KDE) to analyse your profiling data. If you are on Linux you can install KCacheGrind with your favourite package manager; if you are on Windows you can get precompiled binaries of KCacheGrind at SourceForge. Users of Windows can also use WinCacheGrind. There is also an alternative profile information presentation tool called xdebugtoolkit, a web based front-end called Webgrind and a Java based tool called XCallGraph.

http://www.xdebug.org/

Going low level with VLD and PHP library source code

The Vulcan Logic Disassembler hooks into the Zend Engine and dumps all the opcodes (execution units) of a script.This is useful if you have expressions that are executed millions of times, and the benchmark does’t make sense.Also taking a look at the extension source code may help, a example of that is the session extension that implements session locking at the extension level.

http://derickrethans.nl/projects.html#vld

Conclusion

Most of the bottlenecks can be easily solved using microtime. Profiling offers a good global overview of the application and VLD is necessary only on extreme situations.

PS: newrelic is also a pretty good tool