Index: branches/RC/core/kernel/application.php =================================================================== diff -u -N -r10459 -r10494 --- branches/RC/core/kernel/application.php (.../application.php) (revision 10459) +++ branches/RC/core/kernel/application.php (.../application.php) (revision 10494) @@ -593,12 +593,14 @@ } // this allows to save 2 sql queries for each category - $sql = 'SELECT NamedParentPath, CachedCategoryTemplate + $sql = 'SELECT NamedParentPath, CachedCategoryTemplate, TreeLeft, TreeRight FROM '.$table.' WHERE '.$id_field.' = '.$this->Conn->qstr($id); $category_data = $this->Conn->GetRow($sql); $filename = $category_data['NamedParentPath']; $this->setCache('category_templates', $id, $category_data['CachedCategoryTemplate']); + $this->setCache('category_tree', $id, $category_data['TreeLeft'] . ';' . $category_data['TreeRight']); + // $this->setCache('item_templates', $id, $category_data['CachedItemTemplate']); } else { @@ -633,6 +635,8 @@ function setCache($cache_name, $key, $value) { $cache =& $this->recallObject('Cache'); + /* @var $cache kCache */ + $cache->setCache($cache_name, $key, $value); } @@ -799,20 +803,6 @@ $this->Debugger->appendMemoryUsage('Application before Done:'); } - - /*$dupe_queries = Array (); - foreach ($this->Conn->_queryLog as $sql => $repeat_count) { - if ($repeat_count > 1) { - $dupe_queries[] = Array ( - 'sql' => $this->Debugger->formatSQL($sql), - 'dupe_count' => $repeat_count - 1, - ); - } - } - - print_pre($dupe_queries, 'Duplicate SQLs', true);*/ - - if ($this->GetVar('admin')) { $reg = '/('.preg_quote(BASE_PATH, '/').'.*\.html)(#.*){0,1}(")/sU'; $this->HTML = preg_replace($reg, "$1?admin=1$2$3", $this->HTML); @@ -824,6 +814,11 @@ $this->EventManager->RunRegularEvents(reAFTER); $this->Session->SaveData(); + if (constOn('DBG_CACHE')) { + $cache =& $this->recallObject('Cache'); + $cache->printStatistics(); + } + $this->HTML = ob_get_clean() . $this->HTML . $this->Debugger->printReport(true); } else { @@ -844,16 +839,101 @@ flush(); - if ($this->isDebugMode() && constOn('DBG_CACHE')) { - $cache =& $this->recallObject('Cache'); - $cache->printStatistics(); - } - $this->EventManager->RunRegularEvents(reAFTER); $this->Session->SaveData(); + + if (defined('DBG_CAPTURE_STATISTICS') && DBG_CAPTURE_STATISTICS && !$this->IsAdmin()) { + $this->_storeStatistics(); + } } /** + * Stores script execution statistics to database + * + */ + function _storeStatistics() + { + global $start; + + $script_time = getmicrotime() - $start; + $query_statistics = $this->Conn->getQueryStatistics(); // time & count + + $sql = 'SELECT * + FROM ' . TABLE_PREFIX . 'StatisticsCapture + WHERE TemplateName = "' . $this->GetVar('t') . '"'; + $data = $this->Conn->GetRow($sql); + + if ($data) { + $this->_updateAverageStatistics($data, 'ScriptTime', $script_time); + $this->_updateAverageStatistics($data, 'SqlTime', $query_statistics['time']); + $this->_updateAverageStatistics($data, 'SqlCount', $query_statistics['count']); + + $data['Hits']++; + + $this->Conn->doUpdate($data, TABLE_PREFIX . 'StatisticsCapture', 'StatisticsId = ' . $data['StatisticsId']); + } + else { + $data['ScriptTimeMin'] = $data['ScriptTimeAvg'] = $data['ScriptTimeMax'] = $script_time; + $data['SqlTimeMin'] = $data['SqlTimeAvg'] = $data['SqlTimeMax'] = $query_statistics['time']; + $data['SqlCountMin'] = $data['SqlCountAvg'] = $data['SqlCountMax'] = $query_statistics['count']; + $data['TemplateName'] = $this->GetVar('t'); + $data['Hits'] = 1; + $this->Conn->doInsert($data, TABLE_PREFIX . 'StatisticsCapture'); + } + } + + /** + * Calculates average time for statistics + * + * @param Array $data + * @param string $field_prefix + * @param float $current_value + */ + function _updateAverageStatistics(&$data, $field_prefix, $current_value) + { + $data[$field_prefix . 'Avg'] = (($data['Hits'] * $data[$field_prefix . 'Avg']) + $current_value) / ($data['Hits'] + 1); + + if ($current_value < $data[$field_prefix . 'Min']) { + $data[$field_prefix . 'Min'] = $current_value; + } + + if ($current_value > $data[$field_prefix . 'Max']) { + $data[$field_prefix . 'Max'] = $current_value; + } + } + + function logSlowQuery($slow_sql, $time) + { + $query_crc = crc32($slow_sql); + + $sql = 'SELECT * + FROM ' . TABLE_PREFIX . 'SlowSqlCapture + WHERE QueryCrc = ' . $query_crc; + $data = $this->Conn->Query($sql, null, true); + + if ($data) { + $this->_updateAverageStatistics($data, 'Time', $time); + + $template_names = explode(',', $data['TemplateNames']); + array_push($template_names, $this->GetVar('t')); + $data['TemplateNames'] = implode(',', array_unique($template_names)); + + $data['Hits']++; + + $this->Conn->doUpdate($data, TABLE_PREFIX . 'SlowSqlCapture', 'CaptureId = ' . $data['CaptureId']); + } + else { + $data['TimeMin'] = $data['TimeAvg'] = $data['TimeMax'] = $time; + $data['SqlQuery'] = $slow_sql; + $data['QueryCrc'] = $query_crc; + $data['TemplateNames'] = $this->GetVar('t'); + $data['Hits'] = 1; + + $this->Conn->doInsert($data, TABLE_PREFIX . 'SlowSqlCapture'); + } + } + + /** * Checks if output compression options is available * * @return string