Proyectos de Subversion Moodle

Rev

| 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
 * Run the unit tests.
19
 *
20
 * A simple test script that sets up test data in the database then
21
 * measures performance of filter_get_active_in_context.
22
 *
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
26
 */
27
 
28
die(); //TODO: this needs to be rewritten as standard advanced_testcase
29
 
30
require(__DIR__ . '/../../../config.php');
31
require_once($CFG->libdir . '/ddllib.php');
32
 
33
require_login();
34
$syscontext = context_system::instance();
35
require_capability('moodle/site:config', $syscontext);
36
 
37
$baseurl = new moodle_url('/lib/tests/performance/filtersettingsperformancetester.php');
38
 
39
$title = 'filter_get_active_in_context performance test';
40
$PAGE->set_url($baseurl);
41
$PAGE->set_context($syscontext);
42
$PAGE->navbar->add($title);
43
$PAGE->set_title($title);
44
$PAGE->set_heading($title);
45
echo $OUTPUT->header();
46
 
47
// Complain if we get this far and $CFG->unittestprefix is not set.
48
if (empty($CFG->unittestprefix)) {
49
    throw new coding_exception('This page requires $CFG->unittestprefix to be set in config.php.');
50
}
51
 
52
$requiredtables = array('context', 'filter_active', 'filter_config');
53
$realdb = $DB;
54
$testdb = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary);
55
$testdb->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->unittestprefix);
56
/** @var moodle_database $DB */
57
$DB = $testdb;
58
$dbman = $testdb->get_manager();
59
$issetup = 0;
60
foreach ($requiredtables as $table) {
61
    if ($dbman->table_exists(new xmldb_table($table))) {
62
        $issetup++;
63
    }
64
}
65
 
66
switch (optional_param('action', '', PARAM_ALPHANUMEXT)) {
67
    case 'setup':
68
        require_sesskey();
69
        if ($issetup == 0) {
70
            foreach ($requiredtables as $table) {
71
                $dbman->install_one_table_from_xmldb_file($CFG->dirroot . '/lib/db/install.xml', $table);
72
                $issetup++;
73
            }
74
            flush();
75
            populate_test_database($syscontext, 10, 100, 1000, 5000, 5000);
76
            echo $OUTPUT->notification('Test tables created.', 'notifysuccess');
77
        } else if ($issetup == count($requiredtables)) {
78
            echo $OUTPUT->notification('Test tables are already set up.', 'notifysuccess');
79
        } else {
80
            echo $OUTPUT->notification('Something is wrong, please delete the test tables and try again.');
81
        }
82
        break;
83
 
84
    case 'teardown':
85
        require_sesskey();
86
        foreach ($requiredtables as $tablename) {
87
            $table = new xmldb_table($tablename);
88
            if ($dbman->table_exists($table)) {
89
                $dbman->drop_table($table);
90
            }
91
        }
92
        $issetup = 0;
93
        echo $OUTPUT->notification('Test tables dropped.', 'notifysuccess');
94
        break;
95
 
96
    case 'test':
97
        require_sesskey();
98
        if ($issetup != count($requiredtables)) {
99
            echo $OUTPUT->notification('Something is wrong, please delete the test tables and try again.');
100
        } else {
101
            $contexts = $DB->get_records('context');
102
            $numcalls = 1000;
103
            $basetime = run_tests('noop', $contexts, $numcalls, 0);
104
            run_tests('simple_get_record_by_id', $contexts, $numcalls, $basetime);
105
            run_tests('filter_get_active_in_context', $contexts, $numcalls, $basetime);
106
        }
107
        break;
108
}
109
 
110
if ($issetup == count($requiredtables)) {
111
    echo '<p>Total of ' . $DB->count_records('context') . ' contexts, ' .
112
            $DB->count_records('filter_active') . ' filter_active and ' .
113
            $DB->count_records('filter_config') . ' filter_config rows in the database.</p>';
114
}
115
 
116
$DB = $realdb;
117
 
118
echo $OUTPUT->container_start();
119
 
120
$aurl = new moodle_url($baseurl, array('action' => 'setup', 'sesskey'=>sesskey()));
121
echo $OUTPUT->single_button($aurl, 'Set up test tables', 'get', array('disabled'=>($issetup > 0)));
122
 
123
$aurl = new moodle_url($baseurl, array('action' => 'teardown', 'sesskey'=>sesskey()));
124
echo $OUTPUT->single_button($aurl, 'Drop test tables', 'get', array('disabled'=>($issetup == 0)));
125
 
126
$aurl = new moodle_url($baseurl, array('action' => 'test', 'sesskey'=>sesskey()));
127
echo $OUTPUT->single_button($aurl, 'Run tests', 'get', array('disabled'=>($issetup != count($requiredtables))));
128
 
129
echo $OUTPUT->container_end();
130
 
131
echo $OUTPUT->footer();
132
 
133
function noop($context) {
134
}
135
 
136
function simple_get_record_by_id($context) {
137
    global $DB;
138
    $DB->get_record('context', array('id' => $context->id));
139
}
140
 
141
function run_tests($function, $contexts, $numcalls, $basetime) {
142
    core_php_time_limit::raise(120);
143
    $startime = microtime(true);
144
    for ($j = 0; $j < $numcalls; $j++) {
145
        $function($contexts[array_rand($contexts)]);
146
    }
147
    $duration = microtime(true) - $startime;
148
    print_result_line($duration, $basetime, $numcalls, 'calls to ' . $function);
149
    return $duration;
150
}
151
 
