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.

144 lines
4.3 KiB

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