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.

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