Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 311 and 403]

   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   * Run the unit tests.
  19   *
  20   * A simple test script that sets up test data in the database then
  21   * measures performance of filter_get_active_in_context.
  22   *
  23   * @copyright  2009 Tim Hunt
  24   * @author     N.D.Freear@open.ac.uk, T.J.Hunt@open.ac.uk
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  die(); //TODO: this needs to be rewritten as standard advanced_testcase
  29  
  30  require(__DIR__ . '/../../../config.php');
  31  require_once($CFG->libdir . '/ddllib.php');
  32  
  33  require_login();
  34  $syscontext = context_system::instance();
  35  require_capability('moodle/site:config', $syscontext);
  36  
  37  $baseurl = new moodle_url('/lib/tests/performance/filtersettingsperformancetester.php');
  38  
  39  $title = 'filter_get_active_in_context performance test';
  40  $PAGE->set_url($baseurl);
  41  $PAGE->set_context($syscontext);
  42  $PAGE->navbar->add($title);
  43  $PAGE->set_title($title);
  44  $PAGE->set_heading($title);
  45  echo $OUTPUT->header();
  46  
  47  // Complain if we get this far and $CFG->unittestprefix is not set.
  48  if (empty($CFG->unittestprefix)) {
  49      throw new coding_exception('This page requires $CFG->unittestprefix to be set in config.php.');
  50  }
  51  
  52  $requiredtables = array('context', 'filter_active', 'filter_config');
  53  $realdb = $DB;
  54  $testdb = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary);
  55  $testdb->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->unittestprefix);
  56  $DB = $testdb;
  57  $dbman = $testdb->get_manager();
  58  $issetup = 0;
  59  foreach ($requiredtables as $table) {
  60      if ($dbman->table_exists(new xmldb_table($table))) {
  61          $issetup++;
  62      }
  63  }
  64  
  65  switch (optional_param('action', '', PARAM_ALPHANUMEXT)) {
  66      case 'setup':
  67          require_sesskey();
  68          if ($issetup == 0) {
  69              foreach ($requiredtables as $table) {
  70                  $dbman->install_one_table_from_xmldb_file($CFG->dirroot . '/lib/db/install.xml', $table);
  71                  $issetup++;
  72              }
  73              flush();
  74              populate_test_database($syscontext, 10, 100, 1000, 5000, 5000);
  75              echo $OUTPUT->notification('Test tables created.', 'notifysuccess');
  76          } else if ($issetup == count($requiredtables)) {
  77              echo $OUTPUT->notification('Test tables are already set up.', 'notifysuccess');
  78          } else {
  79              echo $OUTPUT->notification('Something is wrong, please delete the test tables and try again.');
  80          }
  81          break;
  82  
  83      case 'teardown':
  84          require_sesskey();
  85          foreach ($requiredtables as $tablename) {
  86              $table = new xmldb_table($tablename);
  87              if ($dbman->table_exists($table)) {
  88                  $dbman->drop_table($table);
  89              }
  90          }
  91          $issetup = 0;
  92          echo $OUTPUT->notification('Test tables dropped.', 'notifysuccess');
  93          break;
  94  
  95      case 'test':
  96          require_sesskey();
  97          if ($issetup != count($requiredtables)) {
  98              echo $OUTPUT->notification('Something is wrong, please delete the test tables and try again.');
  99          } else {
 100              $contexts = $DB->get_records('context');
 101              $numcalls = 1000;
 102              $basetime = run_tests('noop', $contexts, $numcalls, 0);
 103              run_tests('simple_get_record_by_id', $contexts, $numcalls, $basetime);
 104              run_tests('filter_get_active_in_context', $contexts, $numcalls, $basetime);
 105          }
 106          break;
 107  }
 108  
 109  if ($issetup == count($requiredtables)) {
 110      echo '<p>Total of ' . $DB->count_records('context') . ' contexts, ' .
 111              $DB->count_records('filter_active') . ' filter_active and ' .
 112              $DB->count_records('filter_config') . ' filter_config rows in the database.</p>';
 113  }
 114  
 115  $DB = $realdb;
 116  
 117  echo $OUTPUT->container_start();
 118  
 119  $aurl = new moodle_url($baseurl, array('action' => 'setup', 'sesskey'=>sesskey()));
 120  echo $OUTPUT->single_button($aurl, 'Set up test tables', 'get', array('disabled'=>($issetup > 0)));
 121  
 122  $aurl = new moodle_url($baseurl, array('action' => 'teardown', 'sesskey'=>sesskey()));
 123  echo $OUTPUT->single_button($aurl, 'Drop test tables', 'get', array('disabled'=>($issetup == 0)));
 124  
 125  $aurl = new moodle_url($baseurl, array('action' => 'test', 'sesskey'=>sesskey()));
 126  echo $OUTPUT->single_button($aurl, 'Run tests', 'get', array('disabled'=>($issetup != count($requiredtables))));
 127  
 128  echo $OUTPUT->container_end();
 129  
 130  echo $OUTPUT->footer();
 131  
 132  function noop($context) {
 133  }
 134  
 135  function simple_get_record_by_id($context) {
 136      global $DB;
 137      $DB->get_record('context', array('id' => $context->id));
 138  }
 139  
 140  function run_tests($function, $contexts, $numcalls, $basetime) {
 141      core_php_time_limit::raise(120);
 142      $startime = microtime(true);
 143      for ($j = 0; $j < $numcalls; $j++) {
 144          $function($contexts[array_rand($contexts)]);
 145      }
 146      $duration = microtime(true) - $startime;
 147      print_result_line($duration, $basetime, $numcalls, 'calls to ' . $function);
 148      return $duration;
 149  }
 150  
 151  function print_result_line($duration, $basetime, $numcalls, $action1, $action2 = 'calls per second') {
 152      echo '<p>Time for ' . format_float($numcalls, 0) . ' ' . $action1 . ': <b>' .
 153              format_float($duration - $basetime, 3) . 's</b> (' . format_float($duration, 3) . ' - ' .
 154              format_float($basetime, 3) . 's) which is ' .
 155              format_float(($numcalls / ($duration - $basetime)), 0) . ' ' . $action2 . ".</p>\n";
 156      flush();
 157  }
 158  
 159  function populate_test_database($syscontext, $numcategories, $numcourses, $nummodules, $numoverrides, $numconfigs) {
 160      global $DB, $OUTPUT;
 161      core_php_time_limit::raise(600);
 162      $syscontext->id = $DB->insert_record('context', $syscontext);
 163  
 164      // Category contexts.
 165      $categoryparents = array($syscontext);
 166      $categories = array();
 167      for ($i = 0; $i < $numcategories; $i++) {
 168          $context = insert_context(CONTEXT_COURSECAT, $i, $categoryparents[array_rand($categoryparents)]);
 169          $categoryparents[] = $context;
 170          $categories[$context->id] = $context;
 171      }
 172      echo $OUTPUT->notification('Created ' . $numcategories . ' course category contexts.', 'notifysuccess'); flush();
 173  
 174      // Course contexts.
 175      $courses = array();
 176      for ($i = 0; $i < $numcourses; $i++) {
 177          $context = insert_context(CONTEXT_COURSE, $i, $categories[array_rand($categories)]);
 178          $courses[$context->id] = $context;
 179      }
 180      echo $OUTPUT->notification('Created ' . $numcourses . ' course contexts.', 'notifysuccess'); flush();
 181  
 182      // Activities contexts.
 183      $mods = array();
 184      $prog = new progress_bar('modbar', 500, true);
 185      $transaction = $DB->start_delegated_transaction();
 186      for ($i = 0; $i < $nummodules; $i++) {
 187          $context = insert_context(CONTEXT_MODULE, $i, $courses[array_rand($courses)]);
 188          $mods[$context->id] = $context;
 189          if ($i % 50) {
 190              $prog->update($i, $nummodules, '');
 191          }
 192      }
 193      $transaction->allow_commit();
 194      echo $OUTPUT->notification('Created ' . $nummodules . ' module contexts.', 'notifysuccess'); flush();
 195  
 196      $contexts = $categories + $courses + $mods;
 197  
 198      // Global settings.
 199      $installedfilters = filter_get_all_installed();
 200      $counts = array(TEXTFILTER_DISABLED => 0, TEXTFILTER_OFF => 0, TEXTFILTER_ON => 0);
 201      foreach ($installedfilters as $filter => $notused) {
 202          $state = array_rand($counts);
 203          filter_set_global_state($filter, $state);
 204          $counts[$state]++;
 205      }
 206      echo $OUTPUT->notification('Set global setting: ' . $counts[TEXTFILTER_DISABLED] . ' disabled, ' .
 207              $counts[TEXTFILTER_OFF] . ' off and ' . $counts[TEXTFILTER_ON] . ' on.', 'notifysuccess'); flush();
 208  
 209      // Local overrides.
 210      $localstates = array(TEXTFILTER_OFF => 0, TEXTFILTER_ON => 0);
 211      $prog = new progress_bar('locbar', 500, true);
 212      $transaction = $DB->start_delegated_transaction();
 213      for ($i = 0; $i < $numoverrides; $i++) {
 214          filter_set_local_state(array_rand($installedfilters), array_rand($contexts), array_rand($localstates));
 215          if ($i % 50) {
 216              $prog->update($i, $numoverrides, '');
 217          }
 218      }
 219      $transaction->allow_commit();
 220      echo $OUTPUT->notification('Set ' . $numoverrides . ' local overrides.', 'notifysuccess'); flush();
 221  
 222      // Local config.
 223      $variablenames = array('frog' => 0, 'toad' => 0, 'elver' => 0, 'eft' => 0, 'tadpole' => 0);
 224      $prog = new progress_bar('confbar', 500, true);
 225      $transaction = $DB->start_delegated_transaction();
 226      for ($i = 0; $i < $numconfigs; $i++) {
 227          filter_set_local_config(array_rand($installedfilters), array_rand($contexts),
 228                  array_rand($variablenames), random_string(rand(20, 40)));
 229          if ($i % 50) {
 230              $prog->update($i, $numconfigs, '');
 231          }
 232      }
 233      $transaction->allow_commit();
 234      echo $OUTPUT->notification('Set ' . $numconfigs . ' local configs.', 'notifysuccess'); flush();
 235  }
 236  
 237  function insert_context($contextlevel, $instanceid, $parent) {
 238      global $DB;
 239      $context = new stdClass;
 240      $context->contextlevel = $contextlevel;
 241      $context->instanceid = $instanceid;
 242      $context->depth = $parent->depth + 1;
 243      $context->id = $DB->insert_record('context', $context);
 244      $context->path = $parent->path . '/' . $context->id;
 245      $DB->set_field('context', 'path', $context->path, array('id' => $context->id));
 246      return $context;
 247  }
 248