152
function print_result_line($duration, $basetime, $numcalls, $action1, $action2 = 'calls per second') {
153
    echo '<p>Time for ' . format_float($numcalls, 0) . ' ' . $action1 . ': <b>' .
154
            format_float($duration - $basetime, 3) . 's</b> (' . format_float($duration, 3) . ' - ' .
155
            format_float($basetime, 3) . 's) which is ' .
156
            format_float(($numcalls / ($duration - $basetime)), 0) . ' ' . $action2 . ".</p>\n";
157
    flush();
158
}
159
 
160
function populate_test_database($syscontext, $numcategories, $numcourses, $nummodules, $numoverrides, $numconfigs) {
161
    global $DB, $OUTPUT;
162
    core_php_time_limit::raise(600);
163
    $syscontext->id = $DB->insert_record('context', $syscontext);
164
 
165
    // Category contexts.
166
    $categoryparents = array($syscontext);
167
    $categories = array();
168
    for ($i = 0; $i < $numcategories; $i++) {
169
        $context = insert_context(CONTEXT_COURSECAT, $i, $categoryparents[array_rand($categoryparents)]);
170
        $categoryparents[] = $context;
171
        $categories[$context->id] = $context;
172
    }
173
    echo $OUTPUT->notification('Created ' . $numcategories . ' course category contexts.', 'notifysuccess'); flush();
174
 
175
    // Course contexts.
176
    $courses = array();
177
    for ($i = 0; $i < $numcourses; $i++) {
178
        $context = insert_context(CONTEXT_COURSE, $i, $categories[array_rand($categories)]);
179
        $courses[$context->id] = $context;
180
    }
181
    echo $OUTPUT->notification('Created ' . $numcourses . ' course contexts.', 'notifysuccess'); flush();
182
 
183
    // Activities contexts.
184
    $mods = array();
185
    $prog = new progress_bar('modbar', 500, true);
186
    $transaction = $DB->start_delegated_transaction();
187
    for ($i = 0; $i < $nummodules; $i++) {
188
        $context = insert_context(CONTEXT_MODULE, $i, $courses[array_rand($courses)]);
189
        $mods[$context->id] = $context;
190
        if ($i % 50) {
191
            $prog->update($i, $nummodules, '');
192
        }
193
    }
194
    $transaction->allow_commit();
195
    echo $OUTPUT->notification('Created ' . $nummodules . ' module contexts.', 'notifysuccess'); flush();
196
 
197
    $contexts = $categories + $courses + $mods;
198
 
199
    // Global settings.
200
    $installedfilters = filter_get_all_installed();
201
    $counts = array(TEXTFILTER_DISABLED => 0, TEXTFILTER_OFF => 0, TEXTFILTER_ON => 0);
202
    foreach ($installedfilters as $filter => $notused) {
203
        $state = array_rand($counts);
204
        filter_set_global_state($filter, $state);
205
        $counts[$state]++;
206
    }
207
    echo $OUTPUT->notification('Set global setting: ' . $counts[TEXTFILTER_DISABLED] . ' disabled, ' .
208
            $counts[TEXTFILTER_OFF] . ' off and ' . $counts[TEXTFILTER_ON] . ' on.', 'notifysuccess'); flush();
209
 
210
    // Local overrides.
211
    $localstates = array(TEXTFILTER_OFF => 0, TEXTFILTER_ON => 0);
212
    $prog = new progress_bar('locbar', 500, true);
213
    $transaction = $DB->start_delegated_transaction();
214
    for ($i = 0; $i < $numoverrides; $i++) {
215
        filter_set_local_state(array_rand($installedfilters), array_rand($contexts), array_rand($localstates));
216
        if ($i % 50) {
217
            $prog->update($i, $numoverrides, '');
218
        }
219
    }
220
    $transaction->allow_commit();
221
    echo $OUTPUT->notification('Set ' . $numoverrides . ' local overrides.', 'notifysuccess'); flush();
222
 
223
    // Local config.
224
    $variablenames = array('frog' => 0, 'toad' => 0, 'elver' => 0, 'eft' => 0, 'tadpole' => 0);
225
    $prog = new progress_bar('confbar', 500, true);
226
    $transaction = $DB->start_delegated_transaction();
227
    for ($i = 0; $i < $numconfigs; $i++) {
228
        filter_set_local_config(array_rand($installedfilters), array_rand($contexts),
229
                array_rand($variablenames), random_string(rand(20, 40)));
230
        if ($i % 50) {
231
            $prog->update($i, $numconfigs, '');
232
        }
233
    }
234
    $transaction->allow_commit();
235
    echo $OUTPUT->notification('Set ' . $numconfigs . ' local configs.', 'notifysuccess'); flush();
236
}
237
 
238
function insert_context($contextlevel, $instanceid, $parent) {
239
    global $DB;
240
    $context = new stdClass;
241
    $context->contextlevel = $contextlevel;
242
    $context->instanceid = $instanceid;
243
    $context->depth = $parent->depth + 1;
244
    $context->id = $DB->insert_record('context', $context);
245
    $context->path = $parent->path . '/' . $context->id;
246
    $DB->set_field('context', 'path', $context->path, array('id' => $context->id));
247
    return $context;
248
}
249