DebugExecutionTime.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. class DebugExecutionTime {
  3. private $_log = array();
  4. private $_logByGroups = array();
  5. private $_isActive = false;
  6. private $_lastTime = 0;
  7. public function __construct() {
  8. $this->_log = array();
  9. }
  10. public function activate() {
  11. $this->_isActive = true;
  12. }
  13. public function log($msg, $groups = array()) {
  14. if (!$this->_isActive) return;
  15. $curTime = microtime(true);
  16. $execTime = (!$this->_lastTime)? 0 : $curTime - $this->_lastTime;
  17. $this->_lastTime = $curTime;
  18. $this->_log[] = array($msg, $execTime);
  19. foreach ($groups as $group) {
  20. if (!array_key_exists($group, $this->_logByGroups)) $this->_logByGroups[$group] = 0;
  21. $this->_logByGroups[$group] += $execTime;
  22. }
  23. }
  24. public function getLastExecTime() {
  25. if (!$this->_isActive) return null;
  26. $logTotal = count($this->_log);
  27. if ($logTotal < 2) return null;
  28. return $this->_log[$logTotal - 1][1];
  29. }
  30. public function getTotalExecTime() {
  31. if (!$this->_isActive) return null;
  32. $logTotal = count($this->_log);
  33. if ($logTotal < 2) return null;
  34. $sumExecTime = 0;
  35. foreach ($this->_log as $log) $sumExecTime += $log[1];
  36. return $sumExecTime;
  37. }
  38. public function printDebug() {
  39. if (!$this->_isActive) return;
  40. $sumExecTime = 0;
  41. foreach ($this->_log as $log) $sumExecTime += $log[1];
  42. ?>
  43. <table>
  44. <tr>
  45. <td style="vertical-align:top;">
  46. <div style="max-height:300px;overflow:auto">
  47. <table style="text-align:left;" border="1" cellspacing="0" cellpadding="1">
  48. <thead>
  49. <tr>
  50. <th>msg</th>
  51. <th>time</th>
  52. </tr>
  53. </thead>
  54. <tfoot>
  55. <th style="text-align:right">total:</th>
  56. <th style="font-family:monospace;text-align:right;font-weight:bold;"><?php echo number_format($sumExecTime, 6); ?></th>
  57. </tfoot>
  58. <tbody>
  59. <?php foreach ($this->_log as $log) : ?>
  60. <tr>
  61. <td><?php echo $log[0]; ?></td>
  62. <td style="font-family:monospace;text-align:right;"><?php echo number_format($log[1], 6); ?></td>
  63. </tr>
  64. <?php endforeach; ?>
  65. </tbody>
  66. </table>
  67. </div>
  68. </td>
  69. <td style="vertical-align:top;">
  70. <?php if (!empty($this->_logByGroups)) : ?>
  71. <table style="text-align:left;" border="1" cellspacing="0" cellpadding="1">
  72. <thead>
  73. <tr>
  74. <th>group</th>
  75. <th>time</th>
  76. <th>time left</th>
  77. <th>time %</th>
  78. </tr>
  79. </thead>
  80. <tbody>
  81. <?php foreach ($this->_logByGroups as $group => $execTime) : ?>
  82. <tr>
  83. <th><?php echo $group; ?></th>
  84. <td style="font-family:monospace;text-align:right;"><?php echo number_format($execTime, 6); ?></td>
  85. <td style="font-family:monospace;text-align:right;"><?php echo number_format($sumExecTime - $execTime, 6); ?></td>
  86. <td style="font-family:monospace;text-align:right;"><?php echo (!$execTime)? 0 : round($execTime * 100 / $sumExecTime); ?>%</td>
  87. </tr>
  88. <?php endforeach; ?>
  89. </tbody>
  90. </table>
  91. <?php endif; ?>
  92. </td>
  93. </tr>
  94. </table>
  95. <?php
  96. }
  97. }