Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: becce23e4a
Fetching contributors…

Cannot retrieve contributors at this time

file 202 lines (172 sloc) 7.712 kb
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
<?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/>.

/**
* Store performance test run + output script.
*
* @package core
* @category cache
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once('../config.php');
require_once($CFG->dirroot.'/lib/adminlib.php');
require_once($CFG->dirroot.'/cache/locallib.php');

$count = optional_param('count', 100, PARAM_INT);
$count = min($count, 100000);
$count = max($count, 0);

admin_externalpage_setup('cachetestperformance');

$applicationtable = new html_table();
$applicationtable->head = array(
    get_string('plugin', 'cache'),
    get_string('result', 'cache'),
    get_string('set', 'cache'),
    get_string('gethit', 'cache'),
    get_string('getmiss', 'cache'),
    get_string('delete', 'cache'),
);
$applicationtable->data = array();
$sessiontable = clone($applicationtable);
$requesttable = clone($applicationtable);


$application = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cache', 'applicationtest');
$session = cache_definition::load_adhoc(cache_store::MODE_SESSION, 'cache', 'sessiontest');
$request = cache_definition::load_adhoc(cache_store::MODE_REQUEST, 'cache', 'requesttest');

$strinvalidplugin = new lang_string('invalidplugin', 'cache');
$strunsupportedmode = new lang_string('unsupportedmode', 'cache');
$struntestable = new lang_string('untestable', 'cache');
$strtested = new lang_string('tested', 'cache');

foreach (get_plugin_list_with_file('cachestore', 'lib.php', true) as $plugin => $path) {

    $class = 'cachestore_'.$plugin;
    $plugin = get_string('pluginname', 'cachestore_'.$plugin);

    if (!class_exists($class) || !method_exists($class, 'initialise_test_instance') || !$class::are_requirements_met()) {
        $applicationtable->data[] = array($plugin, $strinvalidplugin, '-', '-', '-', '-');
        $sessiontable->data[] = array($plugin, $strinvalidplugin, '-', '-', '-', '-');
        $requesttable->data[] = array($plugin, $strinvalidplugin, '-', '-', '-', '-');
        continue;
    }

    if (!$class::is_supported_mode(cache_store::MODE_APPLICATION)) {
        $applicationtable->data[] = array($plugin, $strunsupportedmode, '-', '-', '-', '-');
    } else {
        $store = $class::initialise_test_instance($application);
        if ($store === false) {
            $applicationtable->data[] = array($plugin, $struntestable, '-', '-', '-', '-');
        } else {
            $result = array($plugin, $strtested, 0, 0, 0);
            $start = microtime(true);
            for ($i = 0; $i < $count; $i++) {
                $store->set('key'.$i, 'test data '.$i);
            }
            $result[2] = sprintf('%01.4f', microtime(true) - $start);

            $start = microtime(true);
            for ($i = 0; $i < $count; $i++) {
                $store->get('key'.$i);
            }
            $result[3] = sprintf('%01.4f', microtime(true) - $start);

            $start = microtime(true);
            for ($i = 0; $i < $count; $i++) {
                $store->get('fake'.$i);
            }
            $result[4] = sprintf('%01.4f', microtime(true) - $start);

            $start = microtime(true);
            for ($i = 0; $i < $count; $i++) {
                $store->delete('key'.$i);
            }
            $result[5] = sprintf('%01.4f', microtime(true) - $start);
            $applicationtable->data[] = $result;
            $store->instance_deleted();
        }
    }

    if (!$class::is_supported_mode(cache_store::MODE_SESSION)) {
        $sessiontable->data[] = array($plugin, $strunsupportedmode, '-', '-', '-', '-');
    } else {
        $store = $class::initialise_test_instance($session);
        if ($store === false) {
            $sessiontable->data[] = array($plugin, $struntestable, '-', '-', '-', '-');
        } else {
            $result = array($plugin, $strtested, 0, 0, 0);
            $start = microtime(true);
            for ($i = 0; $i < $count; $i++) {
                $store->set('key'.$i, 'test data '.$i);
            }
            $result[2] = sprintf('%01.4f', microtime(true) - $start);

            $start = microtime(true);
            for ($i = 0; $i < $count; $i++) {
                $store->get('key'.$i);
            }
            $result[3] = sprintf('%01.4f', microtime(true) - $start);

            $start = microtime(true);
            for ($i = 0; $i < $count; $i++) {
                $store->get('fake'.$i);
            }
            $result[4] = sprintf('%01.4f', microtime(true) - $start);

            $start = microtime(true);
            for ($i = 0; $i < $count; $i++) {
                $store->delete('key'.$i);
            }
            $result[5] = sprintf('%01.4f', microtime(true) - $start);
            $sessiontable->data[] = $result;
            $store->instance_deleted();
        }
    }

    if (!$class::is_supported_mode(cache_store::MODE_REQUEST)) {
        $requesttable->data[] = array($plugin, $strunsupportedmode, '-', '-', '-', '-');
    } else {
        $store = $class::initialise_test_instance($request);
        if ($store === false) {
            $requesttable->data[] = array($plugin, $struntestable, '-', '-', '-', '-');
        } else {
            $result = array($plugin, $strtested, 0, 0, 0);
            $start = microtime(true);
            for ($i = 0; $i < $count; $i++) {
                $store->set('key'.$i, 'test data '.$i);
            }
            $result[2] = sprintf('%01.4f', microtime(true) - $start);

            $start = microtime(true);
            for ($i = 0; $i < $count; $i++) {
                $store->get('key'.$i);
            }
            $result[3] = sprintf('%01.4f', microtime(true) - $start);

            $start = microtime(true);
            for ($i = 0; $i < $count; $i++) {
                $store->get('fake'.$i);
            }
            $result[4] = sprintf('%01.4f', microtime(true) - $start);

            $start = microtime(true);
            for ($i = 0; $i < $count; $i++) {
                $store->delete('key'.$i);
            }
            $result[5] = sprintf('%01.4f', microtime(true) - $start);
            $requesttable->data[] = $result;
            $store->instance_deleted();
        }
    }

}

echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('storeperformance', 'cache', $count));

$possiblecounts = array(1, 10, 100, 500, 1000, 5000, 10000, 50000, 100000);
$links = array();
foreach ($possiblecounts as $pcount) {
    $links[] = html_writer::link(new moodle_url($PAGE->url, array('count' => $pcount)), $pcount);
}
echo $OUTPUT->box_start('generalbox performance-test-counts');
echo get_string('requestcount', 'cache', join(', ', $links));
echo $OUTPUT->box_end();

echo $OUTPUT->heading(get_string('storeresults_application', 'cache'));
echo html_writer::table($applicationtable);

echo $OUTPUT->heading(get_string('storeresults_session', 'cache'));
echo html_writer::table($sessiontable);

echo $OUTPUT->heading(get_string('storeresults_request', 'cache'));
echo html_writer::table($requesttable);

echo $OUTPUT->footer();
Something went wrong with that request. Please try again.