From 74fa94b26a5f0b5adef4f44e67407ac9dda464d0 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Fri, 18 Jan 2013 16:46:16 +0000 Subject: [PATCH] MDL-37585 Make $DB->set_debug(true) show run time of each query. --- lib/dml/moodle_database.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/dml/moodle_database.php b/lib/dml/moodle_database.php index 6ffbcbeb25a..3dc9c55a864 100644 --- a/lib/dml/moodle_database.php +++ b/lib/dml/moodle_database.php @@ -408,6 +408,7 @@ abstract class moodle_database { // free memory $this->last_sql = null; $this->last_params = null; + $this->print_debug_time(); return; } @@ -415,7 +416,6 @@ abstract class moodle_database { $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); @@ -520,6 +520,25 @@ abstract class moodle_database { } } + /** + * 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 "
\n"; + } + } + /** * Returns the SQL WHERE conditions. * @param string $table The table name that these conditions will be validated against. -- 2.43.0