Commit bd5535a9 authored by Richard Mansfield's avatar Richard Mansfield

Move query counting inside adodb to count queries that were being missed

Signed-off-by: default avatarRichard Mansfield <richard.mansfield@catalyst.net.nz>
parent 0e7a938a
......@@ -296,7 +296,7 @@ function init_performance_info() {
global $PERF;
$PERF = new StdClass;
$PERF->dbreads = $PERF->dbwrites = 0;
$PERF->dbreads = $PERF->dbwrites = $PERF->dbcached = 0;
$PERF->logwrites = 0;
if (function_exists('microtime')) {
$PERF->starttime = microtime();
......
......@@ -34,6 +34,7 @@ $string['included'] = 'Included files';
$string['dbqueries'] = 'DB queries';
$string['reads'] = 'reads';
$string['writes'] = 'writes';
$string['cached'] = 'cached';
$string['ticks'] = 'ticks';
$string['sys'] = 'sys';
$string['user'] = 'user';
......
......@@ -154,13 +154,6 @@ function execute_sql($command, $values=null) {
else {
$result = $db->Execute($command);
}
// searching for these rather than just select as subqueries may have select in them.
if (preg_match('/(update|insert|delete|alter|create)/i', $command)) {
increment_perf_db_writes();
}
else {
increment_perf_db_reads();
}
}
catch (ADODB_Exception $e) {
log_debug($e->getMessage() . "Command was: $command");
......@@ -477,10 +470,8 @@ function get_recordset_sql($sql, $values=null, $limitfrom=null, $limitnum=null)
if (!empty($values) && is_array($values) && count($values) > 0) {
$stmt = $db->Prepare($sql);
$rs = $db->Execute($stmt, $values);
increment_perf_db_reads();
} else {
$rs = $db->Execute($sql);
increment_perf_db_reads();
}
}
}
......@@ -895,7 +886,6 @@ function set_field_select($table, $newfield, $newvalue, $select, $values) {
$sql = 'UPDATE '. db_table_name($table) .' SET '. db_quote_identifier($newfield) .' = ? ' . $select;
try {
$stmt = $db->Prepare($sql);
increment_perf_db_writes();
return $db->Execute($stmt, $values);
}
catch (ADODB_Exception $e) {
......@@ -931,7 +921,6 @@ function delete_records($table, $field1=null, $value1=null, $field2=null, $value
$sql = 'DELETE FROM '. db_table_name($table) . ' ' . $select;
try {
$stmt = $db->Prepare($sql);
increment_perf_db_writes();
return $db->Execute($stmt,$values);
}
catch (ADODB_Exception $e) {
......@@ -970,10 +959,8 @@ function delete_records_sql($sql, $values=null) {
$result = false;
if (!empty($values) && is_array($values) && count($values) > 0) {
$stmt = $db->Prepare($sql);
increment_perf_db_writes();
$result = $db->Execute($stmt, $values);
} else {
increment_perf_db_writes();
$result = $db->Execute($sql);
}
}
......@@ -1062,7 +1049,6 @@ function insert_record($table, $dataobject, $primarykey=false, $returnpk=false)
// Run the SQL statement
try {
$stmt = $db->Prepare($insertSQL);
increment_perf_db_writes();
$rs = $db->Execute($stmt,$ddd);
}
catch (ADODB_Exception $e) {
......@@ -1089,7 +1075,6 @@ function insert_record($table, $dataobject, $primarykey=false, $returnpk=false)
// try to get the primary key based on id
try {
$oidsql = 'SELECT ' . $primarykey . ' FROM '. db_table_name($table) . ' WHERE oid = ' . $id;
increment_perf_db_reads();
$rs = $db->Execute($oidsql);
if ($rs->RecordCount() == 1) {
return (integer)$rs->fields[0];
......@@ -1276,7 +1261,6 @@ function update_record($table, $dataobject, $where=null) {
$sql = 'UPDATE '. db_table_name($table) .' SET '. $update .' WHERE ' . $whereclause;
try {
$stmt = $db->Prepare($sql);
increment_perf_db_writes();
$rs = $db->Execute($stmt,array_merge($values, $wherevalues));
return true;
}
......@@ -1373,7 +1357,6 @@ function where_values_prepared($value1=null, $value2=null, $value3=null, $value4
function column_type($table, $column) {
global $db;
increment_perf_db_reads();
if(!$rs = $db->Execute('SELECT ' . $column.' FROM ' . db_table_name($table) . ' WHERE 1=2')) {
return false;
}
......@@ -1475,7 +1458,12 @@ function db_format_tsfield($field, $as = null) {
* and some other variables.
*/
function configure_dbconnection() {
global $db;
global $db, $CFG;
if (!empty($CFG->perftolog) || !empty($CFG->perftofoot)) {
$db->fnExecute = 'increment_perf_db';
$db->fnCacheExecute = 'increment_perf_db_cached';
}
$db->Execute("SET NAMES 'utf8'");
......@@ -1575,20 +1563,24 @@ function create_sql_exception_message($e, $sql, $values) {
return $message;
}
function increment_perf_db_reads() {
if (!get_config('perftolog') && !get_config('perftofoot')) {
return true;
}
function &increment_perf_db($db, $sql, $inputarray) {
global $PERF;
$PERF->dbreads++;
}
function increment_perf_db_writes() {
if (!get_config('perftolog') && !get_config('perftofoot')) {
return true;
// searching for these rather than just select as subqueries may have select in them.
if (preg_match('/^(update|insert|delete|alter|create)/i', trim($sql))) {
$PERF->dbwrites++;
}
else {
$PERF->dbreads++;
}
$null = null;
return $null;
}
function increment_perf_db_cached($db, $secs2cache, $sql, $inputarray) {
global $PERF;
$PERF->dbwrites++;
$PERF->dbcached++;
}
/**
......
......@@ -1962,6 +1962,7 @@ function get_performance_info() {
$info['dbreads'] = $PERF->dbreads;
$info['dbwrites'] = $PERF->dbwrites;
$info['dbcached'] = $PERF->dbcached;
if (function_exists('posix_times')) {
$ptimes = posix_times();
......@@ -2008,7 +2009,7 @@ function perf_to_log($info=null) {
$logstring .= ' memory_total: '.$info['memory_total'].'B (' . display_size($info['memory_total']).') memory_growth: '.$info['memory_growth'].'B ('.display_size($info['memory_growth']).')';
$logstring .= ' time: '.$info['realtime'].'s';
$logstring .= ' includecount: '.$info['includecount'];
$logstring .= ' dbqueries: '.$info['dbreads'] . ' reads, ' . $info['dbwrites'] . ' writes';
$logstring .= ' dbqueries: '.$info['dbreads'] . ' reads, ' . $info['dbwrites'] . ' writes, ' . $info['dbcached'] . ' cached';
$logstring .= ' ticks: ' . $info['ticks'] . ' user: ' . $info['utime'] . ' sys: ' . $info['stime'] .' cuser: ' . $info['cutime'] . ' csys: ' . $info['cstime'];
$logstring .= ' serverload: ' . $info['serverload'];
log_debug($logstring);
......
......@@ -8,8 +8,8 @@
{if $perf_includecount}
<span id="included">{str tag="included" section="performance"}: {$perf_includecount}</span> |
{/if}
{if $perf_dbreads || $perf_dbwrites}
<span id="dbqueries">{str tag="dbqueries" section="performance"}: {$perf_dbreads} {str tag='reads' section='performance'}, {$perf_dbwrites} {str tag='writes' section='performance'}</span> |
{if $perf_dbreads || $perf_dbwrites || $perf_dbcached}
<span id="dbqueries">{str tag="dbqueries" section="performance"}: {$perf_dbreads} {str tag='reads' section='performance'}, {$perf_dbwrites} {str tag='writes' section='performance'}, {$perf_dbcached} {str tag='cached' section='performance'}</span> |
{/if}
{if $perf_ticks}
<span id="posixtimes">{str tag="ticks" section="performance"}: {$perf_ticks} {str tag="user" section="performance"}: {$perf_utime}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment