Added timer capabilities to the profiler, refactored a bunch of the sanity checks associated with the profiler.

This commit is contained in:
Josh Sherman 2010-10-22 22:26:45 -04:00
parent 92e4b0391a
commit 9a4fe84b9b
4 changed files with 145 additions and 27 deletions

View file

@ -115,11 +115,26 @@ class Config extends Object
}
// Defaults profiler to true if it doesn't match an option exactly
if (isset($this->data['pickles']['profiler'])
&& $this->data['pickles']['profiler'] != ''
&& !in_array($this->data['pickles']['profiler'], array('objects', 'module', 'queries', 'explains', 'display')))
if (isset($this->data['pickles']['profiler']))
{
$this->data['pickles']['profiler'] = true;
if ($this->data['pickles']['profiler'] !== true)
{
// If we have an array convert to a string
if (is_array($this->data['pickles']['profiler']))
{
$this->data['pickles']['profiler'] = implode(',', $this->data['pickles']['profiler']);
}
// Checks that one of our known values exists, if not, force true
if (preg_match('/(objects|timers|queries|explains)/', $this->data['pickles']['profiler'] == false))
{
$this->data['pickles']['profiler'] = true;
}
}
}
else
{
$this->data['pickles']['profiler'] = false;
}
return true;

View file

@ -214,7 +214,7 @@ class Controller extends Object
$module_return = null;
// Gets the profiler status
$profiler = (isset($this->config->pickles['profiler']) && $this->config->pickles['profiler'] != '' ? $this->config->pickles['profiler'] : false);
$profiler = $this->config->pickles['profiler'];
// Attempts to execute the default method
if (method_exists($module, '__default'))
@ -231,11 +231,10 @@ class Controller extends Object
'keywords' => $module->keywords
));
// Profiles the module
// @todo Update to Profiler::timer('module') when timer is implemented
if ($profiler === true || ((is_array($profiler) && in_array('module', $profiler)) || stripos($profiler, 'module') !== false))
// Starts a timer before the module is executed
if ($profiler === true || stripos($profiler, 'timers') !== false)
{
Profiler::log($module, '__default');
Profiler::timer('module __default');
}
/**
@ -244,17 +243,28 @@ class Controller extends Object
* or setting it on the object
*/
$display->setModuleReturn($module->__default());
// Stops the module timer
if ($profiler === true || stripos($profiler, 'timers') !== false)
{
Profiler::timer('module __default');
}
}
// Profiles the display
// @todo Update to Profiler::timer('display') when timer is implemented
if ($profiler === true || ((is_array($profiler) && in_array('display', $profiler)) || stripos($profiler, 'display') !== false))
// Starts a timer for the display rendering
if ($profiler === true || stripos($profiler, 'timers') !== false)
{
Profiler::log($display, 'render');
Profiler::timer('display render');
}
// Renders the content
$display->render();
// Steps the display timer
if ($profiler === true || stripos($profiler, 'timers') !== false)
{
Profiler::timer('display render');
}
}
public function __destruct()
@ -262,7 +272,7 @@ class Controller extends Object
parent::__destruct();
// Display the Profiler's report is the stars are aligned
if (isset($this->config->pickles['profiler']) && $this->config->pickles['profiler'] != false && $this->passthru == false)
if ($this->config->pickles['profiler'] != false && $this->passthru == false)
{
Profiler::report();
}

View file

