codebench.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /**
  3. * Codebench — A benchmarking module.
  4. *
  5. * @package koseven/Codebench
  6. *
  7. * @copyright (c) 2007-2016 Kohana Team
  8. * @copyright (c) since 2016 Koseven Team
  9. * @license https://koseven.dev/LICENSE
  10. */
  11. ?>
  12. <!DOCTYPE html>
  13. <html lang="en">
  14. <head>
  15. <meta charset="utf-8" />
  16. <title><?php if ($class !== ''): ?>
  17. <?php echo $class, ' · ' ?>
  18. <?php endif; ?>Codebench</title>
  19. <style>
  20. /* General styles*/
  21. body { position:relative; margin:1em 2em; font:12px monaco,monospace; }
  22. h1 { font-size:24px; letter-spacing:-0.05em; }
  23. h2 { font-size:18px; letter-spacing:-0.1em; }
  24. input, code { font:inherit; }
  25. code { background:#e5e5e5; }
  26. caption { display:none; }
  27. /* Form */
  28. #runner { margin-bottom:2em; }
  29. #runner input[type="text"] { letter-spacing:-0.05em; }
  30. /* Expand/Collapse all */
  31. #toggle_all { position:absolute; top:0; right:0; margin:0; padding:0 4px; background:#000; font-size:18px; color:#fff; cursor:pointer; -moz-border-radius:2px; -webkit-border-radius:2px; }
  32. /* Benchmark main graphs */
  33. #bench { margin:2em 0; padding:0; list-style:none; }
  34. #bench > li { margin:6px 0; }
  35. #bench h2 { position:relative; margin:0; padding:2px; background:#ccc; border:1px solid #999; cursor:pointer; -moz-border-radius:3px; -webkit-border-radius:3px; }
  36. #bench h2 > span { display:block; min-width:1px; height:33px; background:#fff; -moz-border-radius:2px; -webkit-border-radius:2px; }
  37. #bench h2 .method { position:absolute; top:6px; left:8px; text-shadow:0 -1px 0 rgba(255,255,255,0.6); }
  38. #bench h2 .method:before { content:'▸ '; }
  39. #bench h2 .percent { position:absolute; top:6px; right:6px; padding:0 4px; background:#000; color:#fff; font-weight:normal; letter-spacing:0; -moz-border-radius:2px; -webkit-border-radius:2px; }
  40. #bench h2:hover .method { left:10px; }
  41. #bench h2.expanded { margin:12px 0 0; -moz-border-radius-bottomleft:0; -moz-border-radius-bottomright:0; -webkit-border-bottom-left-radius:0; -webkit-border-bottom-right-radius:0; }
  42. #bench h2.expanded .method:before { content:'▾ '; }
  43. /* Colorization of the bars */
  44. #bench .grade-A { background:#3f0; }
  45. #bench .grade-B { background:#fc0; }
  46. #bench .grade-C { background:#f90; }
  47. #bench .grade-D { background:#f60; }
  48. #bench .grade-E { background:#f30; }
  49. #bench .grade-F { background:#f00; }
  50. /* Benchmark details */
  51. #bench > li > div { display:none; margin:0 0 12px; padding:0 0 2px; background:#eee; border:1px solid #999; border-top:0; -moz-border-radius-bottomleft:3px; -moz-border-radius-bottomright:3px; -webkit-border-bottom-left-radius:3px; -webkit-border-bottom-right-radius:3px; }
  52. #bench > li > div table { width:100%; background:#eee; border-collapse:collapse; }
  53. #bench > li > div th { padding:6px; background:#ddd url(data:image/gif;base64,R0lGODlhAQASALMAAMfHx8TExM7Oztvb29jY2NbW1tPT09DQ0MrKygAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAAAAAAALAAAAAABABIAAAQNMAQAEBLiHGNKIcQwRAA7) repeat-x 0 1px; text-align:left; }
  54. #bench > li > div td { padding:6px; border-top:1px solid #ccc; vertical-align:top; }
  55. #bench .numeric { padding-left:18px; text-align:right; }
  56. #bench .numeric span { position:relative; display:block; height:16px; }
  57. #bench .numeric span span { position:absolute; top:0; right:0; min-width:1px; background:#ccc; -moz-border-radius:2px; -webkit-border-radius:2px; }
  58. #bench .numeric span span span { top:0; right:0; background:none; }
  59. #bench tbody tr:hover { background:#fff; }
  60. #bench tbody tr.highlight { background:#ffc; }
  61. /* Footer */
  62. #footer { margin-top:2em; padding-top:1em; border-top:1px solid #ccc; color:#999; }
  63. #footer a { color:inherit; }
  64. /* Misc text styles */
  65. .alert { padding:0 0.5em; background:#900; font-weight:normal; color:#fff; -moz-border-radius:3px; -webkit-border-radius:3px; }
  66. .quiet { color:#999; }
  67. .help { cursor:help; }
  68. </style>
  69. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  70. <script>
  71. $(document).ready(function() {
  72. // Insert "Toggle All" button
  73. var expand_all_text = '▸ Expand all';
  74. var collapse_all_text = '▾ Collapse all';
  75. $('#bench').before('<p id="toggle_all">'+expand_all_text+'</p>');
  76. // Cache these selection operations
  77. var $runner = $('#runner');
  78. var $toggle_all = $('#toggle_all');
  79. var $bench_titles = $('#bench > li > h2');
  80. var $bench_rows = $('#bench > li > div > table > tbody > tr');
  81. // Runner form
  82. $(':input:first', $runner).focus();
  83. $runner.submit(function() {
  84. $(':submit', this).attr('value', 'Running…').attr('disabled', 'disabled');
  85. $('.alert', this).remove();
  86. });
  87. // Toggle details for all benchmarks
  88. $('#toggle_all').click(function() {
  89. if ($(this).data('expanded')) {
  90. $(this).data('expanded', false);
  91. $(this).text(expand_all_text);
  92. $bench_titles.removeClass('expanded').siblings().hide();
  93. }
  94. else {
  95. $(this).data('expanded', true);
  96. $(this).text(collapse_all_text);
  97. $bench_titles.addClass('expanded').siblings().show();
  98. }
  99. });
  100. <?php if (KO7::$config->load('codebench')->expand_all) { ?>
  101. // Expand all benchmark details by default
  102. $toggle_all.click();
  103. <?php } ?>
  104. // Toggle details for a single benchmark
  105. $bench_titles.click(function() {
  106. $(this).toggleClass('expanded').siblings().toggle();
  107. // Counts of bench titles
  108. var total_bench_titles = $bench_titles.length;
  109. var expanded_bench_titles = $bench_titles.filter('.expanded').length;
  110. // If no benchmark details are expanded, change "Collapse all" to "Expand all"
  111. if (expanded_bench_titles == 0 && $toggle_all.data('expanded')) {
  112. $toggle_all.click();
  113. }
  114. // If all benchmark details are expanded, change "Expand all" to "Collapse all"
  115. else if (expanded_bench_titles == total_bench_titles && ! $toggle_all.data('expanded')) {
  116. $toggle_all.click();
  117. }
  118. });
  119. // Highlight clicked rows
  120. $bench_rows.click(function() {
  121. $(this).toggleClass('highlight');
  122. // Highlight doubleclicked rows globally
  123. }).dblclick(function() {
  124. var nth_row = $(this).parent().children().index(this) + 1;
  125. if ($(this).hasClass('highlight')) {
  126. $bench_rows.filter(':nth-child('+nth_row+')').removeClass('highlight');
  127. }
  128. else {
  129. $bench_rows.filter(':nth-child('+nth_row+')').addClass('highlight');
  130. }
  131. });
  132. });
  133. </script>
  134. </head>
  135. <body>
  136. <!--[if IE]><p class="alert">This page is not meant to be viewed in Internet Explorer. Get a better browser.</p><![endif]-->
  137. <form id="runner" method="post" action="<?php echo URL::site('codebench') ?>">
  138. <h1>
  139. <input name="class" type="text" value="<?php echo ($class !== '') ? $class : 'Bench_' ?>" size="25" title="Name of the Codebench library to run" />
  140. <input type="submit" value="Run" />
  141. <?php if ( ! empty($class)) { ?>
  142. <?php if (empty($codebench)) { ?>
  143. <strong class="alert">Library not found</strong>
  144. <?php } elseif (empty($codebench['benchmarks'])) { ?>
  145. <strong class="alert">No methods found to benchmark</strong>
  146. <?php } ?>
  147. <?php } ?>
  148. </h1>
  149. </form>
  150. <?php if ( ! empty($codebench)) { ?>
  151. <?php if (empty($codebench['benchmarks'])) { ?>
  152. <p>
  153. <strong>
  154. Remember to prefix the methods you want to benchmark with “bench”.<br />
  155. You might also want to overwrite <code>Codebench->method_filter()</code>.
  156. </strong>
  157. </p>
  158. <?php } else { ?>
  159. <ul id="bench">
  160. <?php foreach ($codebench['benchmarks'] as $method => $benchmark) { ?>
  161. <li>
  162. <h2 title="<?php printf('%01.6f', $benchmark['time']) ?>s">
  163. <span class="grade-<?php echo $benchmark['grade']['time'] ?>" style="width:<?php echo $benchmark['percent']['slowest']['time'] ?>%">
  164. <span class="method"><?php echo $method ?></span>
  165. <span class="percent">+<?php echo (int) $benchmark['percent']['fastest']['time'] ?>%</span>
  166. </span>
  167. </h2>
  168. <div>
  169. <table>
  170. <caption>Benchmarks per subject for <?php echo $method ?></caption>
  171. <thead>
  172. <tr>
  173. <th style="width:50%">subject → return</th>
  174. <th class="numeric" style="width:25%" title="Total method memory"><?php echo Text::bytes($benchmark['memory'], 'MB', '%01.6f%s') ?></th>
  175. <th class="numeric" style="width:25%" title="Total method time"><?php printf('%01.6f', $benchmark['time']) ?>s</th>
  176. </tr>
  177. </thead>
  178. <tbody>
  179. <?php foreach ($benchmark['subjects'] as $subject_key => $subject) { ?>
  180. <tr>
  181. <td>
  182. <strong class="help" title="(<?php echo gettype($codebench['subjects'][$subject_key]) ?>) <?php echo HTML::chars(var_export($codebench['subjects'][$subject_key], TRUE)) ?>">
  183. [<?php echo HTML::chars($subject_key) ?>] →
  184. </strong>
  185. <span class="quiet">(<?php echo gettype($subject['return']) ?>)</span>
  186. <?php echo HTML::chars(var_export($subject['return'], TRUE)) ?>
  187. </td>
  188. <td class="numeric">
  189. <span title="+<?php echo (int) $subject['percent']['fastest']['memory'] ?>% memory">
  190. <span style="width:<?php echo $subject['percent']['slowest']['memory'] ?>%">
  191. <span><?php echo Text::bytes($subject['memory'], 'MB', '%01.6f%s') ?></span>
  192. </span>
  193. </span>
  194. </td>
  195. <td class="numeric">
  196. <span title="+<?php echo (int) $subject['percent']['fastest']['time'] ?>% time">
  197. <span style="width:<?php echo $subject['percent']['slowest']['time'] ?>%">
  198. <span><?php printf('%01.6f', $subject['time']) ?>s</span>
  199. </span>
  200. </span>
  201. </td>
  202. </tr>
  203. <?php } ?>
  204. </tbody>
  205. </table>
  206. </div>
  207. </li>
  208. <?php } ?>
  209. </ul>
  210. <?php } ?>
  211. <?php if ( ! empty($codebench['description'])) { ?>
  212. <?php echo Text::auto_p(Text::auto_link($codebench['description']), FALSE) ?>
  213. <?php } ?>
  214. <?php // echo '<h2>Raw output:</h2>', Debug::vars($codebench) ?>
  215. <?php } ?>
  216. <p id="footer">
  217. Page executed in <strong><?php echo round(microtime(TRUE) - KO7_START_TIME, 2) ?>&nbsp;s</strong>
  218. using <strong><?php echo Text::widont(Text::bytes(memory_get_usage(), 'MB')) ?></strong> of memory.<br />
  219. <a href="http://github.com/kohana/codebench">Codebench</a>, a <a href="http://koseven.dev/">KO7</a> module
  220. by <a href="http://www.geertdedeckere.be/article/introducing-codebench">Geert De Deckere</a>.
  221. </p>
  222. </body>
  223. </html>