PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/filter/mediaplugin/dev/perftest.php

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