Skip to content

Commit

Permalink
MDL-37585 Make $DB->set_debug(true) show run time of each query.
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt committed Jan 21, 2013
1 parent a5ec499 commit 74fa94b
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/dml/moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,14 @@ protected function query_end($result) {
// free memory
$this->last_sql = null;
$this->last_params = null;
$this->print_debug_time();
return;
}

// remember current info, log queries may alter it
$type = $this->last_type;
$sql = $this->last_sql;
$params = $this->last_params;
$time = microtime(true) - $this->last_time;
$error = $this->get_last_error();

$this->query_log($error);
Expand Down Expand Up @@ -520,6 +520,25 @@ protected function print_debug($sql, array $params=null, $obj=null) {
}
}

/**
* Prints the time a query took to run.
* @return void
*/
protected function print_debug_time() {
if (!$this->get_debug()) {
return;
}
$time = microtime(true) - $this->last_time;
$message = "Query took: {$time} seconds.\n";
if (CLI_SCRIPT) {
echo $message;
echo "--------------------------------\n";
} else {
echo s($message);
echo "<hr />\n";
}
}

/**
* Returns the SQL WHERE conditions.
* @param string $table The table name that these conditions will be validated against.
Expand Down

0 comments on commit 74fa94b

Please sign in to comment.