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.

131 lines
4.1 KiB

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