Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*****************************************************************************
- * Copyright notice
- *
- * ⓒ 2013 Michiel Roos <[email protected]>
- * All rights reserved
- *
- * This script is part of the TYPO3 project. The TYPO3 project 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 2 of the License, or (at your option) any later version.
- *
- * The GNU General Public License can be found at
- * http://www.gnu.org/copyleft/gpl.html.
- *
- * This script 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.
- *
- * This copyright notice MUST APPEAR in all copies of the script!
- ****************************************************************************/
- // Setup
- $testName = 'Ternary expression test';
- $runs = 100;
- // Parameter Sets
- $parameterSets = array(
- 'set1' => array (
- 'description' => 'Array with one element',
- 'arraySize' => 1
- ),
- 'set2' => array (
- 'description' => 'Array with 500 elements',
- 'arraySize' => 500
- ),
- 'set3' => array (
- 'description' => 'Array with 10.000 elements',
- 'arraySize' => 10000
- ),
- 'set4' => array (
- 'description' => 'Array with 100.000 elements',
- 'arraySize' => 100000
- )
- );
- /*****************************************************************************
- * Tests:
- ****************************************************************************/
- /**
- * Baseline
- *
- * @param $path
- * @param string $extensionList
- * @param bool $prependPath
- * @param string $order
- * @param string $excludePattern
- *
- * @return array|string
- */
- $descriptions['version1'] = 'Ternary';
- function version1($arraySize) {
- $context = array('test' => array());
- // optionally fill-in the test value with lots of data
- for ($i = 0; $i < $arraySize; $i++) {
- $context['test'][] = $i;
- }
- return isset($context['test']) ? $context['test'] : FALSE;
- }
- $descriptions['version2'] = 'Compressed Ternary';
- function version2($arraySize) {
- $context = array('test' => array());
- // optionally fill-in the test value with lots of data
- for ($i = 0; $i < $arraySize; $i++) {
- $context['test'][] = $i;
- }
- return $context['test'] ?: FALSE;
- }
- $descriptions['version3'] = 'Regular If';
- function version3($arraySize) {
- $context = array('test' => array());
- // optionally fill-in the test value with lots of data
- for ($i = 0; $i < $arraySize; $i++) {
- $context['test'][] = $i;
- }
- if (isset($context['test'])) {
- return $context['test'];
- }
- return FALSE;
- }
- /*****************************************************************************
- * Helper functions needed for test:
- ****************************************************************************/
- function inList($list, $item) {
- return strpos(',' . $list . ',', ',' . $item . ',') !== FALSE;
- }
- /*****************************************************************************
- * System:
- ****************************************************************************/
- function getmicrotime() {
- $t = gettimeofday();
- return $t['sec'] * 1000 + $t['usec'] / 1000;
- }
- // How many runs?
- if (isset($_GET['runs']) && $_GET['runs']) {
- $runs = (preg_replace('/[^0-9]/', '', $_GET['runs'])) ?
- (preg_replace('/[^0-9]/', '', $_GET['runs'])) :
- $runs;
- }
- $baselineTimes = array();
- $functions = get_defined_functions();
- foreach ($parameterSets as $setName => $parameters) {
- $baselineRun = TRUE;
- foreach ($functions['user'] as $function) {
- if (strpos($function, 'version', 0) === 0) {
- $result = array();
- $start = getmicrotime();
- for ($i = 0; $i < $runs; $i++) {
- $result = $function($parameters['arraySize']);
- }
- $end = getmicrotime();
- $time = $end - $start;
- if ($baselineRun) {
- $baselineTimes[$setName] = $time;
- $baselineRun = FALSE;
- }
- $xAxis[] = $function;
- $resultObjects[$setName][$function] = array_slice($result, 0, 20, TRUE);
- $times[$setName][$function] = sprintf('%1.3f', $time);
- $i++;
- }
- }
- }
- function findWinner($times) {
- foreach ($times as $set) {
- foreach ($set as $functionName => $time) {
- if (isset($cumulativeTime[$functionName])) {
- $cumulativeTime[$functionName] += $time;
- } else {
- $cumulativeTime[$functionName] = $time;
- }
- }
- }
- asort($cumulativeTime);
- return array_slice($cumulativeTime, 0, 1);
- }
- $winningFunction = (findWinner($times));
- ?>
- <html>
- <head>
- <title>TYPO3 Test Suite</title>
- <link rel="stylesheet" href="http://wiki.typo3.org/wiki/load.php?debug=false&lang=en&modules=mediawiki.legacy.commonPrint%2Cshared%7Cskins.typo3vector&only=styles&skin=typo3vector&*" />
- <script src="http://code.jquery.com/jquery-2.0.3.min.js" type="text/javascript"></script>
- <script src="http://code.highcharts.com/highcharts.js" type="text/javascript"></script>
- <script src="http://code.highcharts.com/modules/exporting.js" type="text/javascript"></script>
- </head>
- <body>
- <div id="content" class="mw-body" role="main">
- <h1 id="top"><?php echo $testName ?></h1>
- <p>The winning function is <strong><?php echo key($winningFunction) ?></strong>.</p>
- <p>Run the tests how many times?</p>
- <form action="<?php echo basename(__FILE__) ?>">
- <input name="runs" id="runs" value="<?php echo $runs ?>"/>
- <input class="submit" type="submit" value="Go!"/>
- </form>
- <div id="resultGraph" style="min-width: 310px; min-height: 400px; margin: 0 auto"></div>
- <?php
- foreach ($times as $setName => $functionData) {
- echo '<h2>' . ucfirst($setName) . '</h2>';
- echo '<p>' . $parameterSets[$setName]['description'] . '</p>';
- echo '<ul>';
- foreach ($parameterSets[$setName] as $key => $value) {
- if ($key !== 'description') {
- echo '<li>' . $key . ' = "' . (string) $value . '"</li>';
- }
- }
- echo '</ul>';
- echo '<ul>';
- foreach ($functionData as $function => $time) {
- $identifier = $setName . '-' . $function;
- echo '<li><a style="text-decoration: none" href="#' . $identifier . '">' .
- ucfirst($function) .
- '</a> ' .
- ': ' .
- sprintf('<span style="width: 33px; display: inline-block; text-align: right; font-weight: bold;">%1.2d%%</span> ', $time * 100 / $baselineTimes[$setName]) .
- $descriptions[$function] .
- '</li>';
- }
- echo '</ul>';
- }
- ?>
- <script>
- var baseLineTimes = [<?php echo implode(',', $baselineTimes) ?>];
- var descriptions = ['<?php echo implode("','", array_map('addslashes', $descriptions)) ?>'];
- Highcharts.setOptions({
- colors: ['#f58006', '#f50806', '#067bf5', '#f5bc06', '#8006f5']
- });
- jQuery(document).ready(function($) {
- $('#resultGraph').highcharts({
- chart: {
- // type: 'bar'
- zoomType: 'y'
- },
- title: {
- text: '<?php echo $testName ?>'
- },
- xAxis: {
- categories: ['<?php echo implode("','", $xAxis) ?>'],
- title: {
- text: null
- }
- },
- yAxis: {
- min: 0,
- title: {
- text: 'Time (milliseconds)',
- align: 'high'
- },
- labels: {
- overflow: 'justify',
- formatter: function() {
- return Highcharts.numberFormat(this.value, 0, ',', '.');
- }
- }
- },
- tooltip: {
- useHTML: true,
- formatter: function() {
- return '<strong><a style="text-decoration: none" href="#' + this.point.series.name + '-' + this.point.category + '">' + descriptions[this.point.x] + '</a></strong><br/>' +
- Highcharts.numberFormat(this.point.y, 0, ',', '.') + ' milliseconds' +
- ' (' + Math.ceil(this.point.y * 100 / baseLineTimes[this.point.series.index]) + '%)';
- }
- },
- legend: {
- enabled: true
- },
- credits: {
- enabled: false
- },
- series: [
- <?php
- foreach ($times as $setName => $setTimes) {
- $series[] = "{
- name: '" . $setName . "',
- data: [" . implode(',', $setTimes) . "]
- }";
- }
- echo implode(',', $series);
- ?>
- ]
- });
- });
- </script>
- <h2>Data results</h2>
- <?php
- foreach ($times as $setName => $functionData) {
- echo '<h3>' . ucfirst($setName) . '</h3>';
- echo '<p>' . $parameterSets[$setName]['description'] . '</p>';
- echo '<ul>';
- foreach ($parameterSets[$setName] as $key => $value) {
- if ($key !== 'description') {
- echo '<li>' . $key . ' = "' . (string) $value . '"</li>';
- }
- }
- echo '</ul>';
- echo '<p>' . $parameterSets[$setName]['description'] . '</p>';
- foreach ($functionData as $function => $time) {
- $identifier = $setName . '-' . $function;
- echo '<h4 id="' . $identifier . '">' . ucfirst($function) . '</h4>';
- echo '<p>' . $descriptions[$function] . '</p>';
- var_dump($resultObjects[$setName][$function]);
- echo '<a href="#top" style="color: #ddd; text-decoration: none;">^ top</a>';
- }
- }
- ?>
- <div id="p-personal" role="navigation" class="">
- <ul>
- <?php
- foreach ($resultObjects as $setName => $functionData) {
- foreach ($functionData as $function => $data) {
- echo '<li><a href="#' . $setName . '-' . $function . '">' . ucfirst($setName) . ' ' . ucfirst($function) . '</a></h3>';
- }
- }
- ?>
- </ul>
- </div>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment