DebugExecutionTime.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 printDebug() {
  25. if (!$this->_isActive) return;
  26. $sumExecTime = 0;
  27. foreach ($this->_log as $log) $sumExecTime += $log[1];
  28. ?>
  29. <table>
  30. <tr>
  31. <td style="vertical-align:top;">
  32. <div style="max-height:300px;overflow:auto">
  33. <table style="text-align:left;" border="1" cellspacing="0" cellpadding="1">
  34. <thead>
  35. <tr>
  36. <th>msg</th>
  37. <th>time</th>
  38. </tr>
  39. </thead>
  40. <tfoot>
  41. <th style="text-align:right">total:</th>
  42. <th style="font-family:monospace;text-align:right;font-weight:bold;"><?php echo number_format($sumExecTime, 6); ?></th>
  43. </tfoot>
  44. <tbody>
  45. <?php foreach ($this->_log as $log) : ?>
  46. <tr>
  47. <td><?php echo $log[0]; ?></td>
  48. <td style="font-family:monospace;text-align:right;"><?php echo number_format($log[1], 6); ?></td>
  49. </tr>
  50. <?php endforeach; ?>
  51. </tbody>
  52. </table>
  53. </div>
  54. </td>
  55. <td style="vertical-align:top;">
  56. <?php if (!empty($this->_logByGroups)) : ?>
  57. <table style="text-align:left;" border="1" cellspacing="0" cellpadding="1">
  58. <thead>
  59. <tr>
  60. <th>group</th>
  61. <th>time</th>
  62. <th>time left</th>
  63. <th>time %</th>
  64. </tr>
  65. </thead>
  66. <tbody>
  67. <?php foreach ($this->_logByGroups as $group => $execTime) : ?>
  68. <tr>
  69. <th><?php echo $group; ?></th>
  70. <td style="font-family:monospace;text-align:right;"><?php echo number_format($execTime, 6); ?></td>
  71. <td style="font-family:monospace;text-align:right;"><?php echo number_format($sumExecTime - $execTime, 6); ?></td>
  72. <td style="font-family:monospace;text-align:right;"><?php echo (!$execTime)? 0 : round($execTime * 100 / $sumExecTime); ?>%</td>
  73. </tr>
  74. <?php endforeach; ?>
  75. </tbody>
  76. </table>
  77. <?php endif; ?>
  78. </td>
  79. </tr>
  80. </table>
  81. <?php
  82. }
  83. }