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.

141 lines
4.2 KiB

13 years ago
12 years ago
13 years ago
12 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
  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. /**
  14. * @var int
  15. */
  16. protected $start = null;
  17. /**
  18. * @var int
  19. */
  20. protected $end = null;
  21. /**
  22. * @var CommandProfiler[]
  23. */
  24. protected $queries = array();
  25. static protected $instance = null;
  26. private function __construct()
  27. {
  28. if (Config::get('PROFILER') == false) {
  29. throw new GeneralException('Need turn PROFILER before use.');
  30. }
  31. }
  32. /**
  33. * Refuse cloning
  34. */
  35. private function __clone()
  36. {
  37. }
  38. /**
  39. * @return Profiler
  40. */
  41. static public function getInstance()
  42. {
  43. if (!isset(self::$instance)) {
  44. self::$instance = new self();
  45. }
  46. return self::$instance;
  47. }
  48. /**
  49. * @param string $type
  50. * @param string $command
  51. * @return CommandProfiler
  52. */
  53. public function profilerCommand($type, $command)
  54. {
  55. $profiler = new CommandProfiler($type, $command);
  56. $this->queries[] = $profiler;
  57. return $profiler;
  58. }
  59. public function start()
  60. {
  61. $this->queries = array();
  62. $this->start = microtime(true);
  63. }
  64. public function end($html)
  65. {
  66. $this->end = microtime(true);
  67. if (stripos($html, '</body>') == False) {
  68. return $html;
  69. }
  70. return str_ireplace('</body>', $this->getOutput() . '</body>', $html);
  71. }
  72. protected function getOutput()
  73. {
  74. $temp = '';
  75. $queriesTime = 0;
  76. foreach ($this->queries as $query) {
  77. $temp .= '(' . $query->getType() . ') [' . round($query->getElapsed() * 1000, 2) . 'ms] ' . $query->getCommand() . '<br/>';
  78. $queriesTime += $query->getElapsed();
  79. }
  80. $html = '<div style="clear:both; font:12px monospace; margin: 5px; white-space: pre;">'
  81. . 'Elapsed time: ' . round(($this->end - $this->start) * 1000, 2) . 'ms.<br/>';
  82. if (count($this->queries) == 0 && !Config::get('PROFILER_DETAILS')) {
  83. $html .= 'Queries not counted. Turn PROFILER_DETAILS if you want to profile queries.<br/>';
  84. } else {
  85. $html .= 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]<br/>';
  86. }
  87. $html .= $temp;
  88. $html .= '</div>';
  89. return $html;
  90. }
  91. public function getJson()
  92. {
  93. $this->end = microtime(true);
  94. FB::info(round(($this->end - $this->start) * 1000, 2) . ' ms', 'Elapsed time');
  95. $table = array();
  96. $table[] = array('Type', 'Time (ms)', 'Query');
  97. $queriesTime = 0;
  98. foreach ($this->queries as $query) {
  99. $table[] = array($query->getType(), round($query->getElapsed() * 1000, 2), $query->getCommand());
  100. $queriesTime += $query->getElapsed();
  101. }
  102. if (count($this->queries) == 0 && !Config::get('PROFILER_DETAILS')) {
  103. FB::table('Queries not counted. Turn PROFILER_DETAILS if you want to profile queries.', $table);
  104. } else {
  105. FB::table('Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]', $table);
  106. }
  107. }
  108. public function getCli()
  109. {
  110. $this->end = microtime(true);
  111. $queriesTime = 0;
  112. $temp = str_pad(PHP_EOL, 60, '-', STR_PAD_LEFT);
  113. foreach ($this->queries as $query) {
  114. $temp .= sprintf('%-25s[% 10sms] %s', '(' . $query->getType() .')', round($query->getElapsed() * 1000, 2), $query->getCommand()) . PHP_EOL;
  115. $queriesTime += $query->getElapsed();
  116. }
  117. $html = str_pad(PHP_EOL, 60, '-', STR_PAD_LEFT);
  118. $html .= 'Elapsed time: ' . round(($this->end - $this->start) * 1000, 2) . 'ms.' . PHP_EOL;
  119. if (count($this->queries) == 0 && !Config::get('PROFILER_DETAILS')) {
  120. $html .= 'Queries not counted. Turn PROFILER_DETAILS if you want to profile queries.' . PHP_EOL;
  121. } else {
  122. $html .= 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms] ' . PHP_EOL;
  123. }
  124. $html .= $temp;
  125. return $html;
  126. }
  127. }