codebench.php 10 KB

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