Tuurlijk

Ternary Test

Dec 15th, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.27 KB | None | 0 0
  1. <?php
  2. /*****************************************************************************
  3.  *  Copyright notice
  4.  *
  5.  *  ⓒ 2013 Michiel Roos <[email protected]>
  6.  *  All rights reserved
  7.  *
  8.  *  This script is part of the TYPO3 project. The TYPO3 project is free
  9.  *  software; you can redistribute it and/or modify it under the terms of the
  10.  *  GNU General Public License as published by the Free Software Foundation;
  11.  *  either version 2 of the License, or (at your option) any later version.
  12.  *
  13.  *  The GNU General Public License can be found at
  14.  *  http://www.gnu.org/copyleft/gpl.html.
  15.  *
  16.  *  This script is distributed in the hope that it will be useful, but
  17.  *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  18.  *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  19.  *  more details.
  20.  *
  21.  *  This copyright notice MUST APPEAR in all copies of the script!
  22.  ****************************************************************************/
  23.  
  24. // Setup
  25. $testName = 'Ternary expression test';
  26. $runs = 100;
  27.  
  28. // Parameter Sets
  29. $parameterSets = array(
  30.     'set1' => array (
  31.         'description' => 'Array with one element',
  32.         'arraySize' => 1
  33.     ),
  34.     'set2' => array (
  35.         'description' => 'Array with 500 elements',
  36.         'arraySize' => 500
  37.     ),
  38.     'set3' => array (
  39.         'description' => 'Array with 10.000 elements',
  40.         'arraySize' => 10000
  41.     ),
  42.     'set4' => array (
  43.         'description' => 'Array with 100.000 elements',
  44.         'arraySize' => 100000
  45.     )
  46. );
  47.  
  48. /*****************************************************************************
  49.  * Tests:
  50.  ****************************************************************************/
  51.  
  52. /**
  53.  * Baseline
  54.  *
  55.  * @param $path
  56.  * @param string $extensionList
  57.  * @param bool $prependPath
  58.  * @param string $order
  59.  * @param string $excludePattern
  60.  *
  61.  * @return array|string
  62.  */
  63. $descriptions['version1'] = 'Ternary';
  64. function version1($arraySize) {
  65.     $context = array('test' => array());
  66.     // optionally fill-in the test value with lots of data
  67.     for ($i = 0; $i < $arraySize; $i++) {
  68.         $context['test'][] = $i;
  69.     }
  70.  
  71.     return isset($context['test']) ? $context['test'] : FALSE;
  72. }
  73.  
  74. $descriptions['version2'] = 'Compressed Ternary';
  75. function version2($arraySize) {
  76.     $context = array('test' => array());
  77.     // optionally fill-in the test value with lots of data
  78.     for ($i = 0; $i < $arraySize; $i++) {
  79.         $context['test'][] = $i;
  80.     }
  81.    
  82.     return $context['test'] ?: FALSE;
  83. }
  84.  
  85. $descriptions['version3'] = 'Regular If';
  86. function version3($arraySize) {
  87.     $context = array('test' => array());
  88.     // optionally fill-in the test value with lots of data
  89.     for ($i = 0; $i < $arraySize; $i++) {
  90.         $context['test'][] = $i;
  91.     }
  92.    
  93.     if (isset($context['test'])) {
  94.         return $context['test'];
  95.     }
  96.     return FALSE;
  97. }
  98.  
  99. /*****************************************************************************
  100.  * Helper functions needed for test:
  101.  ****************************************************************************/
  102.  
  103. function inList($list, $item) {
  104.     return strpos(',' . $list . ',', ',' . $item . ',') !== FALSE;
  105. }
  106.  
  107. /*****************************************************************************
  108.  * System:
  109.  ****************************************************************************/
  110.  
  111. function getmicrotime() {
  112.     $t = gettimeofday();
  113.     return $t['sec'] * 1000 + $t['usec'] / 1000;
  114. }
  115.  
  116. // How many runs?
  117. if (isset($_GET['runs']) && $_GET['runs']) {
  118.     $runs = (preg_replace('/[^0-9]/', '', $_GET['runs'])) ?
  119.         (preg_replace('/[^0-9]/', '', $_GET['runs'])) :
  120.         $runs;
  121. }
  122.  
  123. $baselineTimes = array();
  124. $functions = get_defined_functions();
  125.  
  126. foreach ($parameterSets as $setName => $parameters) {
  127.     $baselineRun = TRUE;
  128.     foreach ($functions['user'] as $function) {
  129.         if (strpos($function, 'version', 0) === 0) {
  130.             $result = array();
  131.             $start = getmicrotime();
  132.             for ($i = 0; $i < $runs; $i++) {
  133.                 $result = $function($parameters['arraySize']);
  134.             }
  135.             $end = getmicrotime();
  136.             $time = $end - $start;
  137.             if ($baselineRun) {
  138.                 $baselineTimes[$setName] = $time;
  139.                 $baselineRun = FALSE;
  140.             }
  141.             $xAxis[] = $function;
  142.             $resultObjects[$setName][$function] = array_slice($result, 0, 20, TRUE);
  143.             $times[$setName][$function] = sprintf('%1.3f', $time);
  144.             $i++;
  145.         }
  146.     }
  147. }
  148.  
  149. function findWinner($times) {
  150.     foreach ($times as $set) {
  151.         foreach ($set as $functionName => $time) {
  152.             if (isset($cumulativeTime[$functionName])) {
  153.                 $cumulativeTime[$functionName] += $time;
  154.             } else {
  155.                 $cumulativeTime[$functionName] = $time;
  156.             }
  157.         }
  158.     }
  159.     asort($cumulativeTime);
  160.     return array_slice($cumulativeTime, 0, 1);
  161. }
  162.  
  163. $winningFunction = (findWinner($times));
  164.  
  165. ?>
  166. <html>
  167. <head>
  168.     <title>TYPO3 Test Suite</title>
  169.     <link rel="stylesheet" href="http://wiki.typo3.org/wiki/load.php?debug=false&amp;lang=en&amp;modules=mediawiki.legacy.commonPrint%2Cshared%7Cskins.typo3vector&amp;only=styles&amp;skin=typo3vector&amp;*" />
  170.     <script src="http://code.jquery.com/jquery-2.0.3.min.js" type="text/javascript"></script>
  171.     <script src="http://code.highcharts.com/highcharts.js" type="text/javascript"></script>
  172.     <script src="http://code.highcharts.com/modules/exporting.js" type="text/javascript"></script>
  173. </head>
  174. <body>
  175. <div id="content" class="mw-body" role="main">
  176.     <h1 id="top"><?php echo $testName ?></h1>
  177.     <p>The winning function is <strong><?php echo key($winningFunction) ?></strong>.</p>
  178.     <p>Run the tests how many times?</p>
  179.     <form action="<?php echo basename(__FILE__) ?>">
  180.         <input name="runs" id="runs" value="<?php echo $runs ?>"/>
  181.         <input class="submit" type="submit" value="Go!"/>
  182.     </form>
  183.     <div id="resultGraph" style="min-width: 310px; min-height: 400px; margin: 0 auto"></div>
  184.     <?php
  185.         foreach ($times as $setName => $functionData) {
  186.             echo '<h2>' . ucfirst($setName) . '</h2>';
  187.             echo '<p>' . $parameterSets[$setName]['description'] . '</p>';
  188.             echo '<ul>';
  189.             foreach ($parameterSets[$setName] as $key => $value) {
  190.                 if ($key !== 'description') {
  191.                     echo '<li>' . $key . ' = "' . (string) $value . '"</li>';
  192.                 }
  193.             }
  194.             echo '</ul>';
  195.             echo '<ul>';
  196.             foreach ($functionData as $function => $time) {
  197.                 $identifier = $setName . '-' . $function;
  198.                 echo '<li><a style="text-decoration: none" href="#' . $identifier . '">' .
  199.                     ucfirst($function) .
  200.                     '</a> ' .
  201.                     ': ' .
  202.                     sprintf('<span style="width: 33px; display: inline-block; text-align: right; font-weight: bold;">%1.2d%%</span> ', $time * 100 / $baselineTimes[$setName]) .
  203.                     $descriptions[$function] .
  204.                     '</li>';
  205.             }
  206.             echo '</ul>';
  207.         }
  208.     ?>
  209.     <script>
  210.         var baseLineTimes = [<?php echo implode(',', $baselineTimes) ?>];
  211.         var descriptions = ['<?php echo implode("','", array_map('addslashes', $descriptions)) ?>'];
  212.         Highcharts.setOptions({
  213.             colors: ['#f58006', '#f50806', '#067bf5', '#f5bc06', '#8006f5']
  214.         });
  215.         jQuery(document).ready(function($) {
  216.             $('#resultGraph').highcharts({
  217.                 chart: {
  218. //                  type: 'bar'
  219.                     zoomType: 'y'
  220.                 },
  221.                 title: {
  222.                     text: '<?php echo $testName ?>'
  223.                 },
  224.                 xAxis: {
  225.                     categories: ['<?php echo implode("','", $xAxis)  ?>'],
  226.                     title: {
  227.                         text: null
  228.                     }
  229.                 },
  230.                 yAxis: {
  231.                     min: 0,
  232.                     title: {
  233.                         text: 'Time (milliseconds)',
  234.                         align: 'high'
  235.                     },
  236.                     labels: {
  237.                         overflow: 'justify',
  238.                         formatter: function() {
  239.                             return Highcharts.numberFormat(this.value, 0, ',', '.');
  240.                         }
  241.                     }
  242.                 },
  243.                 tooltip: {
  244.                     useHTML: true,
  245.                     formatter: function() {
  246.                         return '<strong><a style="text-decoration: none" href="#' + this.point.series.name + '-' + this.point.category + '">' + descriptions[this.point.x] + '</a></strong><br/>' +
  247.                             Highcharts.numberFormat(this.point.y, 0, ',', '.') + ' milliseconds' +
  248.                             ' (' + Math.ceil(this.point.y * 100 / baseLineTimes[this.point.series.index]) + '%)';
  249.                     }
  250.                 },
  251.                 legend: {
  252.                     enabled: true
  253.                 },
  254.                 credits: {
  255.                     enabled: false
  256.                 },
  257.                 series: [
  258.                     <?php
  259.                     foreach ($times as $setName => $setTimes) {
  260.                         $series[] = "{
  261.                             name: '" . $setName . "',
  262.                             data: [" . implode(',', $setTimes) . "]
  263.                         }";
  264.                     }
  265.                     echo implode(',', $series);
  266.                     ?>
  267.                 ]
  268.             });
  269.         });
  270.     </script>
  271.     <h2>Data results</h2>
  272.     <?php
  273.         foreach ($times as $setName => $functionData) {
  274.             echo '<h3>' . ucfirst($setName) . '</h3>';
  275.             echo '<p>' . $parameterSets[$setName]['description'] . '</p>';
  276.             echo '<ul>';
  277.             foreach ($parameterSets[$setName] as $key => $value) {
  278.                 if ($key !== 'description') {
  279.                     echo '<li>' . $key . ' = "' . (string) $value . '"</li>';
  280.                 }
  281.             }
  282.             echo '</ul>';
  283.             echo '<p>' . $parameterSets[$setName]['description'] . '</p>';
  284.             foreach ($functionData as $function => $time) {
  285.                 $identifier = $setName . '-' . $function;
  286.                 echo '<h4 id="' . $identifier . '">' . ucfirst($function) . '</h4>';
  287.                 echo '<p>' . $descriptions[$function] . '</p>';
  288.                 var_dump($resultObjects[$setName][$function]);
  289.                 echo '<a href="#top" style="color: #ddd; text-decoration: none;">^ top</a>';
  290.             }
  291.         }
  292.     ?>
  293.     <div id="p-personal" role="navigation" class="">
  294.         <ul>
  295.     <?php
  296.     foreach ($resultObjects as $setName => $functionData) {
  297.         foreach ($functionData as $function => $data) {
  298.         echo '<li><a href="#' . $setName . '-' . $function . '">' . ucfirst($setName) . ' ' . ucfirst($function)  . '</a></h3>';
  299.         }
  300.     }
  301.     ?>
  302.         </ul>
  303.     </div>
  304. </div>
  305. </body>
  306. </html>
Advertisement
Add Comment
Please, Sign In to add comment