PageRenderTime 59ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/cache/testperformance.php

https://bitbucket.org/moodle/moodle
PHP | 209 lines | 157 code | 30 blank | 22 comment | 33 complexity | c3b60a813eee33fcc5ae2b4039d03c4d MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. * Store performance test run + output script.
  18. *
  19. * @package core
  20. * @category cache
  21. * @copyright 2012 Sam Hemelryk
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. require_once('../config.php');
  25. require_once($CFG->dirroot.'/lib/adminlib.php');
  26. require_once($CFG->dirroot.'/cache/locallib.php');
  27. $count = optional_param('count', 100, PARAM_INT);
  28. $count = min($count, 100000);
  29. $count = max($count, 0);
  30. admin_externalpage_setup('cachetestperformance');
  31. $applicationtable = new html_table();
  32. $applicationtable->head = array(
  33. get_string('plugin', 'cache'),
  34. get_string('result', 'cache'),
  35. get_string('set', 'cache'),
  36. get_string('gethit', 'cache'),
  37. get_string('getmiss', 'cache'),
  38. get_string('delete', 'cache'),
  39. );
  40. $applicationtable->data = array();
  41. $sessiontable = clone($applicationtable);
  42. $requesttable = clone($applicationtable);
  43. $application = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cache', 'applicationtest');
  44. $session = cache_definition::load_adhoc(cache_store::MODE_SESSION, 'cache', 'sessiontest');
  45. $request = cache_definition::load_adhoc(cache_store::MODE_REQUEST, 'cache', 'requesttest');
  46. $strinvalidplugin = new lang_string('invalidplugin', 'cache');
  47. $strunsupportedmode = new lang_string('unsupportedmode', 'cache');
  48. $struntestable = new lang_string('untestable', 'cache');
  49. $strtested = new lang_string('tested', 'cache');
  50. $strnotready = new lang_string('storenotready', 'cache');
  51. foreach (core_component::get_plugin_list_with_file('cachestore', 'lib.php', true) as $plugin => $path) {
  52. $class = 'cachestore_'.$plugin;
  53. $plugin = get_string('pluginname', 'cachestore_'.$plugin);
  54. if (!class_exists($class) || !method_exists($class, 'initialise_test_instance') || !$class::are_requirements_met()) {
  55. $applicationtable->data[] = array($plugin, $strinvalidplugin, '-', '-', '-', '-');
  56. $sessiontable->data[] = array($plugin, $strinvalidplugin, '-', '-', '-', '-');
  57. $requesttable->data[] = array($plugin, $strinvalidplugin, '-', '-', '-', '-');
  58. continue;
  59. }
  60. if (!$class::is_supported_mode(cache_store::MODE_APPLICATION)) {
  61. $applicationtable->data[] = array($plugin, $strunsupportedmode, '-', '-', '-', '-');
  62. } else {
  63. $store = $class::initialise_test_instance($application);
  64. if ($store === false) {
  65. $applicationtable->data[] = array($plugin, $struntestable, '-', '-', '-', '-');
  66. } else if (!$store->is_ready()) {
  67. $applicationtable->data[] = array($plugin, $strnotready, '-', '-', '-', '-');
  68. } else {
  69. $result = array($plugin, $strtested, 0, 0, 0);
  70. $start = microtime(true);
  71. for ($i = 0; $i < $count; $i++) {
  72. $store->set('key'.$i, 'test data '.$i);
  73. }
  74. $result[2] = sprintf('%01.4f', microtime(true) - $start);
  75. $start = microtime(true);
  76. for ($i = 0; $i < $count; $i++) {
  77. $store->get('key'.$i);
  78. }
  79. $result[3] = sprintf('%01.4f', microtime(true) - $start);
  80. $start = microtime(true);
  81. for ($i = 0; $i < $count; $i++) {
  82. $store->get('fake'.$i);
  83. }
  84. $result[4] = sprintf('%01.4f', microtime(true) - $start);
  85. $start = microtime(true);
  86. for ($i = 0; $i < $count; $i++) {
  87. $store->delete('key'.$i);
  88. }
  89. $result[5] = sprintf('%01.4f', microtime(true) - $start);
  90. $applicationtable->data[] = $result;
  91. $store->instance_deleted();
  92. }
  93. }
  94. if (!$class::is_supported_mode(cache_store::MODE_SESSION)) {
  95. $sessiontable->data[] = array($plugin, $strunsupportedmode, '-', '-', '-', '-');
  96. } else {
  97. $store = $class::initialise_test_instance($session);
  98. if ($store === false) {
  99. $sessiontable->data[] = array($plugin, $struntestable, '-', '-', '-', '-');
  100. } else if (!$store->is_ready()) {
  101. $sessiontable->data[] = array($plugin, $strnotready, '-', '-', '-', '-');
  102. } else {
  103. $result = array($plugin, $strtested, 0, 0, 0);
  104. $start = microtime(true);
  105. for ($i = 0; $i < $count; $i++) {
  106. $store->set('key'.$i, 'test data '.$i);
  107. }
  108. $result[2] = sprintf('%01.4f', microtime(true) - $start);
  109. $start = microtime(true);
  110. for ($i = 0; $i < $count; $i++) {
  111. $store->get('key'.$i);
  112. }
  113. $result[3] = sprintf('%01.4f', microtime(true) - $start);
  114. $start = microtime(true);
  115. for ($i = 0; $i < $count; $i++) {
  116. $store->get('fake'.$i);
  117. }
  118. $result[4] = sprintf('%01.4f', microtime(true) - $start);
  119. $start = microtime(true);
  120. for ($i = 0; $i < $count; $i++) {
  121. $store->delete('key'.$i);
  122. }
  123. $result[5] = sprintf('%01.4f', microtime(true) - $start);
  124. $sessiontable->data[] = $result;
  125. $store->instance_deleted();
  126. }
  127. }
  128. if (!$class::is_supported_mode(cache_store::MODE_REQUEST)) {
  129. $requesttable->data[] = array($plugin, $strunsupportedmode, '-', '-', '-', '-');
  130. } else {
  131. $store = $class::initialise_test_instance($request);
  132. if ($store === false) {
  133. $requesttable->data[] = array($plugin, $struntestable, '-', '-', '-', '-');
  134. } else if (!$store->is_ready()) {
  135. $requesttable->data[] = array($plugin, $strnotready, '-', '-', '-', '-');
  136. } else {
  137. $result = array($plugin, $strtested, 0, 0, 0);
  138. $start = microtime(true);
  139. for ($i = 0; $i < $count; $i++) {
  140. $store->set('key'.$i, 'test data '.$i);
  141. }
  142. $result[2] = sprintf('%01.4f', microtime(true) - $start);
  143. $start = microtime(true);
  144. for ($i = 0; $i < $count; $i++) {
  145. $store->get('key'.$i);
  146. }
  147. $result[3] = sprintf('%01.4f', microtime(true) - $start);
  148. $start = microtime(true);
  149. for ($i = 0; $i < $count; $i++) {
  150. $store->get('fake'.$i);
  151. }
  152. $result[4] = sprintf('%01.4f', microtime(true) - $start);
  153. $start = microtime(true);
  154. for ($i = 0; $i < $count; $i++) {
  155. $store->delete('key'.$i);
  156. }
  157. $result[5] = sprintf('%01.4f', microtime(true) - $start);
  158. $requesttable->data[] = $result;
  159. $store->instance_deleted();
  160. }
  161. }
  162. }
  163. echo $OUTPUT->header();
  164. echo $OUTPUT->heading(get_string('storeperformance', 'cache', $count));
  165. $possiblecounts = array(1, 10, 100, 500, 1000, 5000, 10000, 50000, 100000);
  166. $links = array();
  167. foreach ($possiblecounts as $pcount) {
  168. $links[] = html_writer::link(new moodle_url($PAGE->url, array('count' => $pcount)), $pcount);
  169. }
  170. echo $OUTPUT->box_start('generalbox performance-test-counts');
  171. echo get_string('requestcount', 'cache', join(', ', $links));
  172. echo $OUTPUT->box_end();
  173. echo $OUTPUT->heading(get_string('storeresults_application', 'cache'));
  174. echo html_writer::table($applicationtable);
  175. echo $OUTPUT->heading(get_string('storeresults_session', 'cache'));
  176. echo html_writer::table($sessiontable);
  177. echo $OUTPUT->heading(get_string('storeresults_request', 'cache'));
  178. echo html_writer::table($requesttable);
  179. echo $OUTPUT->footer();