[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/filter/mediaplugin/dev/ -> perftest.php (source)

   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      print_error('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/perftest.php'));
  40  $PAGE->set_heading($SITE->fullname);
  41  print $OUTPUT->header();
  42  
  43  // Hack setup to enable all players.
  44  $CFG->core_media_enable_youtube    = 1;
  45  $CFG->core_media_enable_vimeo      = 1;
  46  $CFG->core_media_enable_mp3        = 1;
  47  $CFG->core_media_enable_flv        = 1;
  48  $CFG->core_media_enable_swf        = 1;
  49  $CFG->core_media_enable_html5audio = 1;
  50  $CFG->core_media_enable_html5video = 1;
  51  $CFG->core_media_enable_qt         = 1;
  52  $CFG->core_media_enable_wmp        = 1;
  53  $CFG->core_media_enable_rm         = 1;
  54  
  55  $CFG->filter_mediaplugin_enable_youtube    = 1;
  56  $CFG->filter_mediaplugin_enable_vimeo      = 1;
  57  $CFG->filter_mediaplugin_enable_mp3        = 1;
  58  $CFG->filter_mediaplugin_enable_flv        = 1;
  59  $CFG->filter_mediaplugin_enable_swf        = 1;
  60  $CFG->filter_mediaplugin_enable_html5audio = 1;
  61  $CFG->filter_mediaplugin_enable_html5video = 1;
  62  $CFG->filter_mediaplugin_enable_qt         = 1;
  63  $CFG->filter_mediaplugin_enable_wmp        = 1;
  64  $CFG->filter_mediaplugin_enable_rm         = 1;
  65  
  66  // Create plugin.
  67  $filterplugin = new filter_mediaplugin(null, array());
  68  
  69  // Note: As this is a developer test page, language strings are not used: all
  70  // text is English-only.
  71  
  72  /**
  73   * Starts time counter.
  74   */
  75  function filter_mediaplugin_perf_start() {
  76      global $filter_mediaplugin_starttime;
  77      $filter_mediaplugin_starttime = microtime(true);
  78  }
  79  
  80  /**
  81   * Ends and displays time counter.
  82   * @param string $name Counter name to display
  83   */
  84  function filter_mediaplugin_perf_stop($name) {
  85      global $filter_mediaplugin_starttime;
  86      $time = microtime(true) - $filter_mediaplugin_starttime;
  87  
  88      echo html_writer::tag('li', $name . ': ' . html_writer::tag('strong', round($time, 2)) . 'ms');
  89  }
  90  
  91  // 1) Some sample text strings.
  92  //    Note: These are from a random sample of real forum data. Just in case there
  93  //    are any privacy concerns I have altered names as may be clear.
  94  $samples = array(
  95      "<p>Hi,</p>&#13;\n<p>I've got myself 2 Heaney's \"The Burial at Thebes\"</p>",
  96      "best mark iv heard so far v v good",
  97      "<p>I have a script draft anyone want to look at it?",
  98      "<p>Thanks for your input Legolas and Ghimli!</p>",
  99      "<p>Just to say that I'm thinking of those of you who are working on TMA02.</p>",
 100      "<p><strong>1.</strong> <strong>If someone asks you 'where do you come from?'</strong></p>",
 101      "<p>With regards to Aragorn's question 'what would we do different'?</p>&#13;\n",
 102      "<p>Just thought I'd drop a line to see how everyone is managing generally?</p>&#13;\n",
 103      "<p>Feb '12 - Oct '12  AA100</p>&#13;\n<p>Nov '12 - April '13 - A150</p>&#13;\n",
 104      "<p>So where does that leave the bible???</p>",
 105  );
 106  
 107  // 2) Combine sample text strings into one really big (20KB) string.
 108  $length = 0;
 109  $bigstring = '';
 110  $index = 0;
 111  while ($length < 20 * 1024) {
 112      $bigstring .= $samples[$index];
 113      $length += strlen($samples[$index]);
 114      $index++;
 115      if ($index >= count($samples)) {
 116          $index = 0;
 117      }
 118  }
 119  
 120  // 3) Make random samples from this. I did the following stats on recent forum
 121  //    posts:
 122  //    0-199 characters approx 30%
 123  //    200-1999 approx 60%
 124  //    2000-19999 approx 10%.
 125  
 126  $samplebank = array();
 127  foreach (array(100 => 300, 1000 => 600, 10000 => 100) as $chars => $num) {
 128      for ($i = 0; $i < $num; $i++) {
 129          $start = rand(0, $length - $chars - 1);
 130          $samplebank[] = substr($bigstring, $start, $chars);
 131      }
 132  }
 133  
 134  echo html_writer::start_tag('ul');
 135  
 136  // First test: filter text that doesn't have any links.
 137  filter_mediaplugin_perf_start();
 138  foreach ($samplebank as $sample) {
 139      $filterplugin->filter($sample);
 140  }
 141  filter_mediaplugin_perf_stop('No links');
 142  
 143  // Second test: filter text with one link added (that doesn't match).
 144  $link = '<a href="http://www.example.org/another/link/">Link</a>';
 145  $linksamples = array();
 146  foreach ($samplebank as $sample) {
 147      // Make it the same length but with $link replacing the end part.
 148      $linksamples[] = substr($sample, 0, -strlen($link)) . $link;
 149  }
 150  
 151  filter_mediaplugin_perf_start();
 152  foreach ($linksamples as $sample) {
 153      $filterplugin->filter($sample);
 154  }
 155  filter_mediaplugin_perf_stop('One link (no match)');
 156  
 157  // Third test: filter text with one link added that does match (mp3).
 158  $link = '<a href="http://www.example.org/another/file.mp3">MP3 audio</a>';
 159  $linksamples = array();
 160  foreach ($samplebank as $sample) {
 161      // Make it the same length but with $link replacing the end part.
 162      $linksamples[] = substr($sample, 0, -strlen($link)) . $link;
 163  }
 164  
 165  filter_mediaplugin_perf_start();
 166  foreach ($linksamples as $sample) {
 167      $filterplugin->filter($sample);
 168  }
 169  filter_mediaplugin_perf_stop('One link (mp3)');
 170  
 171  // End page.
 172  echo html_writer::end_tag('ul');
 173  print $OUTPUT->footer();


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1