MDL-67264 core_course: Activity chooser new feature
[moodle.git] / cache / testperformance.php
CommitLineData
8139ad13
SH
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 * Store performance test run + output script.
19 *
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
24 */
25
26require_once('../config.php');
27require_once($CFG->dirroot.'/lib/adminlib.php');
28require_once($CFG->dirroot.'/cache/locallib.php');
29
30$count = optional_param('count', 100, PARAM_INT);
31$count = min($count, 100000);
32$count = max($count, 0);
33
34admin_externalpage_setup('cachetestperformance');
35
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'),
44);
45$applicationtable->data = array();
46$sessiontable = clone($applicationtable);
47$requesttable = clone($applicationtable);
48
49
2566210c
SH
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');
8139ad13
SH
53
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');
c643628f 58$strnotready = new lang_string('storenotready', 'cache');
8139ad13 59
d0cac8b5 60foreach (core_component::get_plugin_list_with_file('cachestore', 'lib.php', true) as $plugin => $path) {
8139ad13 61
6fec1820
SH
62 $class = 'cachestore_'.$plugin;
63 $plugin = get_string('pluginname', 'cachestore_'.$plugin);
8139ad13 64
f0ce225c 65 if (!class_exists($class) || !method_exists($class, 'initialise_test_instance') || !$class::are_requirements_met()) {
8139ad13
SH
66 $applicationtable->data[] = array($plugin, $strinvalidplugin, '-', '-', '-', '-');
67 $sessiontable->data[] = array($plugin, $strinvalidplugin, '-', '-', '-', '-');
68 $requesttable->data[] = array($plugin, $strinvalidplugin, '-', '-', '-', '-');
69 continue;
70 }
71
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, '-', '-', '-', '-');
c643628f
SH
78 } else if (!$store->is_ready()) {
79 $applicationtable->data[] = array($plugin, $strnotready, '-', '-', '-', '-');
8139ad13
SH
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);
85 }
86 $result[2] = sprintf('%01.4f', microtime(true) - $start);
87
88 $start = microtime(true);
89 for ($i = 0; $i < $count; $i++) {
90 $store->get('key'.$i);
91 }
92 $result[3] = sprintf('%01.4f', microtime(true) - $start);
93
94 $start = microtime(true);
95 for ($i = 0; $i < $count; $i++) {
96 $store->get('fake'.$i);
97 }
98 $result[4] = sprintf('%01.4f', microtime(true) - $start);
99
100 $start = microtime(true);
101 for ($i = 0; $i < $count; $i++) {
102 $store->delete('key'.$i);
103 }
104 $result[5] = sprintf('%01.4f', microtime(true) - $start);
105 $applicationtable->data[] = $result;
a037c943 106 $store->instance_deleted();
8139ad13
SH
107 }
108 }
109
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, '-', '-', '-', '-');
c643628f
SH
116 } else if (!$store->is_ready()) {
117 $sessiontable->data[] = array($plugin, $strnotready, '-', '-', '-', '-');
8139ad13
SH
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);
123 }
124 $result[2] = sprintf('%01.4f', microtime(true) - $start);
125
126 $start = microtime(true);
127 for ($i = 0; $i < $count; $i++) {
128 $store->get('key'.$i);
129 }
130 $result[3] = sprintf('%01.4f', microtime(true) - $start);
131
132 $start = microtime(true);
133 for ($i = 0; $i < $count; $i++) {
134 $store->get('fake'.$i);
135 }
136 $result[4] = sprintf('%01.4f', microtime(true) - $start);
137
138 $start = microtime(true);
139 for ($i = 0; $i < $count; $i++) {
140 $store->delete('key'.$i);
141 }
142 $result[5] = sprintf('%01.4f', microtime(true) - $start);
143 $sessiontable->data[] = $result;
a037c943 144 $store->instance_deleted();
8139ad13
SH
145 }
146 }
147
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, '-', '-', '-', '-');
c643628f
SH
154 } else if (!$store->is_ready()) {
155 $requesttable->data[] = array($plugin, $strnotready, '-', '-', '-', '-');
8139ad13
SH
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);
161 }
162 $result[2] = sprintf('%01.4f', microtime(true) - $start);
163
164 $start = microtime(true);
165 for ($i = 0; $i < $count; $i++) {
166 $store->get('key'.$i);
167 }
168 $result[3] = sprintf('%01.4f', microtime(true) - $start);
169
170 $start = microtime(true);
171 for ($i = 0; $i < $count; $i++) {
172 $store->get('fake'.$i);
173 }
174 $result[4] = sprintf('%01.4f', microtime(true) - $start);
175
176 $start = microtime(true);
177 for ($i = 0; $i < $count; $i++) {
178 $store->delete('key'.$i);
179 }
180 $result[5] = sprintf('%01.4f', microtime(true) - $start);
181 $requesttable->data[] = $result;
a037c943 182 $store->instance_deleted();
8139ad13
SH
183 }
184 }
185
186}
187
188echo $OUTPUT->header();
189echo $OUTPUT->heading(get_string('storeperformance', 'cache', $count));
190
191$possiblecounts = array(1, 10, 100, 500, 1000, 5000, 10000, 50000, 100000);
192$links = array();
193foreach ($possiblecounts as $pcount) {
194 $links[] = html_writer::link(new moodle_url($PAGE->url, array('count' => $pcount)), $pcount);
195}
196echo $OUTPUT->box_start('generalbox performance-test-counts');
197echo get_string('requestcount', 'cache', join(', ', $links));
198echo $OUTPUT->box_end();
199
200echo $OUTPUT->heading(get_string('storeresults_application', 'cache'));
201echo html_writer::table($applicationtable);
202
203echo $OUTPUT->heading(get_string('storeresults_session', 'cache'));
204echo html_writer::table($sessiontable);
205
206echo $OUTPUT->heading(get_string('storeresults_request', 'cache'));
207echo html_writer::table($requesttable);
208
6fec1820 209echo $OUTPUT->footer();