Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 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   * Media filter performance test script.
  19   *
  20   * For developer test usage only. This can be used to compare performance if
  21   * there are changes to the system in future.
  22   *
  23   * @copyright 2012 The Open University
  24   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   * @package filter_mediaplugin
  26   */
  27  
  28  require(__DIR__ . '/../../../config.php');
  29  require_once($CFG->dirroot . '/filter/mediaplugin/filter.php');
  30  
  31  // Only available to site admins.
  32  require_login();
  33  if (!is_siteadmin()) {
  34      throw new \moodle_exception('nopermissions', 'error', '', 'perftest');
  35  }
  36  
  37  // Set up page.
  38  $PAGE->set_context(context_system::instance());
  39  $PAGE->set_url(new moodle_url('/filter/mediaplugin/dev/perftest.php'));
  40  $PAGE->set_heading($SITE->fullname);
  41  print $OUTPUT->header();
  42  
  43  // Enable all players.
  44  $enabledmediaplugins = \core\plugininfo\media::get_enabled_plugins();
  45  \core\plugininfo\media::set_enabled_plugins('vimeo,youtube,videojs,html5audio,html5video');
  46  
  47  // Create plugin.
  48  $filterplugin = new filter_mediaplugin(null, array());
  49  
  50  // Note: As this is a developer test page, language strings are not used: all
  51  // text is English-only.
  52  
  53  /**
  54   * Starts time counter.
  55   */
  56  function filter_mediaplugin_perf_start() {
  57      global $filter_mediaplugin_starttime;
  58      $filter_mediaplugin_starttime = microtime(true);
  59  }
  60  
  61  /**
  62   * Ends and displays time counter.
  63   * @param string $name Counter name to display
  64   */
  65  function filter_mediaplugin_perf_stop($name) {
  66      global $filter_mediaplugin_starttime;
  67      $time = microtime(true) - $filter_mediaplugin_starttime;
  68  
  69      echo html_writer::tag('li', $name . ': ' . html_writer::tag('strong', round($time, 2)) . 'ms');
  70  }
  71  
  72  // 1) Some sample text strings.
  73  //    Note: These are from a random sample of real forum data. Just in case there
  74  //    are any privacy concerns I have altered names as may be clear.
  75  $samples = array(
  76      "<p>Hi,</p>&#13;\n<p>I've got myself 2 Heaney's \"The Burial at Thebes\"</p>",
  77      "best mark iv heard so far v v good",
  78      "<p>I have a script draft anyone want to look at it?",
  79      "<p>Thanks for your input Legolas and Ghimli!</p>",
  80      "<p>Just to say that I'm thinking of those of you who are working on TMA02.</p>",
  81      "<p><strong>1.</strong> <strong>If someone asks you 'where do you come from?'</strong></p>",
  82      "<p>With regards to Aragorn's question 'what would we do different'?</p>&#13;\n",
  83      "<p>Just thought I'd drop a line to see how everyone is managing generally?</p>&#13;\n",
  84      "<p>Feb '12 - Oct '12  AA100</p>&#13;\n<p>Nov '12 - April '13 - A150</p>&#13;\n",
  85      "<p>So where does that leave the bible???</p>",
  86  );
  87  
  88  // 2) Combine sample text strings into one really big (20KB) string.
  89  $length = 0;
  90  $bigstring = '';
  91  $index = 0;
  92  while ($length < 20 * 1024) {
  93      $bigstring .= $samples[$index];
  94      $length += strlen($samples[$index]);
  95      $index++;
  96      if ($index >= count($samples)) {
  97          $index = 0;
  98      }
  99  }
 100  
 101  // 3) Make random samples from this. I did the following stats on recent forum
 102  //    posts:
 103  //    0-199 characters approx 30%
 104  //    200-1999 approx 60%
 105  //    2000-19999 approx 10%.
 106  
 107  $samplebank = array();
 108  foreach (array(100 => 300, 1000 => 600, 10000 => 100) as $chars => $num) {
 109      for ($i = 0; $i < $num; $i++) {
 110          $start = rand(0, $length - $chars - 1);
 111          $samplebank[] = substr($bigstring, $start, $chars);
 112      }
 113  }
 114  
 115  echo html_writer::start_tag('ul');
 116  
 117  // First test: filter text that doesn't have any links.
 118  filter_mediaplugin_perf_start();
 119  foreach ($samplebank as $sample) {
 120      $filterplugin->filter($sample);
 121  }
 122  filter_mediaplugin_perf_stop('No links');
 123  
 124  // Second test: filter text with one link added (that doesn't match).
 125  $link = '<a href="http://www.example.org/another/link/">Link</a>';
 126  $linksamples = array();
 127  foreach ($samplebank as $sample) {
 128      // Make it the same length but with $link replacing the end part.
 129      $linksamples[] = substr($sample, 0, -strlen($link)) . $link;
 130  }
 131  
 132  filter_mediaplugin_perf_start();
 133  foreach ($linksamples as $sample) {
 134      $filterplugin->filter($sample);
 135  }
 136  filter_mediaplugin_perf_stop('One link (no match)');
 137  
 138  // Third test: filter text with one link added that does match (mp3).
 139  $link = '<a href="http://www.example.org/another/file.mp3">MP3 audio</a>';
 140  $linksamples = array();
 141  foreach ($samplebank as $sample) {
 142      // Make it the same length but with $link replacing the end part.
 143      $linksamples[] = substr($sample, 0, -strlen($link)) . $link;
 144  }
 145  
 146  filter_mediaplugin_perf_start();
 147  foreach ($linksamples as $sample) {
 148      $filterplugin->filter($sample);
 149  }
 150  filter_mediaplugin_perf_stop('One link (mp3)');
 151  
 152  \core\plugininfo\media::set_enabled_plugins($enabledmediaplugins);
 153  
 154  // End page.
 155  echo html_writer::end_tag('ul');
 156  print $OUTPUT->footer();