Skip to content

Commit

Permalink
MDL-15273 basic read/write perf counter in moodle_database
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jun 16, 2008
1 parent 45d4986 commit ab130a0
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 27 deletions.
16 changes: 6 additions & 10 deletions admin/cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,15 @@
if (function_exists($cron_function)) {
mtrace("Processing module function $cron_function ...", '');
$pre_dbqueries = null;
if (!empty($PERF->dbqueries)) {
$pre_dbqueries = $PERF->dbqueries;
$pre_time = microtime(1);
}
$pre_dbqueries = $DB->perf_get_reads();
$pre_time = microtime(1);
if ($cron_function()) {
if (!$DB->set_field("modules", "lastcron", $timenow, array("id"=>$mod->id))) {
mtrace("Error: could not update timestamp for $mod->fullname");
}
}
if (isset($pre_dbqueries)) {
mtrace("... used " . ($PERF->dbqueries - $pre_dbqueries) . " dbqueries");
mtrace("... used " . ($DB->perf_get_reads() - $pre_dbqueries) . " dbqueries");
mtrace("... used " . (microtime(1) - $pre_time) . " seconds");
}
/// Reset possible changes by modules to time_limit. MDL-11597
Expand Down Expand Up @@ -173,13 +171,11 @@
$cronfunction = 'report_'.$report.'_cron';
mtrace('Processing cron function for '.$report.'...', '');
$pre_dbqueries = null;
if (!empty($PERF->dbqueries)) {
$pre_dbqueries = $PERF->dbqueries;
$pre_time = microtime(true);
}
$pre_dbqueries = $DB->perf_get_reads();
$pre_time = microtime(true);
$cronfunction();
if (isset($pre_dbqueries)) {
mtrace("... used " . ($PERF->dbqueries - $pre_dbqueries) . " dbqueries");
mtrace("... used " . ($DB->perf_get_reads() - $pre_dbqueries) . " dbqueries");
mtrace("... used " . round(microtime(true) - $pre_time, 2) . " seconds");
}
mtrace('done.');
Expand Down
26 changes: 23 additions & 3 deletions lib/dml/adodb_moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ public function dispose() {
parent::dispose();
}

//TODO: make all dblibraries return this info in a structured way (new server_info class or so, like database_column_info class)
/**
* Returns database server info array
* @return array
*/
public function get_server_info() {
//TODO: make all dblibraries return this info in a structured way (new server_info class or so, like database_column_info class)
return $this->adodb->ServerInfo();
}

Expand All @@ -126,6 +126,7 @@ public function get_tables() {
* @return array of arrays
*/
public function get_indexes($table) {
$this->reads++;
if (!$indexes = $this->adodb->MetaIndexes($this->prefix.$table)) {
return array();
}
Expand All @@ -145,6 +146,7 @@ public function get_columns($table, $usecache=true) {
return $this->columns[$table];
}

$this->reads++;
if (!$columns = $this->adodb->MetaColumns($this->prefix.$table)) {
return array();
}
Expand Down Expand Up @@ -203,6 +205,8 @@ public function set_logging($state) {
* @return bool success
*/
public function change_database_structure($sql) {
$this->writes++;

if ($rs = $this->adodb->Execute($sql)) {
$result = true;
} else {
Expand All @@ -222,14 +226,15 @@ public function change_database_structure($sql) {
* @return bool success
*/
public function execute($sql, array $params=null) {

list($sql, $params, $type) = $this->fix_sql_params($sql, $params);

if (strpos($sql, ';') !== false) {
debugging('Error: Multiple sql statements found or bound parameters not used properly in query!');
return false;
}

$this->writes++;

if ($rs = $this->adodb->Execute($sql, $params)) {
$result = true;
$rs->Close();
Expand All @@ -240,7 +245,6 @@ public function execute($sql, array $params=null) {
return $result;
}

//TODO: do we want the *_raw() functions being public? I see the benefits but... won't that cause problems. To decide.
/**
* Insert new record into database, as fast as possible, no safety checks, lobs not supported.
* @param string $table name
Expand All @@ -250,6 +254,8 @@ public function execute($sql, array $params=null) {
* @return mixed success or new id
*/
public function insert_record_raw($table, $params, $returnid=true, $bulk=false) {
//TODO: do we want the *_raw() functions being public? I see the benefits but... won't that cause problems. To decide.

if (!is_array($params)) {
$params = (array)$params;
}
Expand All @@ -259,6 +265,8 @@ public function insert_record_raw($table, $params, $returnid=true, $bulk=false)
return false;
}

$this->writes++;

$fields = implode(',', array_keys($params));
$qms = array_fill(0, count($params), '?');
$qms = implode(',', $qms);
Expand Down Expand Up @@ -299,6 +307,8 @@ public function update_record_raw($table, $params, $bulk=false) {
return false;
}

$this->writes++;

$sets = array();
foreach ($params as $field=>$value) {
$sets[] = "$field = ?";
Expand Down Expand Up @@ -332,6 +342,8 @@ public function delete_records_select($table, $select, array $params=null) {

list($sql, $params, $type) = $this->fix_sql_params($sql, $params);

$this->writes++;

$result = false;
if ($rs = $this->adodb->Execute($sql, $params)) {
$result = true;
Expand Down Expand Up @@ -359,6 +371,8 @@ public function delete_records_select($table, $select, array $params=null) {
public function get_recordset_sql($sql, array $params=null, $limitfrom=0, $limitnum=0) {
list($sql, $params, $type) = $this->fix_sql_params($sql, $params);

$this->reads++;

if ($limitfrom || $limitnum) {
///Special case, 0 must be -1 for ADOdb
$limitfrom = empty($limitfrom) ? -1 : $limitfrom;
Expand Down Expand Up @@ -394,6 +408,9 @@ protected function create_recordset($rs) {
*/
public function get_records_sql($sql, array $params=null, $limitfrom=0, $limitnum=0) {
list($sql, $params, $type) = $this->fix_sql_params($sql, $params);

$this->reads++;

if ($limitfrom || $limitnum) {
///Special case, 0 must be -1 for ADOdb
$limitfrom = empty($limitfrom) ? -1 : $limitfrom;
Expand All @@ -420,6 +437,9 @@ public function get_records_sql($sql, array $params=null, $limitfrom=0, $limitnu
*/
public function get_fieldset_sql($sql, array $params=null) {
list($sql, $params, $type) = $this->fix_sql_params($sql, $params);

$this->reads++;

if (!$rs = $this->adodb->Execute($sql, $params)) {
$this->report_error($sql, $params);
return false;
Expand Down
19 changes: 18 additions & 1 deletion lib/dml/moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,16 @@ abstract class moodle_database {
*/
protected $dboptions;

// TODO: perf stuff goes here
/**
* The database reads (performance counter).
*/
protected $reads = 0;

/**
* The database writes (performance counter).
*/
protected $writes = 0;

// TODO: do we really need record caching??

/**
Expand Down Expand Up @@ -1393,4 +1402,12 @@ public function commit_sql() {
public function rollback_sql() {
return true;
}

public function perf_get_reads() {
return $this->reads;
}

public function perf_get_writes() {
return $this->writes;
}
}
9 changes: 7 additions & 2 deletions lib/dml/mssql_adodb_moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ protected function configure_dbconnection() {

/// No need to set charset. It must be specified in the driver conf
/// Allow quoted identifiers
$this->adodb->Execute('SET QUOTED_IDENTIFIER ON');
$this->adodb->Execute('SET QUOTED_IDENTIFIER ON');
/// Force ANSI nulls so the NULL check was done by IS NULL and NOT IS NULL
/// instead of equal(=) and distinct(<>) simbols
$this->adodb->Execute('SET ANSI_NULLS ON');
$this->adodb->Execute('SET ANSI_NULLS ON');

return true;
}
Expand Down Expand Up @@ -192,6 +192,7 @@ public function update_record($table, $dataobject, $bulk=false) {
}

foreach ($blobs as $key=>$value) {
$this->writes++;
if (!$this->adodb->UpdateBlob($this->prefix.$table, $key, $value, "id = {$dataobject->id}")) {
return false;
}
Expand Down Expand Up @@ -223,6 +224,7 @@ public function set_field_select($table, $newfield, $newvalue, $select, array $p
if ($column->meta_type == 'B') { /// If the column is a BLOB (IMAGE)
/// Update BLOB column and return
$select = $this->emulate_bound_params($select, $params); // adodb does not use bound parameters for blob updates :-(
$this->writes++;
return $this->adodb->UpdateBlob($this->prefix.$table, $newfield, $newvalue, $select);
}

Expand All @@ -243,6 +245,8 @@ public function set_field_select($table, $newfield, $newvalue, $select, array $p
}
$sql = "UPDATE {$this->prefix}$table SET $newfield WHERE $select";

$this->writes++;

if (!$rs = $this->adodb->Execute($sql, $params)) {
$this->report_error($sql, $params);
return false;
Expand Down Expand Up @@ -309,6 +313,7 @@ public function insert_record($table, $dataobject, $returnid=true, $bulk=false)


foreach ($blobs as $key=>$value) {
$this->writes++;
if (!$this->adodb->UpdateBlob($this->prefix.$table, $key, $value, "id = $id")) {
return false;
}
Expand Down
5 changes: 5 additions & 0 deletions lib/dml/mysqli_adodb_moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ protected function allowed_param_types() {
* @return bool true if db in unicode mode
*/
function setup_is_unicodedb() {
$this->reads++;
$rs = $this->adodb->Execute("SHOW LOCAL VARIABLES LIKE 'character_set_database'");
if ($rs && !$rs->EOF) {
$records = $rs->GetAssoc(true);
Expand All @@ -111,8 +112,10 @@ function setup_is_unicodedb() {
*/
public function change_db_encoding() {
// try forcing utf8 collation, if mysql db and no tables present
$this->reads++;
if (!$this->adodb->Metatables()) {
$SQL = 'ALTER DATABASE '.$this->dbname.' CHARACTER SET utf8';
$this->writes++;
$this->adodb->Execute($SQL);
if ($this->setup_is_unicodedb()) {
$this->configure_dbconnection();
Expand Down Expand Up @@ -242,6 +245,8 @@ public function set_field_select($table, $newfield, $newvalue, $select, array $p
}
$sql = "UPDATE {$this->prefix}$table SET $newfield $select";

$this->writes++;

if (!$rs = $this->adodb->Execute($sql, $params)) {
$this->report_error($sql, $params);
return false;
Expand Down
13 changes: 12 additions & 1 deletion lib/dml/oci8po_adodb_moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ protected function allowed_param_types() {
* @return bool true if db in unicode mode
*/
function setup_is_unicodedb() {
$this->reads++;
$rs = $this->adodb->Execute("SELECT parameter, value FROM nls_database_parameters where parameter = 'NLS_CHARACTERSET'");
if ($rs && !$rs->EOF) {
$encoding = $rs->fields['value'];
Expand Down Expand Up @@ -182,7 +183,7 @@ public function sql_isempty($tablename, $fieldname, $nullablefield, $textfield)
return " $fieldname IS NULL "; /// empties in nullable fields are stored as
} else { /// NULLs
if ($textfield) {
return " ".sql_compare_text($fieldname)." = ' ' "; /// oracle_dirty_hack inserts 1-whitespace
return " ".$this->sql_compare_text($fieldname)." = ' ' "; /// oracle_dirty_hack inserts 1-whitespace
} else { /// in NOT NULL varchar and text columns so
return " $fieldname = ' ' "; /// we need to look for that in any situation
}
Expand Down Expand Up @@ -267,12 +268,14 @@ public function update_record($table, $dataobject, $bulk=false) {
}

foreach ($blobs as $key=>$value) {
$this->writes++;
if (!$this->adodb->UpdateBlob($this->prefix.$table, $key, $value, "id = {$dataobject->id}")) {
return false;
}
}

foreach ($clobs as $key=>$value) {
$this->writes++;
if (!$this->adodb->UpdateClob($this->prefix.$table, $key, $value, "id = {$dataobject->id}")) {
return false;
}
Expand Down Expand Up @@ -350,12 +353,14 @@ public function insert_record($table, $dataobject, $returnid=true, $bulk=false)
}

foreach ($blobs as $key=>$value) {
$this->writes++;
if (!$this->adodb->UpdateBlob($this->prefix.$table, $key, $value, "id = $id")) {
return false;
}
}

foreach ($clobs as $key=>$value) {
$this->writes++;
if (!$this->adodb->UpdateClob($this->prefix.$table, $key, $value, "id = $id")) {
return false;
}
Expand Down Expand Up @@ -391,12 +396,14 @@ public function set_field_select($table, $newfield, $newvalue, $select, array $p
if ($column->meta_type == 'B') { /// If the column is a BLOB
/// Update BLOB column and return
$select = $this->emulate_bound_params($select, $params); // adodb does not use bound parameters for blob updates :-(
$this->writes++;
return $this->adodb->UpdateBlob($this->prefix.$table, $newfield, $newvalue, $select);
}

if ($column->meta_type == 'X' && strlen($newvalue) > 4000) { /// If the column is a CLOB with lenght > 4000
/// Update BLOB column and return
$select = $this->emulate_bound_params($select, $params); // adodb does not use bound parameters for blob updates :-(
$this->writes++;
return $this->adodb->UpdateClob($this->prefix.$table, $newfield, $newvalue, $select);
}

Expand All @@ -417,6 +424,7 @@ public function set_field_select($table, $newfield, $newvalue, $select, array $p
}
$sql = "UPDATE {$this->prefix}$table SET $newfield WHERE $select";

$this->writes++;
if (!$rs = $this->adodb->Execute($sql, $params)) {
$this->report_error($sql, $params);
return false;
Expand All @@ -442,13 +450,15 @@ public function insert_record_raw($table, $params, $returnid=true, $bulk=false)
if ($returnid) {
$dbman = $this->get_manager();
$xmldb_table = new xmldb_table($table);
$this->reads++;
$seqname = $dbman->find_sequence_name($xmldb_table);
if (!$seqname) {
/// Fallback, seqname not found, something is wrong. Inform and use the alternative getNameForObject() method
$generator = $this->get_dbman()->generator;
$generator->setPrefix($this->getPrefix());
$seqname = $generator->getNameForObject($table, 'id', 'seq');
}
$this->reads++;
if ($nextval = $this->adodb->GenID($seqname)) {
$params['id'] = (int)$nextval;
}
Expand Down Expand Up @@ -478,6 +488,7 @@ public function insert_record_raw($table, $params, $returnid=true, $bulk=false)

$sql = "INSERT INTO {$this->prefix}$table ($fields) VALUES($qms)";

$this->writes++;
if (!$rs = $this->adodb->Execute($sql, $params)) {
$this->report_error($sql, $params);
return false;
Expand Down
Loading

0 comments on commit ab130a0

Please sign in to comment.