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.

119 lines
3.4 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
  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('DEBUG') == false) {
  20. throw new GeneralException('Need to turn on DEBUG 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. . 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]<br/>';
  73. $html .= $temp;
  74. $html .= '</div>';
  75. return $html;
  76. }
  77. public function getJson()
  78. {
  79. $this->end = microtime(true);
  80. FB::info(round(($this->end - $this->start) * 1000, 2) . ' ms', 'Elapsed time');
  81. $table = array();
  82. $table[] = array('Type', 'Time (ms)', 'Query');
  83. $queriesTime = 0;
  84. foreach ($this->queries as $query) {
  85. $table[] = array($query->getType(), round($query->getElapsed() * 1000, 2), $query->getCommand());
  86. $queriesTime += $query->getElapsed();
  87. }
  88. FB::table('Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]', $table);
  89. }
  90. public function getCli()
  91. {
  92. $this->end = microtime(true);
  93. $queriesTime = 0;
  94. $temp = str_pad(PHP_EOL, 60, '-', STR_PAD_LEFT);
  95. foreach ($this->queries as $query) {
  96. $temp .= sprintf('%-25s[% 10sms] %s', '(' . $query->getType() .')', round($query->getElapsed() * 1000, 2), $query->getCommand()) . PHP_EOL;
  97. $queriesTime += $query->getElapsed();
  98. }
  99. $html = str_pad(PHP_EOL, 60, '-', STR_PAD_LEFT);
  100. $html .= 'Elapsed time: ' . round(($this->end - $this->start) * 1000, 2) . 'ms.' . PHP_EOL
  101. . 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms] ' . PHP_EOL;
  102. $html .= $temp;
  103. return $html;
  104. }
  105. }