Files
majestic/util/profiler/Profiler.php

145 lines
4.2 KiB
PHP
Raw Normal View History

<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage util
* @since 2010-03-09
*/
class Profiler
{
2011-07-29 13:05:23 +04:00
2012-11-19 19:25:38 +04:00
/**
* @var int
*/
protected $start = null;
2011-07-29 13:05:23 +04:00
2012-11-19 19:25:38 +04:00
/**
* @var int
*/
2011-07-29 13:05:23 +04:00
protected $end = null;
2012-11-19 19:25:38 +04:00
/**
* @var CommandProfiler[]
*/
protected $queries = array();
2011-07-29 13:05:23 +04:00
static protected $instance = null;
2011-07-29 13:05:23 +04:00
private function __construct()
{
if (Config::get('PROFILER') == false) {
2012-07-19 17:18:54 +04:00
throw new GeneralException('Need turn PROFILER before use.');
}
}
2011-07-29 13:05:23 +04:00
/**
* Refuse cloning
* @codeCoverageIgnoreStart
*/
2011-07-29 13:05:23 +04:00
private function __clone()
{
}
/**
* @codeCoverageIgnoreEnd
*/
/**
* @return Profiler
*/
static public function getInstance()
{
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
2011-07-29 13:05:23 +04:00
/**
2011-07-29 13:05:23 +04:00
* @param string $type
* @param string $command
* @return CommandProfiler
*/
2011-07-29 13:05:23 +04:00
public function profilerCommand($type, $command)
{
2011-07-29 13:05:23 +04:00
$profiler = new CommandProfiler($type, $command);
$this->queries[] = $profiler;
return $profiler;
}
2011-07-29 13:05:23 +04:00
public function start()
{
$this->queries = array();
$this->start = microtime(true);
}
2011-07-29 13:05:23 +04:00
public function end($html)
{
$this->end = microtime(true);
if (stripos($html, '</body>') == False) {
return $html;
}
return str_ireplace('</body>', $this->getOutput() . '</body>', $html);
}
2011-07-29 13:05:23 +04:00
protected function getOutput()
{
2011-07-29 13:05:23 +04:00
$temp = '';
$queriesTime = 0;
foreach ($this->queries as $query) {
2011-07-29 13:05:23 +04:00
$temp .= '(' . $query->getType() . ') [' . round($query->getElapsed() * 1000, 2) . 'ms] ' . $query->getCommand() . '<br/>';
$queriesTime += $query->getElapsed();
}
2011-07-29 13:05:23 +04:00
$html = '<div style="clear:both; font:12px monospace; margin: 5px; white-space: pre;">'
. 'Elapsed time: ' . round(($this->end - $this->start) * 1000, 2) . 'ms.<br/>';
if (count($this->queries) == 0 && !Config::get('PROFILER_DETAILS')) {
2012-07-19 17:18:54 +04:00
$html .= 'Queries not counted. Turn PROFILER_DETAILS if you want to profile queries.<br/>';
} else {
$html .= 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]<br/>';
}
2011-07-29 13:05:23 +04:00
$html .= $temp;
$html .= '</div>';
return $html;
}
2011-07-29 13:05:23 +04:00
public function getJson()
{
$this->end = microtime(true);
FB::info(round(($this->end - $this->start) * 1000, 2) . ' ms', 'Elapsed time');
$table = array();
$table[] = array('Type', 'Time (ms)', 'Query');
$queriesTime = 0;
foreach ($this->queries as $query) {
$table[] = array($query->getType(), round($query->getElapsed() * 1000, 2), $query->getCommand());
$queriesTime += $query->getElapsed();
}
if (count($this->queries) == 0 && !Config::get('PROFILER_DETAILS')) {
2012-07-19 17:18:54 +04:00
FB::table('Queries not counted. Turn PROFILER_DETAILS if you want to profile queries.', $table);
} else {
FB::table('Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]', $table);
}
2011-07-29 13:05:23 +04:00
}
2011-10-07 15:17:12 +04:00
public function getCli()
{
$this->end = microtime(true);
$queriesTime = 0;
$temp = str_pad(PHP_EOL, 60, '-', STR_PAD_LEFT);
foreach ($this->queries as $query) {
$temp .= sprintf('%-25s[% 10sms] %s', '(' . $query->getType() .')', round($query->getElapsed() * 1000, 2), $query->getCommand()) . PHP_EOL;
$queriesTime += $query->getElapsed();
}
$html = str_pad(PHP_EOL, 60, '-', STR_PAD_LEFT);
$html .= 'Elapsed time: ' . round(($this->end - $this->start) * 1000, 2) . 'ms.' . PHP_EOL;
if (count($this->queries) == 0 && !Config::get('PROFILER_DETAILS')) {
2012-07-19 17:18:54 +04:00
$html .= 'Queries not counted. Turn PROFILER_DETAILS if you want to profile queries.' . PHP_EOL;
} else {
$html .= 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms] ' . PHP_EOL;
}
2011-10-07 15:17:12 +04:00
$html .= $temp;
return $html;
}
}