PHP Performance Profiling with XHProf

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

XHProf is a PHP profiler originally written by Facebook, to provide a more lightweight alternative to XDebug.

After installing the xhprof PHP module, profiling can be enabled / disabled from PHP code:

xhprof_enable();
doSlowOperation();
$profile_data = xhprof_disable();

The returned array will contain data about the number of calls, CPU time and memory usage of each function that has been accessed inside doSlowOperation().

xhprof_sample_enable()/xhprof_sample_disable() can be used as a more lightweight option that will only log profiling information for a fraction of requests (and in a different format).

XHProf has some (mostly undocumented) helper functions to display the data (see example), or you can use other tools to visualize it (the platform.sh blog has an example).



Got any PHP Question?