Permalink
Browse files

MDL-37585 Make $DB->set_debug(true) show run time of each query.

  • Loading branch information...
1 parent a5ec499 commit 74fa94b26a5f0b5adef4f44e67407ac9dda464d0 @timhunt timhunt committed Jan 18, 2013
Showing with 20 additions and 1 deletion.
  1. +20 −1 lib/dml/moodle_database.php
View
21 lib/dml/moodle_database.php
@@ -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);
@@ -521,6 +521,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.
* @param array $conditions The conditions to build the where clause. (must not contain numeric indexes)

0 comments on commit 74fa94b

Please sign in to comment.