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/>.
18 * Store performance test run + output script.
22 * @copyright 2012 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once('../config.php');
27 require_once($CFG->dirroot
. '/lib/adminlib.php');
29 $count = optional_param('count', 100, PARAM_INT
);
30 $count = min($count, 100000);
31 $count = max($count, 0);
33 admin_externalpage_setup('cachetestperformance');
35 $applicationtable = new html_table();
36 $applicationtable->head
= [
37 get_string('plugin', 'cache'),
38 get_string('result', 'cache'),
39 get_string('set', 'cache'),
40 get_string('gethit', 'cache'),
41 get_string('getmiss', 'cache'),
42 get_string('delete', 'cache'),
44 $applicationtable->data
= [];
45 $sessiontable = clone($applicationtable);
46 $requesttable = clone($applicationtable);
49 $application = cache_definition
::load_adhoc(cache_store
::MODE_APPLICATION
, 'cache', 'applicationtest');
50 $session = cache_definition
::load_adhoc(cache_store
::MODE_SESSION
, 'cache', 'sessiontest');
51 $request = cache_definition
::load_adhoc(cache_store
::MODE_REQUEST
, 'cache', 'requesttest');
53 $strinvalidplugin = new lang_string('invalidplugin', 'cache');
54 $strunsupportedmode = new lang_string('unsupportedmode', 'cache');
55 $struntestable = new lang_string('untestable', 'cache');
56 $strtested = new lang_string('tested', 'cache');
57 $strnotready = new lang_string('storenotready', 'cache');
59 foreach (core_component
::get_plugin_list_with_file('cachestore', 'lib.php', true) as $plugin => $path) {
60 $class = 'cachestore_' . $plugin;
61 $plugin = get_string('pluginname', 'cachestore_' . $plugin);
63 if (!class_exists($class) ||
!method_exists($class, 'initialise_test_instance') ||
!$class::are_requirements_met()) {
64 $applicationtable->data
[] = [$plugin, $strinvalidplugin, '-', '-', '-', '-'];
65 $sessiontable->data
[] = [$plugin, $strinvalidplugin, '-', '-', '-', '-'];
66 $requesttable->data
[] = [$plugin, $strinvalidplugin, '-', '-', '-', '-'];
70 if (!$class::is_supported_mode(cache_store
::MODE_APPLICATION
)) {
71 $applicationtable->data
[] = [$plugin, $strunsupportedmode, '-', '-', '-', '-'];
73 $store = $class::initialise_test_instance($application);
74 if ($store === false) {
75 $applicationtable->data
[] = [$plugin, $struntestable, '-', '-', '-', '-'];
76 } else if (!$store->is_ready()) {
77 $applicationtable->data
[] = [$plugin, $strnotready, '-', '-', '-', '-'];
79 $result = [$plugin, $strtested, 0, 0, 0];
80 $start = microtime(true);
81 for ($i = 0; $i < $count; $i++
) {
82 $store->set('key' . $i, 'test data ' . $i);
84 $result[2] = sprintf('%01.4f', microtime(true) - $start);
86 $start = microtime(true);
87 for ($i = 0; $i < $count; $i++
) {
88 $store->get('key' . $i);
90 $result[3] = sprintf('%01.4f', microtime(true) - $start);
92 $start = microtime(true);
93 for ($i = 0; $i < $count; $i++
) {
94 $store->get('fake' . $i);
96 $result[4] = sprintf('%01.4f', microtime(true) - $start);
98 $start = microtime(true);
99 for ($i = 0; $i < $count; $i++
) {
100 $store->delete('key' . $i);
102 $result[5] = sprintf('%01.4f', microtime(true) - $start);
103 $applicationtable->data
[] = $result;
104 $store->instance_deleted();
108 if (!$class::is_supported_mode(cache_store
::MODE_SESSION
)) {
109 $sessiontable->data
[] = [$plugin, $strunsupportedmode, '-', '-', '-', '-'];
111 $store = $class::initialise_test_instance($session);
112 if ($store === false) {
113 $sessiontable->data
[] = [$plugin, $struntestable, '-', '-', '-', '-'];
114 } else if (!$store->is_ready()) {
115 $sessiontable->data
[] = [$plugin, $strnotready, '-', '-', '-', '-'];
117 $result = [$plugin, $strtested, 0, 0, 0];
118 $start = microtime(true);
119 for ($i = 0; $i < $count; $i++
) {
120 $store->set('key' . $i, 'test data ' . $i);
122 $result[2] = sprintf('%01.4f', microtime(true) - $start);
124 $start = microtime(true);
125 for ($i = 0; $i < $count; $i++
) {
126 $store->get('key' . $i);
128 $result[3] = sprintf('%01.4f', microtime(true) - $start);
130 $start = microtime(true);
131 for ($i = 0; $i < $count; $i++
) {
132 $store->get('fake' . $i);
134 $result[4] = sprintf('%01.4f', microtime(true) - $start);
136 $start = microtime(true);
137 for ($i = 0; $i < $count; $i++
) {
138 $store->delete('key' . $i);
140 $result[5] = sprintf('%01.4f', microtime(true) - $start);
141 $sessiontable->data
[] = $result;
142 $store->instance_deleted();
146 if (!$class::is_supported_mode(cache_store
::MODE_REQUEST
)) {
147 $requesttable->data
[] = [$plugin, $strunsupportedmode, '-', '-', '-', '-'];
149 $store = $class::initialise_test_instance($request);
150 if ($store === false) {
151 $requesttable->data
[] = [$plugin, $struntestable, '-', '-', '-', '-'];
152 } else if (!$store->is_ready()) {
153 $requesttable->data
[] = [$plugin, $strnotready, '-', '-', '-', '-'];
155 $result = [$plugin, $strtested, 0, 0, 0];
156 $start = microtime(true);
157 for ($i = 0; $i < $count; $i++
) {
158 $store->set('key' . $i, 'test data ' . $i);
160 $result[2] = sprintf('%01.4f', microtime(true) - $start);
162 $start = microtime(true);
163 for ($i = 0; $i < $count; $i++
) {
164 $store->get('key' . $i);
166 $result[3] = sprintf('%01.4f', microtime(true) - $start);
168 $start = microtime(true);
169 for ($i = 0; $i < $count; $i++
) {
170 $store->get('fake' . $i);
172 $result[4] = sprintf('%01.4f', microtime(true) - $start);
174 $start = microtime(true);
175 for ($i = 0; $i < $count; $i++
) {
176 $store->delete('key' . $i);
178 $result[5] = sprintf('%01.4f', microtime(true) - $start);
179 $requesttable->data
[] = $result;
180 $store->instance_deleted();
185 echo $OUTPUT->header();
186 echo $OUTPUT->heading(get_string('storeperformance', 'cache', $count));
188 $possiblecounts = [1, 10, 100, 500, 1000, 5000, 10000, 50000, 100000];
190 foreach ($possiblecounts as $pcount) {
191 $links[] = html_writer
::link(new moodle_url($PAGE->url
, ['count' => $pcount]), $pcount);
193 echo $OUTPUT->box_start('generalbox performance-test-counts');
194 echo get_string('requestcount', 'cache', join(', ', $links));
195 echo $OUTPUT->box_end();
197 echo $OUTPUT->heading(get_string('storeresults_application', 'cache'));
198 echo html_writer
::table($applicationtable);
200 echo $OUTPUT->heading(get_string('storeresults_session', 'cache'));
201 echo html_writer
::table($sessiontable);
203 echo $OUTPUT->heading(get_string('storeresults_request', 'cache'));
204 echo html_writer
::table($requesttable);
206 echo $OUTPUT->footer();