Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Store performance test run + output script.
  19   *
  20   * @package    core
  21   * @category   cache
  22   * @copyright  2012 Sam Hemelryk
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require_once('../config.php');
  27  require_once($CFG->dirroot.'/lib/adminlib.php');
  28  require_once($CFG->dirroot.'/cache/locallib.php');
  29  
  30  $count = optional_param('count', 100, PARAM_INT);
  31  $count = min($count, 100000);
  32  $count = max($count, 0);
  33  
  34  admin_externalpage_setup('cachetestperformance');
  35  
  36  $applicationtable = new html_table();
  37  $applicationtable->head = array(
  38      get_string('plugin', 'cache'),
  39      get_string('result', 'cache'),
  40      get_string('set', 'cache'),
  41      get_string('gethit', 'cache'),
  42      get_string('getmiss', 'cache'),
  43      get_string('delete', 'cache'),
  44  );
  45  $applicationtable->data = array();
  46  $sessiontable = clone($applicationtable);
  47  $requesttable = clone($applicationtable);
  48  
  49  
  50  $application = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cache', 'applicationtest');
  51  $session = cache_definition::load_adhoc(cache_store::MODE_SESSION, 'cache', 'sessiontest');
  52  $request = cache_definition::load_adhoc(cache_store::MODE_REQUEST, 'cache', 'requesttest');
  53  
  54  $strinvalidplugin = new lang_string('invalidplugin', 'cache');
  55  $strunsupportedmode = new lang_string('unsupportedmode', 'cache');
  56  $struntestable = new lang_string('untestable', 'cache');
  57  $strtested = new lang_string('tested', 'cache');
  58  $strnotready = new lang_string('storenotready', 'cache');
  59  
  60  foreach (core_component::get_plugin_list_with_file('cachestore', 'lib.php', true) as $plugin => $path) {
  61  
  62      $class = 'cachestore_'.$plugin;
  63      $plugin = get_string('pluginname', 'cachestore_'.$plugin);
  64  
  65      if (!class_exists($class) || !method_exists($class, 'initialise_test_instance') || !$class::are_requirements_met()) {
  66          $applicationtable->data[] = array($plugin, $strinvalidplugin, '-', '-', '-', '-');
  67          $sessiontable->data[] = array($plugin, $strinvalidplugin, '-', '-', '-', '-');
  68          $requesttable->data[] = array($plugin, $strinvalidplugin, '-', '-', '-', '-');
  69          continue;
  70      }
  71  
  72      if (!$class::is_supported_mode(cache_store::MODE_APPLICATION)) {
  73          $applicationtable->data[] = array($plugin, $strunsupportedmode, '-', '-', '-', '-');
  74      } else {
  75          $store = $class::initialise_test_instance($application);
  76          if ($store === false) {
  77              $applicationtable->data[] = array($plugin, $struntestable, '-', '-', '-', '-');
  78          } else if (!$store->is_ready()) {
  79              $applicationtable->data[] = array($plugin, $strnotready, '-', '-', '-', '-');
  80          } else {
  81              $result = array($plugin, $strtested, 0, 0, 0);
  82              $start = microtime(true);
  83              for ($i = 0; $i < $count; $i++) {
  84                  $store->set('key'.$i, 'test data '.$i);
  85              }
  86              $result[2] = sprintf('%01.4f', microtime(true) - $start);
  87  
  88              $start = microtime(true);
  89              for ($i = 0; $i < $count; $i++) {
  90                  $store->get('key'.$i);
  91              }
  92              $result[3] = sprintf('%01.4f', microtime(true) - $start);
  93  
  94              $start = microtime(true);
  95              for ($i = 0; $i < $count; $i++) {
  96                  $store->get('fake'.$i);
  97              }
  98              $result[4] = sprintf('%01.4f', microtime(true) - $start);
  99  
 100              $start = microtime(true);
 101              for ($i = 0; $i < $count; $i++) {
 102                  $store->delete('key'.$i);
 103              }
 104              $result[5] = sprintf('%01.4f', microtime(true) - $start);
 105              $applicationtable->data[] = $result;
 106              $store->instance_deleted();
 107          }
 108      }
 109  
 110      if (!$class::is_supported_mode(cache_store::MODE_SESSION)) {
 111          $sessiontable->data[] = array($plugin, $strunsupportedmode, '-', '-', '-', '-');
 112      } else {
 113          $store = $class::initialise_test_instance($session);
 114          if ($store === false) {
 115              $sessiontable->data[] = array($plugin, $struntestable, '-', '-', '-', '-');
 116          } else if (!$store->is_ready()) {
 117              $sessiontable->data[] = array($plugin, $strnotready, '-', '-', '-', '-');
 118          } else {
 119              $result = array($plugin, $strtested, 0, 0, 0);
 120              $start = microtime(true);
 121              for ($i = 0; $i < $count; $i++) {
 122                  $store->set('key'.$i, 'test data '.$i);
 123              }
 124              $result[2] = sprintf('%01.4f', microtime(true) - $start);
 125  
 126              $start = microtime(true);
 127              for ($i = 0; $i < $count; $i++) {
 128                  $store->get('key'.$i);
 129              }
 130              $result[3] = sprintf('%01.4f', microtime(true) - $start);
 131  
 132              $start = microtime(true);
 133              for ($i = 0; $i < $count; $i++) {
 134                  $store->get('fake'.$i);
 135              }
 136              $result[4] = sprintf('%01.4f', microtime(true) - $start);
 137  
 138              $start = microtime(true);
 139              for ($i = 0; $i < $count; $i++) {
 140                  $store->delete('key'.$i);
 141              }
 142              $result[5] = sprintf('%01.4f', microtime(true) - $start);
 143              $sessiontable->data[] = $result;
 144              $store->instance_deleted();
 145          }
 146      }
 147  
 148      if (!$class::is_supported_mode(cache_store::MODE_REQUEST)) {
 149          $requesttable->data[] = array($plugin, $strunsupportedmode, '-', '-', '-', '-');
 150      } else {
 151          $store = $class::initialise_test_instance($request);
 152          if ($store === false) {
 153              $requesttable->data[] = array($plugin, $struntestable, '-', '-', '-', '-');
 154          } else if (!$store->is_ready()) {
 155              $requesttable->data[] = array($plugin, $strnotready, '-', '-', '-', '-');
 156          } else {
 157              $result = array($plugin, $strtested, 0, 0, 0);
 158              $start = microtime(true);
 159              for ($i = 0; $i < $count; $i++) {
 160                  $store->set('key'.$i, 'test data '.$i);
 161              }
 162              $result[2] = sprintf('%01.4f', microtime(true) - $start);
 163  
 164              $start = microtime(true);
 165              for ($i = 0; $i < $count; $i++) {
 166                  $store->get('key'.$i);
 167              }
 168              $result[3] = sprintf('%01.4f', microtime(true) - $start);
 169  
 170              $start = microtime(true);
 171              for ($i = 0; $i < $count; $i++) {
 172                  $store->get('fake'.$i);
 173              }
 174              $result[4] = sprintf('%01.4f', microtime(true) - $start);
 175  
 176              $start = microtime(true);
 177              for ($i = 0; $i < $count; $i++) {
 178                  $store->delete('key'.$i);
 179              }
 180              $result[5] = sprintf('%01.4f', microtime(true) - $start);
 181              $requesttable->data[] = $result;
 182              $store->instance_deleted();
 183          }
 184      }
 185  
 186  }
 187  
 188  echo $OUTPUT->header();
 189  echo $OUTPUT->heading(get_string('storeperformance', 'cache', $count));
 190  
 191  $possiblecounts = array(1, 10, 100, 500, 1000, 5000, 10000, 50000, 100000);
 192  $links = array();
 193  foreach ($possiblecounts as $pcount) {
 194      $links[] = html_writer::link(new moodle_url($PAGE->url, array('count' => $pcount)), $pcount);
 195  }
 196  echo $OUTPUT->box_start('generalbox performance-test-counts');
 197  echo get_string('requestcount', 'cache', join(', ', $links));
 198  echo $OUTPUT->box_end();
 199  
 200  echo $OUTPUT->heading(get_string('storeresults_application', 'cache'));
 201  echo html_writer::table($applicationtable);
 202  
 203  echo $OUTPUT->heading(get_string('storeresults_session', 'cache'));
 204  echo html_writer::table($sessiontable);
 205  
 206  echo $OUTPUT->heading(get_string('storeresults_request', 'cache'));
 207  echo html_writer::table($requesttable);
 208  
 209  echo $OUTPUT->footer();