DebugExecutionTime.php 3.1 KB

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