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.

132 lines
4.1 KiB

14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 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->queries = array();
  53. $this->start = microtime(true);
  54. }
  55. public function end($html)
  56. {
  57. $this->end = microtime(true);
  58. if (stripos($html, '</body>') == False) {
  59. return $html;
  60. }
  61. return str_ireplace('</body>', $this->getOutput() . '</body>', $html);
  62. }
  63. protected function getOutput()
  64. {
  65. $temp = '';
  66. $queriesTime = 0;
  67. foreach ($this->queries as $query) {
  68. $temp .= '(' . $query->getType() . ') [' . round($query->getElapsed() * 1000, 2) . 'ms] ' . $query->getCommand() . '<br/>';
  69. $queriesTime += $query->getElapsed();
  70. }
  71. $html = '<div style="clear:both; font:12px monospace; margin: 5px; white-space: pre;">'
  72. . 'Elapsed time: ' . round(($this->end - $this->start) * 1000, 2) . 'ms.<br/>';
  73. if (count($this->queries) == 0 && !Config::get('PROFILER_DETAILS')) {
  74. $html .= 'Queries not counted. Turn PROFILER_DETAILS if you want to profile queries.<br/>';
  75. } else {
  76. $html .= 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]<br/>';
  77. }
  78. $html .= $temp;
  79. $html .= '</div>';
  80. return $html;
  81. }
  82. public function getJson()
  83. {
  84. $this->end = microtime(true);
  85. FB::info(round(($this->end - $this->start) * 1000, 2) . ' ms', 'Elapsed time');
  86. $table = array();
  87. $table[] = array('Type', 'Time (ms)', 'Query');
  88. $queriesTime = 0;
  89. foreach ($this->queries as $query) {
  90. $table[] = array($query->getType(), round($query->getElapsed() * 1000, 2), $query->getCommand());
  91. $queriesTime += $query->getElapsed();
  92. }
  93. if (count($this->queries) == 0 && !Config::get('PROFILER_DETAILS')) {
  94. FB::table('Queries not counted. Turn PROFILER_DETAILS if you want to profile queries.', $table);
  95. } else {
  96. FB::table('Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]', $table);
  97. }
  98. }
  99. public function getCli()
  100. {
  101. $this->end = microtime(true);
  102. $queriesTime = 0;
  103. $temp = str_pad(PHP_EOL, 60, '-', STR_PAD_LEFT);
  104. foreach ($this->queries as $query) {
  105. $temp .= sprintf('%-25s[% 10sms] %s', '(' . $query->getType() .')', round($query->getElapsed() * 1000, 2), $query->getCommand()) . PHP_EOL;
  106. $queriesTime += $query->getElapsed();
  107. }
  108. $html = str_pad(PHP_EOL, 60, '-', STR_PAD_LEFT);
  109. $html .= 'Elapsed time: ' . round(($this->end - $this->start) * 1000, 2) . 'ms.' . PHP_EOL;
  110. if (count($this->queries) == 0 && !Config::get('PROFILER_DETAILS')) {
  111. $html .= 'Queries not counted. Turn PROFILER_DETAILS if you want to profile queries.' . PHP_EOL;
  112. } else {
  113. $html .= 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms] ' . PHP_EOL;
  114. }
  115. $html .= $temp;
  116. return $html;
  117. }
  118. }