From 03440bcb37ec476eaa6d94bcdfe3a6e58aa1158a Mon Sep 17 00:00:00 2001 From: sam marshall Date: Tue, 11 Apr 2017 11:01:42 +0100 Subject: [PATCH] MDL-58567 Upgrade: Show upgrade times --- lang/en/moodle.php | 1 + lib/upgradelib.php | 62 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 196ebde7e8f..07e154d24c6 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -1843,6 +1843,7 @@ $string['subcategory'] = 'Subcategory'; $string['subcategoryof'] = 'Subcategory of {$a}'; $string['submit'] = 'Submit'; $string['success'] = 'Success'; +$string['successduration'] = 'Success ({$a} seconds)'; $string['summary'] = 'Summary'; $string['summary_help'] = 'The idea of a summary is a short text to prepare students for the activities within the topic or week. The text is shown on the course page under the section name.'; $string['summaryof'] = 'Summary of {$a}'; diff --git a/lib/upgradelib.php b/lib/upgradelib.php index fc81446d97f..cf4f53b372b 100644 --- a/lib/upgradelib.php +++ b/lib/upgradelib.php @@ -137,6 +137,54 @@ class plugin_misplaced_exception extends moodle_exception { } } +/** + * Static class monitors performance of upgrade steps. + */ +class core_upgrade_time { + /** @var float Time at start of current upgrade (plugin/system) */ + protected static $before; + /** @var float Time at end of last savepoint */ + protected static $lastsavepoint; + + /** + * Records current time at the start of the current upgrade item, e.g. plugin. + */ + public static function record_start() { + self::$before = microtime(true); + self::$lastsavepoint = self::$before; + } + + /** + * Records current time at the end of a given numbered step. + * + * @param float $version Version number (may have decimals, or not) + */ + public static function record_savepoint($version) { + global $CFG, $OUTPUT; + + // In developer debug mode we show a notification after each individual save point. + if ($CFG->debugdeveloper) { + $time = microtime(true); + + $notification = new \core\output\notification($version . ': ' . + get_string('successduration', '', format_float($time - self::$lastsavepoint, 2)), + \core\output\notification::NOTIFY_SUCCESS); + $notification->set_show_closebutton(false); + echo $OUTPUT->render($notification); + self::$lastsavepoint = $time; + } + } + + /** + * Gets the time since the record_start function was called, rounded to 2 digits. + * + * @return float Elapsed time + */ + public static function get_elapsed() { + return microtime(true) - self::$before; + } +} + /** * Sets maximum expected time needed for upgrade task. * Please always make sure that upgrade will not run longer! @@ -224,6 +272,8 @@ function upgrade_main_savepoint($result, $version, $allowabort=true) { // reset upgrade timeout to default upgrade_set_timeout(); + core_upgrade_time::record_savepoint($version); + // this is a safe place to stop upgrades if user aborts page loading if ($allowabort and connection_aborted()) { die; @@ -268,6 +318,8 @@ function upgrade_mod_savepoint($result, $version, $modname, $allowabort=true) { // reset upgrade timeout to default upgrade_set_timeout(); + core_upgrade_time::record_savepoint($version); + // this is a safe place to stop upgrades if user aborts page loading if ($allowabort and connection_aborted()) { die; @@ -312,6 +364,8 @@ function upgrade_block_savepoint($result, $version, $blockname, $allowabort=true // reset upgrade timeout to default upgrade_set_timeout(); + core_upgrade_time::record_savepoint($version); + // this is a safe place to stop upgrades if user aborts page loading if ($allowabort and connection_aborted()) { die; @@ -352,6 +406,8 @@ function upgrade_plugin_savepoint($result, $version, $type, $plugin, $allowabort // Reset upgrade timeout to default upgrade_set_timeout(); + core_upgrade_time::record_savepoint($version); + // This is a safe place to stop upgrades if user aborts page loading if ($allowabort and connection_aborted()) { die; @@ -1501,6 +1557,7 @@ function print_upgrade_part_start($plugin, $installation, $verbose) { echo $OUTPUT->heading($plugin); } } + core_upgrade_time::record_start(); if ($installation) { if (empty($plugin) or $plugin == 'moodle') { // no need to log - log table not yet there ;-) @@ -1538,7 +1595,10 @@ function print_upgrade_part_end($plugin, $installation, $verbose) { } } if ($verbose) { - $notification = new \core\output\notification(get_string('success'), \core\output\notification::NOTIFY_SUCCESS); + $duration = core_upgrade_time::get_elapsed(); + $notification = new \core\output\notification( + get_string('successduration', '', format_float($duration, 2)), + \core\output\notification::NOTIFY_SUCCESS); $notification->set_show_closebutton(false); echo $OUTPUT->render($notification); print_upgrade_separator();