Finding time consuming PHP scripts


I’m hitting some server limitations and I want to find out which scripts use the most processing time/memory.

I have something similar for mysql with Jet Profiler, as that lists the slowest queries, or those that take most of the time because they are run often.

Is there anything script that can provide or accumulate such metrics?
Or how do you guys handle site-wide performance questions?

Thank you

I don’t think there is a way to do that…

Profiling…look at xDebug

I use xdebug for some development, but I believe it’s only see see performance in a script, not over a server running multiple scripts.

Anyway, I decided to fix something basic, now writing to file each request uri along with the parsetime. I’ll do some excel processing after I have a bit of data

Depending on the version of php your using:

  • memory_get_peak_usage() can be used to get the peak memory consumption of a page.
  • php function gentime() { static $a; if($a == 0) $a = microtime(true); else return (string)(microtime(true)-$a); }
    calling that function will give you time elapsed, called once to start the timing and once to get the time elapsed, you could use it to time the execution of a query and log it to a file.
  • Queries run can be monitored by adding 1 each time your database query function is used, to a global variable (the globals super-global array) and echoing and resetting when the page is generated
  • There may be a way to display all current objects, offhand I don’t know of one

Examine where your using multiple queries to do something, in some cases can you get the same information with a single query. Has your database tables been optimized?

Din’t know this existed, Thanks logic_earth

I use XDEBUG alot. It has a neat setting which allows you to just add extra param to query string, for example index.php?XDEBUG_PROFILE=1
and it will generate profile dump file in a directory you define in php.ini.
That dump file can be viewed with a program called WinCacheGrind on Windows. You can Google it. It shows alot of details of which class/function executed how many times and how log it took for each function to run. This is a great way to find areas of your script that taking too long and should be improved.

If you are concerned about slow running scripts have you considered that this may be caused by slow queries? Look at the mysql slow queries log and related settings.

Otherwise, xDebug, CacheGrind files as previously said.