What's the easiest way to profile a PHP script?
I'd love tacking something on that shows me a dump of all function calls and how long they took but I'm also OK with putting something around specific functions.
I tried experimenting with the microtime function:
$then = microtime();
myFunc();
$now = microtime();
echo sprintf("Elapsed: %f", $now-$then);
but that sometimes gives me negative results. Plus it's a lot of trouble to sprinkle that all over my code.
No extensions are needed, just use these two functions for simple profiling.
Here is an example, calling prof_flag() with a description at each checkpoint, and prof_print() at the end:
Output looks like this:
Start
0.004303
Connect to DB
0.003518
Perform query
0.000308
Retrieve data
0.000009
Close DB
0.000049
Done
The PECL APD extension is used as follows:
After, parse the generated file using
pprofp
.Example output:
Warning: the latest release of APD is dated 2004, the extension is no longer maintained and has various compability issues (see comments).
I would defiantly give BlackFire a try.
There is this virtualBox I've put together using puphpet, to test different php frameworks which coms with BlackFire, please feel free to fork and/or distribute if required :)
https://github.com/webit4me/PHPFrameworks
If subtracting microtimes gives you negative results, try using the function with the argument
true
(microtime(true)
). Withtrue
, the function returns a float instead of a string (as it does if it is called without arguments).For benchmarking, like in your example, I use the pear Benchmark package. You set markers for measuring. The class also provides a few presentation helpers, or you can process the data as you see fit.
I actually have it wrapped in another class with a __destruct method. When a script exits, the output is logged via log4php to syslog, so I have a lot of performance data to work from.
Cross posting my reference from SO Documentation beta which is going offline.
Profiling with XDebug
An extension to PHP called Xdebug is available to assist in profiling PHP applications, as well as runtime debugging. When running the profiler, the output is written to a file in a binary format called "cachegrind". Applications are available on each platform to analyze these files. No application code changes are necessary to perform this profiling.
To enable profiling, install the extension and adjust php.ini settings. Some Linux distributions come with standard packages (e.g. Ubuntu's
php-xdebug
package). In our example we will run the profile optionally based on a request parameter. This allows us to keep settings static and turn on the profiler only as needed.Next use a web client to make a request to your application's URL you wish to profile, e.g.
As the page processes it will write to a file with a name similar to
By default the number in the filename is the process id which wrote it. This is configurable with the
xdebug.profiler_output_name
setting.Note that it will write one file for each PHP request / process that is executed. So, for example, if you wish to analyze a form post, one profile will be written for the GET request to display the HTML form. The XDEBUG_PROFILE parameter will need to be passed into the subsequent POST request to analyze the second request which processes the form. Therefore when profiling it is sometimes easier to run curl to POST a form directly.
Analyzing the Output
Once written the profile cache can be read by an application such as KCachegrind or Webgrind. PHPStorm, a popular PHP IDE, can also display this profiling data.
KCachegrind, for example, will display information including:
What to Look For
Obviously performance tuning is very specific to each application's use cases. In general it's good to look for:
Note: Xdebug, and in particular its profiling features, are very resource intensive and slow down PHP execution. It is recommended to not run these in a production server environment.