|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00028 require(dirname(__FILE__) . '/../../../../config.php'); 00029 require_once($CFG->libdir . '/ddllib.php'); 00030 00031 require_login(); 00032 $syscontext = get_context_instance(CONTEXT_SYSTEM); 00033 require_capability('tool/unittest:execute', $syscontext); 00034 00035 $baseurl = new moodle_url('/admin/tool/unittest/other/filtersettingsperformancetester.php'); 00036 00037 $title = 'filter_get_active_in_context performance test'; 00038 $PAGE->set_url($baseurl); 00039 $PAGE->set_context($syscontext); 00040 $PAGE->navbar->add($title); 00041 $PAGE->set_title($title); 00042 $PAGE->set_heading($title); 00043 echo $OUTPUT->header(); 00044 00045 // Complain if we get this far and $CFG->unittestprefix is not set. 00046 if (empty($CFG->unittestprefix)) { 00047 throw new coding_exception('This page requires $CFG->unittestprefix to be set in config.php.'); 00048 } 00049 00050 $requiredtables = array('context', 'filter_active', 'filter_config'); 00051 $realdb = $DB; 00052 $testdb = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary); 00053 $testdb->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->unittestprefix); 00054 $DB = $testdb; 00055 $dbman = $testdb->get_manager(); 00056 $issetup = 0; 00057 foreach ($requiredtables as $table) { 00058 if ($dbman->table_exists(new xmldb_table($table))) { 00059 $issetup++; 00060 } 00061 } 00062 00063 switch (optional_param('action', '', PARAM_ACTION)) { 00064 case 'setup': 00065 require_sesskey(); 00066 if ($issetup == 0) { 00067 foreach ($requiredtables as $table) { 00068 $dbman->install_one_table_from_xmldb_file($CFG->dirroot . '/lib/db/install.xml', $table); 00069 $issetup++; 00070 } 00071 flush(); 00072 populate_test_database($syscontext, 10, 100, 1000, 5000, 5000); 00073 echo $OUTPUT->notification('Test tables created.', 'notifysuccess'); 00074 } else if ($issetup == count($requiredtables)) { 00075 echo $OUTPUT->notification('Test tables are already set up.', 'notifysuccess'); 00076 } else { 00077 echo $OUTPUT->notification('Something is wrong, please delete the test tables and try again.'); 00078 } 00079 break; 00080 00081 case 'teardown': 00082 require_sesskey(); 00083 foreach ($requiredtables as $tablename) { 00084 $table = new xmldb_table($tablename); 00085 if ($dbman->table_exists($table)) { 00086 $dbman->drop_table($table); 00087 } 00088 } 00089 $issetup = 0; 00090 echo $OUTPUT->notification('Test tables dropped.', 'notifysuccess'); 00091 break; 00092 00093 case 'test': 00094 require_sesskey(); 00095 if ($issetup != count($requiredtables)) { 00096 echo $OUTPUT->notification('Something is wrong, please delete the test tables and try again.'); 00097 } else { 00098 $contexts = $DB->get_records('context'); 00099 $numcalls = 1000; 00100 $basetime = run_tests('noop', $contexts, $numcalls, 0); 00101 run_tests('simple_get_record_by_id', $contexts, $numcalls, $basetime); 00102 run_tests('filter_get_active_in_context', $contexts, $numcalls, $basetime); 00103 } 00104 break; 00105 } 00106 00107 if ($issetup == count($requiredtables)) { 00108 echo '<p>Total of ' . $DB->count_records('context') . ' contexts, ' . 00109 $DB->count_records('filter_active') . ' filter_active and ' . 00110 $DB->count_records('filter_config') . ' filter_config rows in the database.</p>'; 00111 } 00112 00113 $DB = $realdb; 00114 00115 echo $OUTPUT->container_start(); 00116 00117 $aurl = new moodle_url($baseurl, array('action' => 'setup', 'sesskey'=>sesskey())); 00118 echo $OUTPUT->single_button($aurl, 'Set up test tables', 'get', array('disabled'=>($issetup > 0))); 00119 00120 $aurl = new moodle_url($baseurl, array('action' => 'teardown', 'sesskey'=>sesskey())); 00121 echo $OUTPUT->single_button($aurl, 'Drop test tables', 'get', array('disabled'=>($issetup == 0))); 00122 00123 $aurl = new moodle_url($baseurl, array('action' => 'test', 'sesskey'=>sesskey())); 00124 echo $OUTPUT->single_button($aurl, 'Run tests', 'get', array('disabled'=>($issetup != count($requiredtables)))); 00125 00126 echo $OUTPUT->container_end(); 00127 00128 echo $OUTPUT->footer(); 00129 00130 function noop($context) { 00131 } 00132 00133 function simple_get_record_by_id($context) { 00134 global $DB; 00135 $DB->get_record('context', array('id' => $context->id)); 00136 } 00137 00138 function run_tests($function, $contexts, $numcalls, $basetime) { 00139 set_time_limit(120); 00140 $startime = microtime(true); 00141 for ($j = 0; $j < $numcalls; $j++) { 00142 $function($contexts[array_rand($contexts)]); 00143 } 00144 $duration = microtime(true) - $startime; 00145 print_result_line($duration, $basetime, $numcalls, 'calls to ' . $function); 00146 return $duration; 00147 } 00148 00149 function print_result_line($duration, $basetime, $numcalls, $action1, $action2 = 'calls per second') { 00150 echo '<p>Time for ' . format_float($numcalls, 0) . ' ' . $action1 . ': <b>' . 00151 format_float($duration - $basetime, 3) . 's</b> (' . format_float($duration, 3) . ' - ' . 00152 format_float($basetime, 3) . 's) which is ' . 00153 format_float(($numcalls / ($duration - $basetime)), 0) . ' ' . $action2 . ".</p>\n"; 00154 flush(); 00155 } 00156 00157 function populate_test_database($syscontext, $numcategories, $numcourses, $nummodules, $numoverrides, $numconfigs) { 00158 global $DB, $OUTPUT; 00159 set_time_limit(600); 00160 $syscontext->id = $DB->insert_record('context', $syscontext); 00161 00162 // Category contexts. 00163 $categoryparents = array($syscontext); 00164 $categories = array(); 00165 for ($i = 0; $i < $numcategories; $i++) { 00166 $context = insert_context(CONTEXT_COURSECAT, $i, $categoryparents[array_rand($categoryparents)]); 00167 $categoryparents[] = $context; 00168 $categories[$context->id] = $context; 00169 } 00170 echo $OUTPUT->notification('Created ' . $numcategories . ' course category contexts.', 'notifysuccess'); flush(); 00171 00172 // Course contexts. 00173 $courses = array(); 00174 for ($i = 0; $i < $numcourses; $i++) { 00175 $context = insert_context(CONTEXT_COURSE, $i, $categories[array_rand($categories)]); 00176 $courses[$context->id] = $context; 00177 } 00178 echo $OUTPUT->notification('Created ' . $numcourses . ' course contexts.', 'notifysuccess'); flush(); 00179 00180 // Activities contexts. 00181 $mods = array(); 00182 $prog = new progress_bar('modbar', 500, true); 00183 $transaction = $DB->start_delegated_transaction(); 00184 for ($i = 0; $i < $nummodules; $i++) { 00185 $context = insert_context(CONTEXT_MODULE, $i, $courses[array_rand($courses)]); 00186 $mods[$context->id] = $context; 00187 if ($i % 50) { 00188 $prog->update($i, $nummodules, ''); 00189 } 00190 } 00191 $transaction->allow_commit(); 00192 echo $OUTPUT->notification('Created ' . $nummodules . ' module contexts.', 'notifysuccess'); flush(); 00193 00194 $contexts = $categories + $courses + $mods; 00195 00196 // Global settings. 00197 $installedfilters = filter_get_all_installed(); 00198 $counts = array(TEXTFILTER_DISABLED => 0, TEXTFILTER_OFF => 0, TEXTFILTER_ON => 0); 00199 foreach ($installedfilters as $filter => $notused) { 00200 $state = array_rand($counts); 00201 filter_set_global_state($filter, $state); 00202 $counts[$state]++; 00203 } 00204 echo $OUTPUT->notification('Set global setting: ' . $counts[TEXTFILTER_DISABLED] . ' disabled, ' . 00205 $counts[TEXTFILTER_OFF] . ' off and ' . $counts[TEXTFILTER_ON] . ' on.', 'notifysuccess'); flush(); 00206 00207 // Local overrides. 00208 $localstates = array(TEXTFILTER_OFF => 0, TEXTFILTER_ON => 0); 00209 $prog = new progress_bar('locbar', 500, true); 00210 $transaction = $DB->start_delegated_transaction(); 00211 for ($i = 0; $i < $numoverrides; $i++) { 00212 filter_set_local_state(array_rand($installedfilters), array_rand($contexts), array_rand($localstates)); 00213 if ($i % 50) { 00214 $prog->update($i, $numoverrides, ''); 00215 } 00216 } 00217 $transaction->allow_commit(); 00218 echo $OUTPUT->notification('Set ' . $numoverrides . ' local overrides.', 'notifysuccess'); flush(); 00219 00220 // Local config. 00221 $variablenames = array('frog' => 0, 'toad' => 0, 'elver' => 0, 'eft' => 0, 'tadpole' => 0); 00222 $prog = new progress_bar('confbar', 500, true); 00223 $transaction = $DB->start_delegated_transaction(); 00224 for ($i = 0; $i < $numconfigs; $i++) { 00225 filter_set_local_config(array_rand($installedfilters), array_rand($contexts), 00226 array_rand($variablenames), random_string(rand(20, 40))); 00227 if ($i % 50) { 00228 $prog->update($i, $numconfigs, ''); 00229 } 00230 } 00231 $transaction->allow_commit(); 00232 echo $OUTPUT->notification('Set ' . $numconfigs . ' local configs.', 'notifysuccess'); flush(); 00233 } 00234 00235 function insert_context($contextlevel, $instanceid, $parent) { 00236 global $DB; 00237 $context = new stdClass; 00238 $context->contextlevel = $contextlevel; 00239 $context->instanceid = $instanceid; 00240 $context->depth = $parent->depth + 1; 00241 $context->id = $DB->insert_record('context', $context); 00242 $context->path = $parent->path . '/' . $context->id; 00243 $DB->set_field('context', 'path', $context->path, array('id' => $context->id)); 00244 return $context; 00245 } 00246