Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

log slowest query times #210

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
44 changes: 44 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no longer used

$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));
}
}