* @copyright 2007-2011 PrestaShop SA * @version Release: $Revision: 6643 $ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * International Registered Trademark & Property of PrestaShop SA */ // // IMPORTANT : don't forget to delete the underscore _ in the file name if you want to use it ! // function developpementErrorHandler($errno, $errstr, $errfile, $errline) { if (!(error_reporting() & $errno)) return; switch($errno) { case E_ERROR: echo '[PHP Error #'.$errno.'] '.$errstr.' ('.$errfile.', line '.$errline.')'; break; case E_WARNING: echo '[PHP Warning #'.$errno.'] '.$errstr.' ('.$errfile.', line '.$errline.')'; break; case E_PARSE: echo '[PHP Parse #'.$errno.'] '.$errstr.' ('.$errfile.', line '.$errline.')'; break; case E_NOTICE: echo '[PHP Notice #'.$errno.'] '.$errstr.' ('.$errfile.', line '.$errline.')'; break; case E_CORE_ERROR: echo '[PHP Core #'.$errno.'] '.$errstr.' ('.$errfile.', line '.$errline.')'; break; case E_CORE_WARNING: echo '[PHP Core warning #'.$errno.'] '.$errstr.' ('.$errfile.', line '.$errline.')'; break; case E_COMPILE_ERROR: echo '[PHP Compile #'.$errno.'] '.$errstr.' ('.$errfile.', line '.$errline.')'; break; case E_COMPILE_WARNING: echo '[PHP Compile warning #'.$errno.'] '.$errstr.' ('.$errfile.', line '.$errline.')'; break; case E_USER_ERROR: echo '[PHP Error #'.$errno.'] '.$errstr.' ('.$errfile.', line '.$errline.')'; break; case E_USER_WARNING: echo '[PHP User warning #'.$errno.'] '.$errstr.' ('.$errfile.', line '.$errline.')'; break; case E_USER_NOTICE: echo '[PHP User notice #'.$errno.'] '.$errstr.' ('.$errfile.', line '.$errline.')'; break; case E_STRICT: echo '[PHP Strict #'.$errno.'] '.$errstr.' ('.$errfile.', line '.$errline.')'; break; case E_RECOVERABLE_ERROR: echo '[PHP Recoverable error #'.$errno.'] '.$errstr.' ('.$errfile.', line '.$errline.')'; break; default: echo '[PHP Unknown error #'.$errno.'] '.$errstr.' ('.$errfile.', line '.$errline.')'; } die; return true; } class FrontController extends FrontControllerCore { public $_memory = array(); public $_time = array(); private static $_footer = true; public static function disableParentCalls() { self::$_footer = false; } private function displayMemoryColor($n) { $n /= 1048576; if ($n > 3) return ''.round($n, 2).' Mb'; if ($n > 1) return ''.round($n, 2).' Mb'; return ''.round($n, 2).' Mb'; } private function displaySQLQueries($n) { if ($n > 150) return ''.$n.' queries'; if ($n > 100) return ''.$n.' queries'; return ''.$n.' quer'.($n == 1 ? 'y' : 'ies').''; } private function displayLoadTimeColor($n, $kikoo = false) { if ($n > 1) return ''.round($n, 3).'s'.($kikoo ? '
You\'d better run your shop on a toaster' : ''); if ($n > 0.5) return ''.round($n * 1000).'ms'.($kikoo ? '
I hope it is a shared hosting' : ''); return ''.round($n * 1000).'ms'.($kikoo ? '
Good boy! That\'s what I call a webserver!' : ''); } private function getTimeColor($n) { if ($n > 4) return 'style="color:red"'; if ($n > 2) return 'style="color:orange"'; return 'style="color:green"'; } private function getQueryColor($n) { if ($n > 5) return 'style="color:red"'; if ($n > 2) return 'style="color:orange"'; return 'style="color:green"'; } private function getTableColor($n) { if ($n > 30) return 'style="color:red"'; if ($n > 20) return 'style="color:orange"'; return 'style="color:green"'; } public function __construct() { // error management set_error_handler('developpementErrorHandler'); ini_set('html_errors', 'on'); ini_set('display_errors', 'on'); error_reporting(E_ALL | E_STRICT); $this->_memory = array_fill(0, 10, 0); $this->_time = array_fill(0, 10, 0); // Usually set in the parent constructor, but here I need it to evaluate init() $useSSL = $this->ssl; if (!self::$_footer) return; $this->_memory[-3] = memory_get_usage(); $this->_time[-3] = microtime(true); $this->init(); $this->_memory[-2] = memory_get_usage(); $this->_time[-2] = microtime(true); parent::__construct(); $this->_memory[-1] = memory_get_usage(); $this->_time[-1] = microtime(true); } public function run() { $this->_memory[0] = memory_get_usage(); $this->_time[0] = microtime(true); $this->preProcess(); $this->_memory[1] = memory_get_usage(); $this->_time[1] = microtime(true); $this->setMedia(); $this->_memory[2] = memory_get_usage(); $this->_time[2] = microtime(true); $this->displayHeader(); $this->_memory[3] = memory_get_usage(); $this->_time[3] = microtime(true); $this->process(); $this->_memory[4] = memory_get_usage(); $this->_time[4] = microtime(true); $this->displayContent(); $this->_memory[5] = memory_get_usage(); $this->_time[5] = microtime(true); $this->displayFooter(); } function ini_get_display_errors() { $a = 'display_errors'; $b = ini_get($a); switch (strtolower($b)) { case 'on': case 'yes': case 'true': return 'assert.active' !== $a; case 'stdout': case 'stderr': return 'display_errors' === $a; default: return (bool)(int)$b; } } private function sizeofvar($var) { $start_memory = memory_get_usage(); $tmp = unserialize(serialize($var)); $size = memory_get_usage() - $start_memory; return $size; } public function displayFooter() { global $start_time; if (self::$_footer) parent::displayFooter(); if (!$this->ini_get_display_errors()) return; $this->_memory[6] = memory_get_usage(); $this->_time[6] = microtime(true); $hr = '
'; $totalSize = 0; foreach (get_included_files() as $file) $totalSize += filesize($file); $totalQueryTime = 0; foreach (Db::getInstance()->queriesTime as $time) $totalQueryTime += $time; $hooktime = Module::getHookTime(); arsort($hooktime); $totalHookTime = 0; foreach ($hooktime as $time) $totalHookTime += $time; $globalSize = array(); $totalGlobalSize = 0; foreach ($GLOBALS as $key => $value) if ($key != 'GLOBALS') { $totalGlobalSize += ($size = $this->sizeofvar($value)); if ($size > 1024) $globalSize[$key] = round($size / 1024, 1); } arsort($globalSize); echo '

Load time: '.$this->displayLoadTimeColor($this->_time[6] - $start_time, true).''; if (self::$_footer) echo ' '; echo '
Hook processing: '.$this->displayLoadTimeColor($totalHookTime).'
Memory peak usage: '.$this->displayMemoryColor(memory_get_peak_usage()).''; if (self::$_footer) echo ' '; echo '
'; $countByTypes = ''; foreach (Db::getInstance()->countTypes as $type => $count) if ($count) $countByTypes .= '
  • '.$count.' x '.$type.'
  • '; $countByTypes = rtrim($countByTypes, ' |'); echo '
    SQL Queries: '.$this->displaySQLQueries(Db::getInstance()->count).'
    Time spent querying: '.$this->displayLoadTimeColor($totalQueryTime).'
    Included files: '.sizeof(get_included_files()).'
    Size of included files: '.$this->displayMemoryColor($totalSize).'
    Globals (> 1 Ko only): '.round($totalGlobalSize / 1024).' Ko
    '; echo '

    Stopwatch (with SQL_NO_CACHE)

    '; $queries = Db::getInstance()->queriesTime; arsort($queries); foreach ($queries as $q => $time) echo $hr.'getTimeColor($time * 1000).'>'.round($time * 1000, 3).' ms '.$q; echo '

    Doubles (IDs replaced by "XX")

    '; $queries = Db::getInstance()->queries; arsort($queries); foreach ($queries as $q => $nb) echo $hr.'getQueryColor($nb).'>'.$nb.' '.$q; echo '

    Tables stress

    '; $tables = Db::getInstance()->tables; arsort($tables); foreach ($tables as $table => $nb) echo $hr.'getTableColor($nb).'>'.$nb.' '.$table; echo '
    '; } }