Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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