Simple Profiler


/ Published in: PHP
Save to your folder(s)

Rather simple profiler for > php5, if you want to find out where the heck those 0.5 sec function time goes into


Copy this code and paste it in your HTML
  1. <?php
  2. // simple profiler class
  3. // by Patrik Plihal
  4.  
  5. class myProfiler{
  6. var $timestamps = array();
  7. var $cnt = 0; // in fact count(%timestamps)+1
  8. var $style = '<style type="text/css">.profiler{width: 420px; font-size: 11px; border: solid 1px #45A} .tar{text-align:right;} th{background-color: #D9D9D9 !important} .names{width: 150px;} .time1{width: 120px;background-color: #AAD} .time2{width: 120px;background-color: #DDA}</style>';
  9. var $factor;
  10. var $unit;
  11.  
  12. function __construct($unit="s",$lazy = false){
  13. if(!$lazy)
  14. $this->capture('start');
  15. $this->unit = $unit;
  16. switch($unit){
  17. case "ms":
  18. $this->factor = 1000;
  19. break;
  20. case "µs":
  21. $this->factor = 1000000;
  22. break;
  23. default:
  24. $this->factor = 1;
  25. break;
  26. }
  27. }
  28.  
  29. function capture($desc){
  30. $this->timestamps []= new myTimeStamp((empty($desc) ? $this->cnt : $desc));
  31. $this->cnt++;
  32. }
  33.  
  34. function getReport(){
  35. if($this->cnt < 2) return "";
  36. $rep = "\n capturepoint | absolute relative\n";
  37. $profiletime = $this->timestamps[$this->cnt-1]->time - $this->timestamps[0]->time;
  38. $lasttime = $this->timestamps[0]->time;
  39. for($i = 1; $i < $this->cnt; $i++){
  40. $delta = $this->timestamps[$i]->time - $lasttime;
  41. $rep .= sprintf("[ %12s ] : %10s %3s %10s %%\n",$this->timestamps[$i]->name,round($delta*$this->factor,5),$this->unit,round(($delta/$profiletime),3)*100); //$delta,($delta/$wholetime))
  42. $lasttime = $this->timestamps[$i]->time;
  43. }
  44. $rep .= sprintf("[ %12s ] | %10s sec %10s %%\n","summary",round($profiletime,5),100); //$delta,($delta/$wholetime))
  45. return $rep."\n\n";
  46. }
  47.  
  48. function getHTMLReport(){
  49. if($this->cnt < 2) return "";
  50. $rep = $this->style."<div><table class=\"profiler\" border=0>\n";
  51. $rep .= "<thead><tr><th class=\"names tar\">capturepoint</th>\n<th class=\"time1 tar\">absolute</th><th class=\"time2 tar\">relative</th></tr></thead><tbody>\n";
  52. $profiletime = max($this->timestamps[$this->cnt-1]->time - $this->timestamps[0]->time,0.0000001);
  53. $lasttime = $this->timestamps[0]->time;
  54. for($i = 1; $i < $this->cnt; $i++){
  55. $delta = $this->timestamps[$i]->time - $lasttime;
  56. $rep .= "<tr><td class=\"names tar\">".$this->timestamps[$i]->name."</td><td class=\"time1 tar\">".round($delta*$this->factor,5)." {$this->unit}</td><td class=\"time2 tar\">".(round($delta/$profiletime,3)*100)." %</td></tr>\n";
  57. $lasttime = $this->timestamps[$i]->time;
  58. }
  59. $rep .= "</tbody><tfoot><tr><th class=\"names tar\">summary</th>\n<th class=\"time1 tar\">".round($profiletime,5)." sec</th><th class=\"time2 tar\">100 %</th></tr></tfoot>\n";
  60. return $rep."</table></div>\n";
  61. }
  62.  
  63. function test(){
  64. print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
  65. print '<html><head></head><body>';
  66. foreach(range(1,5) as $i){
  67. usleep(250000*$i);
  68. $this->capture(($i/((float)4)));
  69. }
  70. print $this->getHTMLReport();
  71. print '</body></html>';
  72. }
  73. }
  74.  
  75. class myTimeStamp{
  76. var $name;
  77. var $time;
  78.  
  79. function __construct($n,$t = false){
  80. $this->name = $n;
  81. $this->time = (empty($t) ? microtime(true) : $t);
  82. }
  83. }
  84. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.