sfWebDebugPanelTimer.class.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfWebDebugPanelTimer adds a panel to the web debug toolbar with timer information.
  11. *
  12. * @package symfony
  13. * @subpackage debug
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfWebDebugPanelTimer.class.php 12982 2008-11-13 17:25:10Z hartym $
  16. */
  17. class sfWebDebugPanelTimer extends sfWebDebugPanel
  18. {
  19. static protected
  20. $startTime = null;
  21. /**
  22. * Constructor.
  23. *
  24. * @param sfWebDebug $webDebug The web debut toolbar instance
  25. */
  26. public function __construct(sfWebDebug $webDebug)
  27. {
  28. parent::__construct($webDebug);
  29. $this->webDebug->getEventDispatcher()->connect('debug.web.filter_logs', array($this, 'filterLogs'));
  30. }
  31. public function getTitle()
  32. {
  33. return '<img src="'.$this->webDebug->getOption('image_root_path').'/time.png" alt="Time" /> '.$this->getTotalTime().' ms';
  34. }
  35. public function getPanelTitle()
  36. {
  37. return 'Timers';
  38. }
  39. public function getPanelContent()
  40. {
  41. if (sfTimerManager::getTimers())
  42. {
  43. $totalTime = $this->getTotalTime();
  44. $panel = '<table class="sfWebDebugLogs" style="width: 300px"><tr><th>type</th><th>calls</th><th>time (ms)</th><th>time (%)</th></tr>';
  45. foreach (sfTimerManager::getTimers() as $name => $timer)
  46. {
  47. $panel .= sprintf('<tr><td class="sfWebDebugLogType">%s</td><td class="sfWebDebugLogNumber" style="text-align: right">%d</td><td style="text-align: right">%.2f</td><td style="text-align: right">%d</td></tr>', $name, $timer->getCalls(), $timer->getElapsedTime() * 1000, $totalTime ? ($timer->getElapsedTime() * 1000 * 100 / $totalTime) : 'N/A');
  48. }
  49. $panel .= '</table>';
  50. return $panel;
  51. }
  52. }
  53. public function filterLogs(sfEvent $event, $logs)
  54. {
  55. $newLogs = array();
  56. foreach ($logs as $log)
  57. {
  58. if ('sfWebDebugLogger' != $log['type'])
  59. {
  60. $newLogs[] = $log;
  61. }
  62. }
  63. return $newLogs;
  64. }
  65. static public function startTime()
  66. {
  67. self::$startTime = microtime(true);
  68. }
  69. static public function isStarted()
  70. {
  71. return !is_null(self::$startTime);
  72. }
  73. protected function getTotalTime()
  74. {
  75. return !is_null(self::$startTime) ? sprintf('%.0f', (microtime(true) - self::$startTime) * 1000) : 0;
  76. }
  77. }