From a922209e84aff78de26850ea209170a431a6ce2e Mon Sep 17 00:00:00 2001 From: David Monllao Date: Mon, 20 Jan 2014 13:34:26 +0800 Subject: [PATCH] MDL-43758 performance: New metric, time spent by the database This patch adds a new performance metric to the performance info shown by MDL_PERF* vars, the time spent by the database, it was one of the wonderful @poltawski ideas. To be more specific the value displayed is the sum of the time elapsed between query_start() and query_end(). --- lib/dml/moodle_database.php | 25 +++++++++++++++++++++++-- lib/moodlelib.php | 4 ++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/dml/moodle_database.php b/lib/dml/moodle_database.php index 070f0cb9eff..67dce39c57f 100644 --- a/lib/dml/moodle_database.php +++ b/lib/dml/moodle_database.php @@ -91,6 +91,8 @@ abstract class moodle_database { protected $reads = 0; /** @var int The database writes (performance counter).*/ protected $writes = 0; + /** @var float Time queries took to finish, seconds with microseconds.*/ + protected $queriestime = 0; /** @var int Debug level. */ protected $debug = 0; @@ -459,7 +461,10 @@ abstract class moodle_database { $logerrors = !empty($this->dboptions['logerrors']); $iserror = ($error !== false); - $time = microtime(true) - $this->last_time; + $time = $this->query_time(); + + // Will be shown or not depending on MDL_PERF values rather than in dboptions['log*]. + $this->queriestime = $this->queriestime + $time; if ($logall or ($logslow and ($logslow < ($time+0.00001))) or ($iserror and $logerrors)) { $this->loggingquery = true; @@ -489,6 +494,14 @@ abstract class moodle_database { } } + /** + * Returns the time elapsed since the query started. + * @return float Seconds with microseconds + */ + protected function query_time() { + return microtime(true) - $this->last_time; + } + /** * Returns database server info array * @return array Array containing 'description' and 'version' at least. @@ -543,7 +556,7 @@ abstract class moodle_database { if (!$this->get_debug()) { return; } - $time = microtime(true) - $this->last_time; + $time = $this->query_time(); $message = "Query took: {$time} seconds.\n"; if (CLI_SCRIPT) { echo $message; @@ -2466,4 +2479,12 @@ abstract class moodle_database { public function perf_get_queries() { return $this->writes + $this->reads; } + + /** + * Time waiting for the database engine to finish running all queries. + * @return float Number of seconds with microseconds + */ + public function perf_get_queries_time() { + return $this->queriestime; + } } diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 312ac4e8502..6a0b96e485a 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -8857,6 +8857,10 @@ function get_performance_info() { $info['html'] .= 'DB reads/writes: '.$info['dbqueries'].' '; $info['txt'] .= 'db reads/writes: '.$info['dbqueries'].' '; + $info['dbtime'] = round($DB->perf_get_queries_time(), 5); + $info['html'] .= 'DB queries time: '.$info['dbtime'].' secs '; + $info['txt'] .= 'db queries time: ' . $info['dbtime'] . 's '; + if (function_exists('posix_times')) { $ptimes = posix_times(); if (is_array($ptimes)) {