Posted By

lfatr on 02/16/10


Tagged


Versions (?)

PHP RUNTIME FOR EACH FUNCTION EXECUTED


 / Published in: PHP
 

  1. <?php
  2.  
  3. # DEBUG
  4. //error_reporting(0);
  5. $script_stats = array();
  6. $time = microtime(true);
  7.  
  8. function track_stats(){
  9. global $script_stats,$time;
  10. $trace = debug_backtrace();
  11. $exe_time = (microtime(true) - $time) * 1000;
  12. if ( $trace[1]["args"] ) {
  13. $func_args = implode(", ",$trace[1]["args"]);
  14. } else {
  15. $func_args = '';
  16. }
  17. $script_stats[] = array(
  18. "current_time" => microtime(true),
  19. "memory" => memory_get_usage(true),
  20. "file" => $trace[1]["file"].': '.$trace[1]["line"],
  21. "function" => '<strong>'.$trace[1]["function"].'</strong>'.'('.$func_args.')',
  22. "called_by" => $trace[2]["function"].' in '.$trace[2]["file"].': '.$trace[2]["line"],
  23. "ns" => $exe_time
  24. );
  25. $time = microtime(true);
  26. }
  27.  
  28. declare(ticks = 1);
  29. register_tick_function("track_stats");
  30.  
  31. # /DEBUG
  32. ?>
  33.  
  34. WEBSITE
  35.  
  36. <?php
  37. sleep(5);
  38. unregister_tick_function("track_stats");
  39.  
  40. echo '<table>';
  41. foreach ($script_stats as $counter => $line) {
  42. echo '<tr>';
  43. echo '<td>'.$counter.'</td>';
  44. echo '<td>'.$line['current_time'].'</td>';
  45. echo '<td>'.$line['memory'].'</td>';
  46. echo '<td>'.$line['file'].'</td>';
  47. echo '<td>'.$line['function'].'</td>';
  48. echo '<td>'.$line['called_by'].'</td>';
  49. echo '<td>'.$line['ns'].'</td>';
  50. echo '</tr>';
  51. }
  52. echo '</table>';
  53. ?>

Report this snippet  

You need to login to post a comment.