Merge branch 'MDL-81457-main' of https://github.com/andrewnicols/moodle
[moodle.git] / cache / testperformance.php
blob4592e41d2cf18462d5850b27daf05570b2505054
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.
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/>.
17 /**
18 * Store performance test run + output script.
20 * @package core
21 * @category cache
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');
28 require_once($CFG->dirroot.'/cache/locallib.php');
30 $count = optional_param('count', 100, PARAM_INT);
31 $count = min($count, 100000);
32 $count = max($count, 0);
34 admin_externalpage_setup('cachetestperformance');
36 $applicationtable = new html_table();
37 $applicationtable->head = array(
38 get_string('plugin', 'cache'),
39 get_string('result', 'cache'),
40 get_string('set', 'cache'),
41 get_string('gethit', 'cache'),
42 get_string('getmiss', 'cache'),
43 get_string('delete', 'cache'),
45 $applicationtable->data = array();
46 $sessiontable = clone($applicationtable);
47 $requesttable = clone($applicationtable);
50 $application = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cache', 'applicationtest');
51 $session = cache_definition::load_adhoc(cache_store::MODE_SESSION, 'cache', 'sessiontest');
52 $request = cache_definition::load_adhoc(cache_store::MODE_REQUEST, 'cache', 'requesttest');
54 $strinvalidplugin = new lang_string('invalidplugin', 'cache');
55 $strunsupportedmode = new lang_string('unsupportedmode', 'cache');
56 $struntestable = new lang_string('untestable', 'cache');
57 $strtested = new lang_string('tested', 'cache');
58 $strnotready = new lang_string('storenotready', 'cache');
60 foreach (core_component::get_plugin_list_with_file('cachestore', 'lib.php', true) as $plugin => $path) {
62 $class = 'cachestore_'.$plugin;
63 $plugin = get_string('pluginname', 'cachestore_'.$plugin);
65 if (!class_exists($class) || !method_exists($class, 'initialise_test_instance') || !$class::are_requirements_met()) {
66 $applicationtable->data[] = array($plugin, $strinvalidplugin, '-', '-', '-', '-');
67 $sessiontable->data[] = array($plugin, $strinvalidplugin, '-', '-', '-', '-');
68 $requesttable->data[] = array($plugin, $strinvalidplugin, '-', '-', '-', '-');
69 continue;
72 if (!$class::is_supported_mode(cache_store::MODE_APPLICATION)) {
73 $applicationtable->data[] = array($plugin, $strunsupportedmode, '-', '-', '-', '-');
74 } else {
75 $store = $class::initialise_test_instance($application);
76 if ($store === false) {
77 $applicationtable->data[] = array($plugin, $struntestable, '-', '-', '-', '-');
78 } else if (!$store->is_ready()) {
79 $applicationtable->data[] = array($plugin, $strnotready, '-', '-', '-', '-');
80 } else {
81 $result = array($plugin, $strtested, 0, 0, 0);
82 $start = microtime(true);
83 for ($i = 0; $i < $count; $i++) {
84 $store->set('key'.$i, 'test data '.$i);
86 $result[2] = sprintf('%01.4f', microtime(true) - $start);
88 $start = microtime(true);
89 for ($i = 0; $i < $count; $i++) {
90 $store->get('key'.$i);
92 $result[3] = sprintf('%01.4f', microtime(true) - $start);
94 $start = microtime(true);
95 for ($i = 0; $i < $count; $i++) {
96 $store->get('fake'.$i);
98 $result[4] = sprintf('%01.4f', microtime(true) - $start);
100 $start = microtime(true);
101 for ($i = 0; $i < $count; $i++) {
102 $store->delete('key'.$i);
104 $result[5] = sprintf('%01.4f', microtime(true) - $start);
105 $applicationtable->data[] = $result;
106 $store->instance_deleted();
110 if (!$class::is_supported_mode(cache_store::MODE_SESSION)) {
111 $sessiontable->data[] = array($plugin, $strunsupportedmode, '-', '-', '-', '-');
112 } else {
113 $store = $class::initialise_test_instance($session);
114 if ($store === false) {
115 $sessiontable->data[] = array($plugin, $struntestable, '-', '-', '-', '-');
116 } else if (!$store->is_ready()) {
117 $sessiontable->data[] = array($plugin, $strnotready, '-', '-', '-', '-');
118 } else {
119 $result = array($plugin, $strtested, 0, 0, 0);
120 $start = microtime(true);
121 for ($i = 0; $i < $count; $i++) {
122 $store->set('key'.$i, 'test data '.$i);
124 $result[2] = sprintf('%01.4f', microtime(true) - $start);
126 $start = microtime(true);
127 for ($i = 0; $i < $count; $i++) {
128 $store->get('key'.$i);
130 $result[3] = sprintf('%01.4f', microtime(true) - $start);
132 $start = microtime(true);
133 for ($i = 0; $i < $count; $i++) {
134 $store->get('fake'.$i);
136 $result[4] = sprintf('%01.4f', microtime(true) - $start);
138 $start = microtime(true);
139 for ($i = 0; $i < $count; $i++) {
140 $store->delete('key'.$i);
142 $result[5] = sprintf('%01.4f', microtime(true) - $start);
143 $sessiontable->data[] = $result;
144 $store->instance_deleted();
148 if (!$class::is_supported_mode(cache_store::MODE_REQUEST)) {
149 $requesttable->data[] = array($plugin, $strunsupportedmode, '-', '-', '-', '-');
150 } else {
151 $store = $class::initialise_test_instance($request);
152 if ($store === false) {
153 $requesttable->data[] = array($plugin, $struntestable, '-', '-', '-', '-');
154 } else if (!$store->is_ready()) {
155 $requesttable->data[] = array($plugin, $strnotready, '-', '-', '-', '-');
156 } else {
157 $result = array($plugin, $strtested, 0, 0, 0);
158 $start = microtime(true);
159 for ($i = 0; $i < $count; $i++) {
160 $store->set('key'.$i, 'test data '.$i);
162 $result[2] = sprintf('%01.4f', microtime(true) - $start);
164 $start = microtime(true);
165 for ($i = 0; $i < $count; $i++) {
166 $store->get('key'.$i);
168 $result[3] = sprintf('%01.4f', microtime(true) - $start);
170 $start = microtime(true);
171 for ($i = 0; $i < $count; $i++) {
172 $store->get('fake'.$i);
174 $result[4] = sprintf('%01.4f', microtime(true) - $start);
176 $start = microtime(true);
177 for ($i = 0; $i < $count; $i++) {
178 $store->delete('key'.$i);
180 $result[5] = sprintf('%01.4f', microtime(true) - $start);
181 $requesttable->data[] = $result;
182 $store->instance_deleted();
188 echo $OUTPUT->header();
189 echo $OUTPUT->heading(get_string('storeperformance', 'cache', $count));
191 $possiblecounts = array(1, 10, 100, 500, 1000, 5000, 10000, 50000, 100000);
192 $links = array();
193 foreach ($possiblecounts as $pcount) {
194 $links[] = html_writer::link(new moodle_url($PAGE->url, array('count' => $pcount)), $pcount);
196 echo $OUTPUT->box_start('generalbox performance-test-counts');
197 echo get_string('requestcount', 'cache', join(', ', $links));
198 echo $OUTPUT->box_end();
200 echo $OUTPUT->heading(get_string('storeresults_application', 'cache'));
201 echo html_writer::table($applicationtable);
203 echo $OUTPUT->heading(get_string('storeresults_session', 'cache'));
204 echo html_writer::table($sessiontable);
206 echo $OUTPUT->heading(get_string('storeresults_request', 'cache'));
207 echo html_writer::table($requesttable);
209 echo $OUTPUT->footer();