DebugExecutionTime.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. /*
  23. * @param $msg string or NULL (NULL to log only group stats)
  24. */
  25. public function log($msg, $groups = array()) {
  26. if (!$this->_isActive) return;
  27. $curTime = microtime(true);
  28. $execTime = (!$this->_lastTime)? 0 : $curTime - $this->_lastTime;
  29. $this->_lastTime = $curTime;
  30. $this->_log[] = array($msg, $execTime);
  31. foreach ($groups as $group) {
  32. if (!array_key_exists($group, $this->_logByGroups)) $this->_logByGroups[$group] = 0;
  33. $this->_logByGroups[$group] += $execTime;
  34. }
  35. }
  36. public function getLastExecTime() {
  37. if (!$this->_isActive) return null;
  38. $logTotal = count($this->_log);
  39. if ($logTotal < 2) return null;
  40. return $this->_log[$logTotal - 1][1];
  41. }
  42. public function getTotalExecTime() {
  43. if (!$this->_isActive) return null;
  44. $logTotal = count($this->_log);
  45. if ($logTotal < 2) return null;
  46. $sumExecTime = 0;
  47. foreach ($this->_log as $log) $sumExecTime += $log[1];
  48. return $sumExecTime;
  49. }
  50. public function printDebug() {
  51. if (!$this->_isActive) return;
  52. $sumExecTime = 0;
  53. foreach ($this->_log as $log) $sumExecTime += $log[1];
  54. ?>
  55. <table>
  56. <tr>
  57. <td style="vertical-align:top;">
  58. <div style="max-height:300px;overflow:auto">
  59. <table style="text-align:left;" border="1" cellspacing="0" cellpadding="1">
  60. <thead>
  61. <tr>
  62. <th>msg</th>
  63. <th>time</th>
  64. </tr>
  65. </thead>
  66. <tfoot>
  67. <th style="text-align:right">total:</th>
  68. <th style="font-family:monospace;text-align:right;font-weight:bold;"><?php echo number_format($sumExecTime, 6); ?></th>
  69. </tfoot>
  70. <tbody>
  71. <?php foreach ($this->_log as $log) : ?>
  72. <?php if (null === $log[0]) continue; ?>
  73. <tr>
  74. <td><?php echo $log[0]; ?></td>
  75. <td style="font-family:monospace;text-align:right;"><?php echo number_format($log[1], 6); ?></td>
  76. </tr>
  77. <?php endforeach; ?>
  78. </tbody>
  79. </table>
  80. </div>
  81. </td>
  82. <td style="vertical-align:top;">
  83. <?php if (!empty($this->_logByGroups)) : ?>
  84. <table style="text-align:left;" border="1" cellspacing="0" cellpadding="1">
  85. <thead>
  86. <tr>
  87. <th>group</th>
  88. <th>time</th>
  89. <th>time left</th>
  90. <th>time %</th>
  91. </tr>
  92. </thead>
  93. <tbody>
  94. <?php foreach ($this->_logByGroups as $group => $execTime) : ?>
  95. <tr>
  96. <th><?php echo $group; ?></th>
  97. <td style="font-family:monospace;text-align:right;"><?php echo number_format($execTime, 6); ?></td>
  98. <td style="font-family:monospace;text-align:right;"><?php echo number_format($sumExecTime - $execTime, 6); ?></td>
  99. <td style="font-family:monospace;text-align:right;"><?php echo (!$execTime)? 0 : round($execTime * 100 / $sumExecTime); ?>%</td>
  100. </tr>
  101. <?php endforeach; ?>
  102. </tbody>
  103. </table>
  104. <?php endif; ?>
  105. </td>
  106. </tr>
  107. </table>
  108. <?php
  109. }
  110. }