diff --git a/core/Bootstrap.php b/core/Bootstrap.php index 64f3bbc5e..15190ef22 100644 --- a/core/Bootstrap.php +++ b/core/Bootstrap.php @@ -87,6 +87,7 @@ protected function _initDatabase() 'username' => $config->database->params->username, 'password' => $config->database->params->password, 'driver_options' => $driverOptions, + 'profiler' => array_key_exists('profiler', $_GET), ); if (empty($config->database->params->unix_socket)) { diff --git a/index.php b/index.php index c9eb2108f..e0f101c5c 100644 --- a/index.php +++ b/index.php @@ -37,3 +37,47 @@ $application = new Zend_Application('global', CORE_CONFIG); $application->bootstrap()->run(); + +if (array_key_exists('profiler', $_GET)) { + Zend_Registry::get('logger')->info('Profiler for '.$_SERVER['REQUEST_URI']); + $db = Zend_Db_Table::getDefaultAdapter(); + $profiler = $db->getProfiler(); + $profile = ''; + $queryCounts = array(); + $queryTimes = array(); + foreach ($profiler->getQueryProfiles() as $query) { + if (array_key_exists($query->getQuery(), $queryCounts)) { + $queryCounts[$query->getQuery()] += 1; + } else { + $queryCounts[$query->getQuery()] = 1; + } + $queryTimes[] = array('query' => $query->getQuery(), 'time_seconds' => $query->getElapsedSecs()); + } + // Sort queries by count and time + arsort($queryCounts, SORT_NUMERIC); + function cmp($a, $b) + { + if ($a['time_seconds'] == $b['time_seconds']) { + return 0; + } + + return ($a['time_seconds'] < $b['time_seconds']) ? 1 : -1; + } + uasort($queryTimes, 'cmp'); + + if (array_key_exists('profilerFiveSlowest', $_GET)) { + // Logs much less info, only the five slowest queries. + $fiveSlowest = array(); + $count = 0; + foreach ($queryTimes as $query => $time) { + $fiveSlowest[$query] = $time; + if ($count++ == 4) { + break; + } + } + Zend_Registry::get('logger')->info(print_r($fiveSlowest, true)); + } else { + Zend_Registry::get('logger')->info(print_r($queryCounts, true)); + Zend_Registry::get('logger')->info(print_r($queryTimes, true)); + } +}