You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.9 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage util
  7. * @since 2010-03-09
  8. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. class Profiler
  12. {
  13. protected $start = null;
  14. protected $end = null;
  15. protected $queries = array();
  16. static protected $instance = null;
  17. private function __construct()
  18. {
  19. if (DEBUG == false) {
  20. throw new GeneralException('Need to turn on DEBUG before use.');
  21. }
  22. }
  23. /**
  24. * Refuse cloning
  25. */
  26. private function __clone(){}
  27. /**
  28. * @return Profiler
  29. */
  30. static public function getInstance()
  31. {
  32. if (!isset(self::$instance)) {
  33. self::$instance = new self();
  34. }
  35. return self::$instance;
  36. }
  37. /**
  38. * @param string $query
  39. * @return QueryProfiler
  40. */
  41. public function profilerQuery($query)
  42. {
  43. $profiler = new QueryProfiler($query);
  44. $this->queries[] = $profiler;
  45. return $profiler;
  46. }
  47. public function start()
  48. {
  49. $this->start = microtime(true);
  50. }
  51. public function end($html)
  52. {
  53. $this->end = microtime(true);
  54. if (stripos($html, '</body>') == False) {
  55. return $html;
  56. }
  57. return str_ireplace('</body>', $this->getOutput() . '</body>', $html);
  58. }
  59. protected function getOutput()
  60. {
  61. $html = '<div style="clear:both; font:12px monospace; margin: 5px;">'
  62. . 'Elapsed time: ' . round(($this->end- $this->start) * 1000) . 'ms.<br/>'
  63. . 'Queries: ' . count($this->queries) . '<br/>';
  64. foreach ($this->queries as $query) {
  65. $html .= '[' . round($query->getElapsed() * 1000) . 'ms] ' . $query->getQuery() . '<br/>';
  66. }
  67. $html .= '</div>';
  68. return $html;
  69. }
  70. }