Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Function execution time for debugging [duplicate]

Tags:

php

debugging

Possible Duplicate:
Simplest way to profile a PHP script

Sometimes my website loads slowly and I would like to trace which function causes it. (or if it is related to me or the webserver) How can I get execution times of each function that is being run?

Like;

<?php
function A() { }
function B() { }
function C() { }
A();
B();

Trace();
?>

Trace() would output: A ran in 0,1 miliseconds, B ran in 1,1 seconds, script completed. Is there any debugging feature like this in PHP?

Ps. I'm not speaking about applying microtime for each function.

like image 850
Aristona Avatar asked Nov 24 '25 10:11

Aristona


1 Answers

Use:

  • XHProf if you need to profile in production environments. It has a footprint, but it's usually affordable. It does not produce trace if the script ends unexpectedly though.

http://blog.tech4him.com/wp-content/uploads/xhprof1.png http://www.wiktik.com/blog/wp-content/uploads/2012/02/xhprof-callgraph_small.png

  • XDebug if you want to profile in development and testing environments. It's generally more reliable, but has a noticeable footprint; long running scripts will generate very large (100s MBs) files for instance, as opposed to XHProf.

http://blog.haohtml.com/wp-content/uploads/2010/03/win_xdebug_WinCacheGrind_4.jpg

Don't use:

  • APD, because even though it's recommended in this answer, it's very outdated and doesn't look like it's being developed anymore (last release in 2004 according to its PECL page)

PS: My personal recommendation: Play with them all if you have the time to do so, or just set up xdebug in your dev environment otherwise.

like image 128
Mahn Avatar answered Nov 26 '25 00:11

Mahn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!