2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
20 * A simple test script that sets up test data in the database then
21 * measures performance of filter_get_active_in_context.
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
28 require(dirname(__FILE__) . '/../../../../config.php');
29 require_once($CFG->libdir . '/ddllib.php');
32 $syscontext = get_context_instance(CONTEXT_SYSTEM);
33 require_capability('tool/unittest:execute', $syscontext);
35 $baseurl = new moodle_url('/admin/tool/unittest/other/filtersettingsperformancetester.php');
37 $title = 'filter_get_active_in_context performance test';
38 $PAGE->set_url($baseurl);
39 $PAGE->set_context($syscontext);
40 $PAGE->navbar->add($title);
41 $PAGE->set_title($title);
42 $PAGE->set_heading($title);
43 echo $OUTPUT->header();
45 // Complain if we get this far and $CFG->unittestprefix is not set.
46 if (empty($CFG->unittestprefix)) {
47 throw new coding_exception('This page requires $CFG->unittestprefix to be set in config.php.');
50 $requiredtables = array('context', 'filter_active', 'filter_config');
52 $testdb = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary);
53 $testdb->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->unittestprefix);
55 $dbman = $testdb->get_manager();
57 foreach ($requiredtables as $table) {
58 if ($dbman->table_exists(new xmldb_table($table))) {
63 switch (optional_param('action', '', PARAM_ACTION)) {
67 foreach ($requiredtables as $table) {
68 $dbman->install_one_table_from_xmldb_file($CFG->dirroot . '/lib/db/install.xml', $table);
72 populate_test_database($syscontext, 10, 100, 1000, 5000, 5000);
73 echo $OUTPUT->notification('Test tables created.', 'notifysuccess');
74 } else if ($issetup == count($requiredtables)) {
75 echo $OUTPUT->notification('Test tables are already set up.', 'notifysuccess');
77 echo $OUTPUT->notification('Something is wrong, please delete the test tables and try again.');
83 foreach ($requiredtables as $tablename) {
84 $table = new xmldb_table($tablename);
85 if ($dbman->table_exists($table)) {
86 $dbman->drop_table($table);
90 echo $OUTPUT->notification('Test tables dropped.', 'notifysuccess');
95 if ($issetup != count($requiredtables)) {
96 echo $OUTPUT->notification('Something is wrong, please delete the test tables and try again.');
98 $contexts = $DB->get_records('context');
100 $basetime = run_tests('noop', $contexts, $numcalls, 0);
101 run_tests('simple_get_record_by_id', $contexts, $numcalls, $basetime);
102 run_tests('filter_get_active_in_context', $contexts, $numcalls, $basetime);
107 if ($issetup == count($requiredtables)) {
108 echo '<p>Total of ' . $DB->count_records('context') . ' contexts, ' .
109 $DB->count_records('filter_active') . ' filter_active and ' .
110 $DB->count_records('filter_config') . ' filter_config rows in the database.</p>';
115 echo $OUTPUT->container_start();
117 $aurl = new moodle_url($baseurl, array('action' => 'setup', 'sesskey'=>sesskey()));
118 echo $OUTPUT->single_button($aurl, 'Set up test tables', 'get', array('disabled'=>($issetup > 0)));
120 $aurl = new moodle_url($baseurl, array('action' => 'teardown', 'sesskey'=>sesskey()));
121 echo $OUTPUT->single_button($aurl, 'Drop test tables', 'get', array('disabled'=>($issetup == 0)));
123 $aurl = new moodle_url($baseurl, array('action' => 'test', 'sesskey'=>sesskey()));
124 echo $OUTPUT->single_button($aurl, 'Run tests', 'get', array('disabled'=>($issetup != count($requiredtables))));
126 echo $OUTPUT->container_end();
128 echo $OUTPUT->footer();
130 function noop($context) {
133 function simple_get_record_by_id($context) {
135 $DB->get_record('context', array('id' => $context->id));
138 function run_tests($function, $contexts, $numcalls, $basetime) {
140 $startime = microtime(true);
141 for ($j = 0; $j < $numcalls; $j++) {
142 $function($contexts[array_rand($contexts)]);
144 $duration = microtime(true) - $startime;
145 print_result_line($duration, $basetime, $numcalls, 'calls to ' . $function);
149 function print_result_line($duration, $basetime, $numcalls, $action1, $action2 = 'calls per second') {
150 echo '<p>Time for ' . format_float($numcalls, 0) . ' ' . $action1 . ': <b>' .
151 format_float($duration - $basetime, 3) . 's</b> (' . format_float($duration, 3) . ' - ' .
152 format_float($basetime, 3) . 's) which is ' .
153 format_float(($numcalls / ($duration - $basetime)), 0) . ' ' . $action2 . ".</p>\n";
157 function populate_test_database($syscontext, $numcategories, $numcourses, $nummodules, $numoverrides, $numconfigs) {
160 $syscontext->id = $DB->insert_record('context', $syscontext);
162 // Category contexts.
163 $categoryparents = array($syscontext);
164 $categories = array();
165 for ($i = 0; $i < $numcategories; $i++) {
166 $context = insert_context(CONTEXT_COURSECAT, $i, $categoryparents[array_rand($categoryparents)]);
167 $categoryparents[] = $context;
168 $categories[$context->id] = $context;
170 echo $OUTPUT->notification('Created ' . $numcategories . ' course category contexts.', 'notifysuccess'); flush();
174 for ($i = 0; $i < $numcourses; $i++) {
175 $context = insert_context(CONTEXT_COURSE, $i, $categories[array_rand($categories)]);
176 $courses[$context->id] = $context;
178 echo $OUTPUT->notification('Created ' . $numcourses . ' course contexts.', 'notifysuccess'); flush();
180 // Activities contexts.
182 $prog = new progress_bar('modbar', 500, true);
183 $transaction = $DB->start_delegated_transaction();
184 for ($i = 0; $i < $nummodules; $i++) {
185 $context = insert_context(CONTEXT_MODULE, $i, $courses[array_rand($courses)]);
186 $mods[$context->id] = $context;
188 $prog->update($i, $nummodules, '');
191 $transaction->allow_commit();
192 echo $OUTPUT->notification('Created ' . $nummodules . ' module contexts.', 'notifysuccess'); flush();
194 $contexts = $categories + $courses + $mods;
197 $installedfilters = filter_get_all_installed();
198 $counts = array(TEXTFILTER_DISABLED => 0, TEXTFILTER_OFF => 0, TEXTFILTER_ON => 0);
199 foreach ($installedfilters as $filter => $notused) {
200 $state = array_rand($counts);
201 filter_set_global_state($filter, $state);
204 echo $OUTPUT->notification('Set global setting: ' . $counts[TEXTFILTER_DISABLED] . ' disabled, ' .
205 $counts[TEXTFILTER_OFF] . ' off and ' . $counts[TEXTFILTER_ON] . ' on.', 'notifysuccess'); flush();
208 $localstates = array(TEXTFILTER_OFF => 0, TEXTFILTER_ON => 0);
209 $prog = new progress_bar('locbar', 500, true);
210 $transaction = $DB->start_delegated_transaction();
211 for ($i = 0; $i < $numoverrides; $i++) {
212 filter_set_local_state(array_rand($installedfilters), array_rand($contexts), array_rand($localstates));
214 $prog->update($i, $numoverrides, '');
217 $transaction->allow_commit();
218 echo $OUTPUT->notification('Set ' . $numoverrides . ' local overrides.', 'notifysuccess'); flush();
221 $variablenames = array('frog' => 0, 'toad' => 0, 'elver' => 0, 'eft' => 0, 'tadpole' => 0);
222 $prog = new progress_bar('confbar', 500, true);
223 $transaction = $DB->start_delegated_transaction();
224 for ($i = 0; $i < $numconfigs; $i++) {
225 filter_set_local_config(array_rand($installedfilters), array_rand($contexts),
226 array_rand($variablenames), random_string(rand(20, 40)));
228 $prog->update($i, $numconfigs, '');
231 $transaction->allow_commit();
232 echo $OUTPUT->notification('Set ' . $numconfigs . ' local configs.', 'notifysuccess'); flush();
235 function insert_context($contextlevel, $instanceid, $parent) {
237 $context = new stdClass;
238 $context->contextlevel = $contextlevel;
239 $context->instanceid = $instanceid;
240 $context->depth = $parent->depth + 1;
241 $context->id = $DB->insert_record('context', $context);
242 $context->path = $parent->path . '/' . $context->id;
243 $DB->set_field('context', 'path', $context->path, array('id' => $context->id));