@ -139,16 +139,14 @@ class Database_PDO_Common extends Database_Common
{
try
{
// Establishes if the profiler is enabled
$profiler = (isset($this->config->pickles['profiler']) && $this->config->pickles['profiler'] != '' && preg_match('/^EXPLAIN /i', $sql) == false ? $this->config->pickles['profiler'] : false);
$explain = false;
// Establishes if we're working on an EXPLAIN
$explaining = preg_match('/^EXPLAIN /i', $sql);
// Executes a standard query
if ($input_parameters === null)
{
// Explains the query
if ($profiler === true || ((is_array($profiler) && in_array('explains', $profiler)) || stripos($profiler, 'explains') !== false))
if ($explaining == false && Profiler::enabled('explains'))
{
$explain = $this->fetchAll('EXPLAIN ' . $sql);
}
@ -160,7 +158,7 @@ class Database_PDO_Common extends Database_Common
else
{
// Explains the query
if ($profiler === true || ((is_array($profiler) && in_array('explains', $profiler)) || stripos($profiler, 'explains') !== false))
if ($explaining == false && Profiler::enabled('explains'))
{
$explain = $this->fetchAll('EXPLAIN ' . $sql, $input_parameters);
}
@ -179,13 +177,9 @@ class Database_PDO_Common extends Database_Common
}
// Logs the information to the profiler
if ($profiler === true
|| ((is_array($profiler) && in_array('explains', $profiler))
|| stripos($profiler, 'explains') !== false)
|| ((is_array($profiler) && in_array('queries', $profiler))
|| stripos($profiler, 'queries') !== false))
if ($explaining == false && Profiler::enabled('explains', 'queries'))
{
Profiler::logQuery($sql, $input_parameters, $explain, $duration);
Profiler::logQuery($sql, $input_parameters, (isset($explain) ? $explain : false), $duration);
}
}
catch (PDOException $e)

View file

@ -33,6 +33,17 @@
*/
class Profiler
{
/**
* Config
*
* Profiler configuration
*
* @static
* @access private
* @var array
*/
private static $config;
/**
* Profile
*
@ -55,6 +66,17 @@ class Profiler
*/
private static $queries = 0;
/**
* Timers
*
* Array of active timers
*
* @static
* @access private
* @var array
*/
private static $timers = array();
/**
* Constructor
*
@ -66,6 +88,45 @@ class Profiler
{
}
/**
* Enabled
*
* Checks if the profiler is set to boolean true or if the passed type is
* specified in the profiler configuration value.
*
* @param array $type type(s) to check
* @return boolean whether or not the type is enabled
*/
public static function enabled(/* polymorphic */)
{
// Grabs the config object if we don't have one yet
if (self::$config == null)
{
$config = Config::getInstance();
self::$config = $config->pickles['profiler'];
}
// Checks if we're set to boolean true
if (self::$config === true)
{
return true;
}
else
{
$types = func_get_args();
foreach ($types as $type)
{
if (stripos(self::$config, $type) !== false)
{
return true;
}
}
}
return false;
}
/**
* Log
@ -83,7 +144,7 @@ class Profiler
public static function log($data, $method = false, $type = false)
{
$time = microtime(true);
$data_type = gettype($data);
$data_type = ($data == 'timer' ? $data : gettype($data));
// Tidys the data by type
switch ($data_type)
@ -100,6 +161,12 @@ class Profiler
$data_type = '<span style="color:Peru">' . $data_type . '</span>';
break;
case 'timer':
$log = $method;
$data_type = '<span style="color:#6c0">' . $data_type . '</span>';
break;
case 'string':
default:
if ($type != false)
@ -170,6 +237,38 @@ class Profiler
self::log($log, false, '<span style="color:DarkCyan">database</span>');
}
/**
* Timer
*
* Logs the start and end of a timer.
*
* @param string $timer name of the timer
* @return boolean whether or not timer profiling is enabled
*/
public static function timer($timer)
{
if (self::enabled('timers'))
{
// Starts the timer
if (!isset(self::$timers[$timer]))
{
self::$timers[$timer] = microtime(true);
self::Log('timer', '<span style="color:Orchid">Started timer</span> <span style="color:Yellow">' . $timer . '</span>');
}
// Ends the timer
else
{
self::Log('timer', '<span style="color:Orchid">Stopped timer</span> <span style="color:Yellow">' . $timer . '</span> <span style="color:#666">=></span> <span style="color:DarkKhaki">Time Elapsed:</span> ' . number_format((microtime(true) - self::$timers[$timer]) * 100, 3) . ' ms');
unset(self::$timers[$timer]);
}
return true;
}
return false;
}
/**
* Report
*