Profiling PHP Code

Simplest way to profile a PHP script

The PECL APD extension is used as follows:

<?php
apd_set_pprof_trace();

//rest of the script
?>

After, parse the generated file using pprofp.

Example output:

Trace for /home/dan/testapd.php
Total Elapsed Time = 0.00
Total System Time = 0.00
Total User Time = 0.00


Real User System secs/ cumm
%Time (excl/cumm) (excl/cumm) (excl/cumm) Calls call s/call Memory Usage Name
--------------------------------------------------------------------------------------
100.0 0.00 0.00 0.00 0.00 0.00 0.00 1 0.0000 0.0009 0 main
56.9 0.00 0.00 0.00 0.00 0.00 0.00 1 0.0005 0.0005 0 apd_set_pprof_trace
28.0 0.00 0.00 0.00 0.00 0.00 0.00 10 0.0000 0.0000 0 preg_replace
14.3 0.00 0.00 0.00 0.00 0.00 0.00 10 0.0000 0.0000 0 str_replace

Warning: the latest release of APD is dated 2004, the extension is no longer maintained and has various compability issues (see comments).

Profiling PHP code

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!

how to profile concreate function or segment of code in php?

Use xhprof. And you can write something like this:

if ($_SERVER['REMOTE_ADDR'] == 'your_ip') {
enable_profiler();
}
function_with_bottle_neck();

if ($_SERVER['REMOTE_ADDR'] == 'your_ip') {
disable_profiler();
}

How can I use a PHP code profiler for with Symfony CLI scripts?

Here's the final solution that I'm quite happy with.

I tried installing XDebug 2.1.1 via the command:

pecl install xdebug 

However, this was failing miserably with this kind of error:

Cannot find autoconf. Please check
your autoconf installation and the
$PHP_AUTOCONF environment variable.
Then, rerun this script. ERROR:
`phpize' failed

After a bunch of searching I came across someone else who had tried this on OS X. Fortunately they mentioned that installing Apple Xcode would provide the autoconf, phpize and other needed tools.

I got Xcode 3 and installed that, then re-tried the pecl install xdebug and it worked beautifully. After learning how to enable xdebug in my CLI instance of PHP and figuring out where the output files went, I then moved on to Webgrind. Within a couple minutes of looking at my first webgrind report, I was happily pinpointing the areas of my symfony task to optimize. It has been fabulous.

Also in my searching I happened across this promising page about how to use Xdebug for MEMORY profiling. I haven't yet attempted this but wanted to add it here in case it helps anyone else.


I received one helpful comment with a link to a relevant discussion and two partial answers which were both helpful. I gave each of those responders an upvote and indicated so.

I wish S.O. didn't seem to place such emphasis on accepting a single answer, but since it does, I'm writing a more complete answer and accepting it but made sure to give all the helpful responders upvotes. Thanks again.

Tool to profile php code

You'll want to install and configure Xdebug. It's sort of the de-facto standard PHP debugging and profiling tool.

WinCacheGrind can crunch the profiling output. It's a bit buggy, but it does the job.

How to find the line of PHP code which is using most CPU

I'd suggest xdebug and kcachegrind to profile and analyse the behaviour of your code.

Configure xdebug like this to enable profiling:

xdebug.profiler_enable = 1
xdebug.profiler_output_name = xdebug.out.%t
xdebug.profiler_output_dir = /tmp
xdebug.profiler_enable_trigger = 1

If you pass XDEBUG_PROFILE as POST or GET paramteter, xdebug will produce profiling data in the configured location.

Open these files with kcachegrind to drill into it.

Beware: Execution while profiling will take its time, and the files produced can get pretty big -- have an eye on diskusage.



Related Topics



Leave a reply



Submit