forked from mikasmart/moodle-report_benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testlib.php
299 lines (253 loc) · 10.7 KB
/
testlib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Declare all tests for the benchmark
*
* @package report
* @subpackage benchmark
* @copyright Mickaël Pannequin, mickael.pannequin@smartcanal.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* HOW TO CREATE A TEST
*
* Create a public static function in this class (benchmark_test).
* The function doesn't need to be declared elsewhere because it is automaticly loaded by the main class "benchmark".
*
* Structure :
*
* public static function the_function_name() {
* echo 'foo';
* return array('limit' => .5, 'over' => .8, 'fail' => BENCHFAIL_BLABLABLA);
* }
*
* 1) The function must return an array with attributes :
* (float) limit: Time limit too high but acceptable (orange)
* (float) over : Over time, the benchmark fail (red)
* (define) fail : To display the good text if the test fail
*
* 2) The function must have strings in language file "/lang/xy/report_benchmark.php"
*
* If you create more test, please send it to the community
*
*
*
*/
defined('MOODLE_INTERNAL') || die();
/**
* BenchMark Test
*/
// Define to join the language pack
define('BENCHFAIL_SLOWSERVER', 'slowserver');
define('BENCHFAIL_SLOWPROCESSOR', 'slowprocessor');
define('BENCHFAIL_SLOWHARDDRIVE', 'slowharddrive');
define('BENCHFAIL_SLOWDATABASE', 'slowdatabase');
define('BENCHFAIL_SLOWWEB', 'slowweb');
/**
* Tests for the BenchMark report
*
*/
class benchmark_test extends benchmark {
/**
* Moodle loading time
*
* @return array Contains the test results
*/
public static function cload() {
return array(
'limit' => .5,
'over' => .8,
'start' => BENCHSTART,
'stop' => BENCHSTOP,
'fail' => BENCHFAIL_SLOWSERVER
);
}
/**
* Function called many times
*
* @return array Contains the test results
*/
public static function processor() {
$pass = 10000000;
for ($i = 0; $i < $pass; ++$i);
$i = 0;
while($i < $pass) {
++$i;
}
return array('limit' => .5, 'over' => .8, 'fail' => BENCHFAIL_SLOWPROCESSOR);
}
/**
* Reading files in the Moodle's temporary folder
*
* @return array Contains the test results
*/
public static function fileread() {
global $CFG;
file_put_contents($CFG->tempdir.'/benchmark.temp', 'benchmark');
$i = 0;
$pass = 2000;
while($i < $pass) {
++$i;
file_get_contents($CFG->tempdir.'/benchmark.temp');
}
unlink($CFG->tempdir.'/benchmark.temp');
return array('limit' => .5, 'over' => .8, 'fail' => BENCHFAIL_SLOWHARDDRIVE);
}
/**
* Creating files in the Moodle's temporary folder
*
* @return array Contains the test results
*/
public static function filewrite() {
global $CFG;
$lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque lacus felis, dignissim quis nisl sit amet, blandit suscipit lacus. Duis maximus, urna sed fringilla consequat, tellus ex sollicitudin ante, vitae posuere neque purus nec justo. Donec porta ipsum sed urna tempus, sit amet dictum lorem euismod. Phasellus vel erat a libero aliquet venenatis. Phasellus condimentum venenatis risus ut egestas. Morbi sit amet posuere orci, id tempor dui. Vestibulum eget sapien eget mauris eleifend ullamcorper. In finibus mauris id augue fermentum porta. Fusce dictum vestibulum justo eget malesuada. Nullam at tincidunt urna, nec ultrices velit. Nunc eget augue velit. Mauris sed rhoncus purus. Etiam aliquam urna ac nisl tristique, vitae tristique urna tincidunt. Vestibulum luctus nulla magna, non tristique risus rhoncus nec. Vestibulum vestibulum, nulla scelerisque congue molestie, dolor risus hendrerit velit, non malesuada nisi orci eget eros. Aenean interdum ut lectus quis semper. Curabitur viverra vitae augue id.';
$loremipsum = str_repeat($lorem, 16);
$i = 0;
$pass = 2000;
while($i < $pass) {
++$i;
file_put_contents($CFG->tempdir.'/benchmark.temp', $loremipsum);
}
unlink($CFG->tempdir.'/benchmark.temp');
return array('limit' => 1, 'over' => 1.25, 'fail' => BENCHFAIL_SLOWHARDDRIVE);
}
/**
* Reading course
*
* @return array Contains the test results
*/
public static function courseread() {
global $DB;
$i = 0;
$pass = 500;
while($i < $pass) {
++$i;
$DB->get_record('course', array('id' => 1));
}
return array('limit' => .75, 'over' => 1, 'fail' => BENCHFAIL_SLOWDATABASE);
}
/**
* Writing course
*
* @return array Contains the test results
*/
public static function coursewrite() {
global $DB;
$uniq = md5(uniqid(rand(), true));
$newrecord = new stdClass;
$newrecord->shortname = '!!!BENCH-'.$uniq;
$newrecord->fullname = '!!!BENCH-'.$uniq;
$newrecord->format = 'site';
$newrecord->visible = 0;
$newrecord->sortorder = 0;
$i = 0;
$pass = 25;
while($i < $pass) {
++$i;
$DB->insert_record('course', $newrecord);
}
$DB->delete_records('course', array('shortname' => $newrecord->shortname));
unset($newrecord);
return array('limit' => 1, 'over' => 1.25, 'fail' => BENCHFAIL_SLOWDATABASE);
}
/**
* Complex request (n°1)
*
* @return array Contains the test results
*/
public static function querytype1() {
global $DB;
$i = 0;
$sql = "SELECT bi.id,bp.id AS blockpositionid,bi.blockname,bi.parentcontextid,bi.showinsubcontexts,bi.pagetypepattern,bi.subpagepattern,bi.defaultregion,bi.defaultweight,COALESCE(bp.visible, 1) AS visible,COALESCE(bp.region, bi.defaultregion) AS region,COALESCE(bp.weight, bi.defaultweight) AS weight,bi.configdata, ctx.id AS ctxid, ctx.path AS ctxpath, ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel, ctx.instanceid AS ctxinstance FROM mdl_block_instances bi JOIN mdl_block b ON bi.blockname = b.name LEFT JOIN mdl_block_positions bp ON bp.blockinstanceid = bi.id AND bp.contextid = '26' AND bp.pagetype = 'mod-forum-discuss' AND bp.subpage = '' LEFT JOIN mdl_context ctx ON (ctx.instanceid = bi.id AND ctx.contextlevel = '80') WHERE (bi.parentcontextid = '26' OR (bi.showinsubcontexts = 1 AND bi.parentcontextid IN ('16','3','1'))) AND bi.pagetypepattern IN ('mod-forum-discuss','mod-forum-discuss-*','mod-forum-*','mod-*','*') AND (bi.subpagepattern IS NULL OR bi.subpagepattern = '') AND (bp.visible = 1 OR bp.visible IS NULL) AND b.visible = 1 ORDER BY COALESCE(bp.region, bi.defaultregion),COALESCE(bp.weight, bi.defaultweight),bi.id;";
$pass = 100;
while($i < $pass) {
++$i;
$DB->get_records_sql($sql);
}
return array('limit' => .5, 'over' => .7, 'fail' => BENCHFAIL_SLOWDATABASE);
}
/**
* Complex request (n°2)
*
* @return array Contains the test results
*/
public static function querytype2() {
global $DB;
$i = 0;
$sql = "SELECT parent_states.filter, CASE WHEN fa.active IS NULL THEN 0 ELSE fa.active END AS localstate, parent_states.inheritedstate FROM (SELECT f.filter, MAX(f.sortorder) AS sortorder, CASE WHEN MAX(f.active * ctx.depth) > -MIN(f.active * ctx.depth) THEN 1 ELSE -1 END AS inheritedstate FROM mdl_filter_active f JOIN mdl_context ctx ON f.contextid = ctx.id WHERE ctx.id IN (1,3,16) GROUP BY f.filter HAVING MIN(f.active) > -9999 ) parent_states LEFT JOIN mdl_filter_active fa ON fa.filter = parent_states.filter AND fa.contextid = 26 ORDER BY parent_states.sortorder;";
$pass = 250;
while($i < $pass) {
++$i;
$DB->get_records_sql($sql);
}
return array('limit' => .3, 'over' => .5, 'fail' => BENCHFAIL_SLOWDATABASE);
}
/**
* Time to connect with the guest account
*
* @return array Contains the test results
*/
public static function loginguest() {
global $CFG;
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query(
array(
'username' => 'guest',
'password' => 'guest',
)
)
)
);
file_get_contents($CFG->wwwroot.'/login/index.php', false, stream_context_create($opts));
return array('limit' => .3, 'over' => .8, 'fail' => BENCHFAIL_SLOWWEB);
}
/**
* Time to connect with the user account
*
* @return array Contains the test results
*/
public static function loginuser() {
global $CFG, $DB;
$user = new stdClass();
$user->auth = 'manual';
$user->confirmed = 1;
$user->mnethostid = 1;
$user->email = 'benchtest@benchtest.com';
$user->username = 'benchtest';
$user->password = md5('benchtest');
$user->lastname = 'benchtest';
$user->firstname = 'benchtest';
$user->id = $DB->insert_record('user', $user);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query(
array(
'username' => 'benchtest',
'password' => md5('benchtest'),
)
)
)
);
file_get_contents($CFG->wwwroot . '/login/index.php', false, stream_context_create($opts));
$DB->delete_records('user', array('id' => $user->id));
unset($user);
return array('limit' => .3, 'over' => .8, 'fail' => BENCHFAIL_SLOWWEB);
}
}