Merge branch 'MDL-37943_24' of git://github.com/timhunt/moodle into MOODLE_24_STABLE
[moodle.git] / cache / testperformance.php
bloba4704dc25c85b1f3e7df340ab80f6509a0d48733
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');
59 foreach (get_plugin_list_with_file('cachestore', 'lib.php', true) as $plugin => $path) {
61 $class = 'cachestore_'.$plugin;
62 $plugin = get_string('pluginname', 'cachestore_'.$plugin);
64 if (!class_exists($class) || !method_exists($class, 'initialise_test_instance') || !$class::are_requirements_met()) {
65 $applicationtable->data[] = array($plugin, $strinvalidplugin, '-', '-', '-', '-');
66 $sessiontable->data[] = array($plugin, $strinvalidplugin, '-', '-', '-', '-');
67 $requesttable->data[] = array($plugin, $strinvalidplugin, '-', '-', '-', '-');
68 continue;
71 if (!$class::is_supported_mode(cache_store::MODE_APPLICATION)) {
72 $applicationtable->data[] = array($plugin, $strunsupportedmode, '-', '-', '-', '-');
73 } else {
74 $store = $class::initialise_test_instance($application);
75 if ($store === false) {
76 $applicationtable->data[] = array($plugin, $struntestable, '-', '-', '-', '-');
77 } else {
78 $result = array($plugin, $strtested, 0, 0, 0);
79 $start = microtime(true);
80 for ($i = 0; $i < $count; $i++) {
81 $store->set('key'.$i, 'test data '.$i);
83 $result[2] = sprintf('%01.4f', microtime(true) - $start);
85 $start = microtime(true);
86 for ($i = 0; $i < $count; $i++) {
87 $store->get('key'.$i);
89 $result[3] = sprintf('%01.4f', microtime(true) - $start);
91 $start = microtime(true);
92 for ($i = 0; $i < $count; $i++) {
93 $store->get('fake'.$i);
95 $result[4] = sprintf('%01.4f', microtime(true) - $start);
97 $start = microtime(true);
98 for ($i = 0; $i < $count; $i++) {
99 $store->delete('key'.$i);
101 $result[5] = sprintf('%01.4f', microtime(true) - $start);
102 $applicationtable->data[] = $result;
103 $store->instance_deleted();
107 if (!$class::is_supported_mode(cache_store::MODE_SESSION)) {
108 $sessiontable->data[] = array($plugin, $strunsupportedmode, '-', '-', '-', '-');
109 } else {
110 $store = $class::initialise_test_instance($session);
111 if ($store === false) {
112 $sessiontable->data[] = array($plugin, $struntestable, '-', '-', '-', '-');
113 } else {
114 $result = array($plugin, $strtested, 0, 0, 0);
115 $start = microtime(true);
116 for ($i = 0; $i < $count; $i++) {
117 $store->set('key'.$i, 'test data '.$i);
119 $result[2] = sprintf('%01.4f', microtime(true) - $start);
121 $start = microtime(true);
122 for ($i = 0; $i < $count; $i++) {
123 $store->get('key'.$i);
125 $result[3] = sprintf('%01.4f', microtime(true) - $start);
127 $start = microtime(true);
128 for ($i = 0; $i < $count; $i++) {
129 $store->get('fake'.$i);
131 $result[4] = sprintf('%01.4f', microtime(true) - $start);
133 $start = microtime(true);
134 for ($i = 0; $i < $count; $i++) {
135 $store->delete('key'.$i);
137 $result[5] = sprintf('%01.4f', microtime(true) - $start);
138 $sessiontable->data[] = $result;
139 $store->instance_deleted();
143 if (!$class::is_supported_mode(cache_store::MODE_REQUEST)) {
144 $requesttable->data[] = array($plugin, $strunsupportedmode, '-', '-', '-', '-');
145 } else {
146 $store = $class::initialise_test_instance($request);
147 if ($store === false) {
148 $requesttable->data[] = array($plugin, $struntestable, '-', '-', '-', '-');
149 } else {
150 $result = array($plugin, $strtested, 0, 0, 0);
151 $start = microtime(true);
152 for ($i = 0; $i < $count; $i++) {
153 $store->set('key'.$i, 'test data '.$i);
155 $result[2] = sprintf('%01.4f', microtime(true) - $start);
157 $start = microtime(true);
158 for ($i = 0; $i < $count; $i++) {
159 $store->get('key'.$i);
161 $result[3] = sprintf('%01.4f', microtime(true) - $start);
163 $start = microtime(true);
164 for ($i = 0; $i < $count; $i++) {
165 $store->get('fake'.$i);
167 $result[4] = sprintf('%01.4f', microtime(true) - $start);
169 $start = microtime(true);
170 for ($i = 0; $i < $count; $i++) {
171 $store->delete('key'.$i);
173 $result[5] = sprintf('%01.4f', microtime(true) - $start);
174 $requesttable->data[] = $result;
175 $store->instance_deleted();
181 echo $OUTPUT->header();
182 echo $OUTPUT->heading(get_string('storeperformance', 'cache', $count));
184 $possiblecounts = array(1, 10, 100, 500, 1000, 5000, 10000, 50000, 100000);
185 $links = array();
186 foreach ($possiblecounts as $pcount) {
187 $links[] = html_writer::link(new moodle_url($PAGE->url, array('count' => $pcount)), $pcount);
189 echo $OUTPUT->box_start('generalbox performance-test-counts');
190 echo get_string('requestcount', 'cache', join(', ', $links));
191 echo $OUTPUT->box_end();
193 echo $OUTPUT->heading(get_string('storeresults_application', 'cache'));
194 echo html_writer::table($applicationtable);
196 echo $OUTPUT->heading(get_string('storeresults_session', 'cache'));
197 echo html_writer::table($sessiontable);
199 echo $OUTPUT->heading(get_string('storeresults_request', 'cache'));
200 echo html_writer::table($requesttable);
202 echo $OUTPUT->footer();