Memcache, refactoring, View helpers, #16
git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@124 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
This commit is contained in:
82
util/Profiler.php
Normal file
82
util/Profiler.php
Normal file
@ -0,0 +1,82 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage util
|
||||
* @since 2010-03-09
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class Profiler
|
||||
{
|
||||
|
||||
protected $start = null;
|
||||
protected $end = null;
|
||||
|
||||
protected $queries = array();
|
||||
|
||||
static protected $instance = null;
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
if (DEBUG == false) {
|
||||
throw new GeneralException('Need to turn on DEBUG before use.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Refuse cloning
|
||||
*/
|
||||
private function __clone(){}
|
||||
|
||||
/**
|
||||
* @return Profiler
|
||||
*/
|
||||
static public function getInstance()
|
||||
{
|
||||
if (!isset(self::$instance)) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $query
|
||||
* @return QueryProfiler
|
||||
*/
|
||||
public function profilerQuery($query)
|
||||
{
|
||||
$profiler = new QueryProfiler($query);
|
||||
$this->queries[] = $profiler;
|
||||
return $profiler;
|
||||
}
|
||||
|
||||
public function start()
|
||||
{
|
||||
$this->start = microtime(true);
|
||||
}
|
||||
|
||||
public function end($html)
|
||||
{
|
||||
$this->end = microtime(true);
|
||||
if (stripos($html, '</body>') == False) {
|
||||
return $html;
|
||||
}
|
||||
return str_ireplace('</body>', $this->getOutput() . '</body>', $html);
|
||||
}
|
||||
|
||||
protected function getOutput()
|
||||
{
|
||||
$html = '<div style="clear:both; font:12px monospace; margin: 5px;">'
|
||||
. 'Elapsed time: ' . round(($this->end- $this->start) * 1000) . 'ms.<br/>'
|
||||
. 'Queries: ' . count($this->queries) . '<br/>';
|
||||
|
||||
foreach ($this->queries as $query) {
|
||||
$html .= '[' . round($query->getElapsed() * 1000) . 'ms] ' . $query->getQuery() . '<br/>';
|
||||
}
|
||||
$html .= '</div>';
|
||||
return $html;
|
||||
}
|
||||
}
|
39
util/QueryProfiler.php
Normal file
39
util/QueryProfiler.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage util
|
||||
* @since 2010-03-09
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class QueryProfiler
|
||||
{
|
||||
|
||||
protected $query = '';
|
||||
protected $start = null;
|
||||
protected $end = null;
|
||||
|
||||
public function __construct($query)
|
||||
{
|
||||
$this->query = $query;
|
||||
$this->start = microtime(true);
|
||||
}
|
||||
|
||||
public function end()
|
||||
{
|
||||
$this->end = microtime(true);
|
||||
}
|
||||
|
||||
public function getQuery()
|
||||
{
|
||||
return $this->query;
|
||||
}
|
||||
|
||||
public function getElapsed()
|
||||
{
|
||||
return $this->end - $this->start;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user