Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(E_ALL & ~E_NOTICE);
- /*****************************************************************************
- * Copyright notice
- *
- * ⓒ 2013 Michiel Roos <michiel@maxserv.nl>
- * 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 = '\TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode';
- $reverseExecutionOrder = 0;
- $runs = 100;
- // Parameter Sets
- $parameterSets = array(
- 'set1' => array (
- 'description' => 'CSV, no removeEmpty, no limit',
- 'delim' => ',',
- 'string' => 'a, b, c, ,d, e, f',
- 'removeEmptyValues' => FALSE,
- 'limit' => 0,
- ),
- 'set2' => array (
- 'description' => 'CSV, removeEmpty, no limit',
- 'delim' => ',',
- 'string' => 'a, b, c, ,d, e, f',
- 'removeEmptyValues' => TRUE,
- 'limit' => 0,
- ),
- 'set3' => array (
- 'description' => 'CSV, no removeEmpty, limit 3',
- 'delim' => ',',
- 'string' => 'a, b, c, ,d, e, f',
- 'removeEmptyValues' => FALSE,
- 'limit' => 3,
- ),
- 'set4' => array (
- 'description' => 'CSV, removeEmpty, limit -3',
- 'delim' => ',',
- 'string' => 'a, b, c, ,d, e, f',
- 'removeEmptyValues' => TRUE,
- 'limit' => -3,
- ),
- 'set5' => array (
- 'description' => 'LF, removeEmpty, limit -3',
- 'delim' => ',',
- 'string' => ' a , b , ' . PHP_EOL . ' ,d ,, e,f,',
- 'removeEmptyValues' => TRUE,
- 'limit' => -3,
- ),
- 'set6' => array (
- 'description' => 'Keep 0, removeEmpty, no limit',
- 'delim' => ',',
- 'string' => 'a , b , c , ,d ,, ,e,f, 0 ,',
- 'removeEmptyValues' => TRUE,
- 'limit' => 0,
- ),
- );
- /*****************************************************************************
- * Tests:
- ****************************************************************************/
- /**
- * Baseline
- *
- * @return array|string
- */
- $descriptions['version1'] = 'Baseline trimExplode';
- /**
- * Explodes a string and trims all values for whitespace in the ends.
- * If $onlyNonEmptyValues is set, then all blank ('') values are removed.
- *
- * @param string $delim Delimiter string to explode with
- * @param string $string The string to explode
- * @param boolean $removeEmptyValues If set, all empty values will be removed in output
- * @param integer $limit If positive, the result will contain a maximum of
- * @return array Exploded values
- */
- function version1($delim, $string, $removeEmptyValues = FALSE, $limit = 0) {
- $explodedValues = explode($delim, $string);
- $result = array_map('trim', $explodedValues);
- if ($removeEmptyValues) {
- $temp = array();
- foreach ($result as $value) {
- if ($value !== '') {
- $temp[] = $value;
- }
- }
- $result = $temp;
- }
- if ($limit != 0) {
- if ($limit < 0) {
- $result = array_slice($result, 0, $limit);
- } elseif (count($result) > $limit) {
- $lastElements = array_slice($result, $limit - 1);
- $result = array_slice($result, 0, $limit - 1);
- $result[] = implode($delim, $lastElements);
- }
- }
- return $result;
- }
- $descriptions['version2'] = 'Optimized version';
- function version2($delim, $string, $removeEmptyValues = FALSE, $limit = 0) {
- if ($delim !== ' ') {
- $string = str_replace(
- array(
- ' ',
- "\t",
- "\n",
- "\r",
- "\0",
- "\x0B"
- ),
- '',
- $string
- );
- $result = explode($delim, $string);
- } else {
- $explodedValues = explode($delim, $string);
- $result = array_map('trim', $explodedValues);
- }
- if ($removeEmptyValues) {
- $result = array_filter($result);
- }
- if ($limit != 0) {
- if ($limit < 0) {
- $result = array_slice($result, 0, $limit);
- } elseif (count($result) > $limit) {
- $lastElements = array_slice($result, $limit - 1);
- $result = array_slice($result, 0, $limit - 1);
- $result[] = implode($delim, $lastElements);
- }
- }
- return $result;
- }
- /*****************************************************************************
- * Helper functions needed for test:
- ****************************************************************************/
- /*****************************************************************************
- * System (look, but don't touch ;-) . . . only touch if you must.
- ****************************************************************************/
- // Show source?
- if (isset($_GET['source']) && $_GET['source']) {
- show_source(__FILE__);
- exit;
- }
- // How many runs?
- if (isset($_GET['runs'])) $runs = preg_replace('/[^0-9]/', '', $_GET['runs']);
- // Execution order?
- if (isset($_GET['reverseExecutionOrder'])) $reverseExecutionOrder = intval($_GET['reverseExecutionOrder']);
- // Prepare
- $baselineTimes = $functionsToCall = $times = array();
- $allFunctions = get_defined_functions();
- $functions = array_filter($allFunctions['user'], create_function('$a','return strpos($a, "version") === 0;'));
- if ($reverseExecutionOrder) arsort($functions);
- foreach ($functions as $function) {
- $xAxis[] = $function;
- $functionsToCall[$function] = new ReflectionFunction($function);
- }
- // Execute
- foreach ($parameterSets as $setName => $parameters) {
- // Description is used later on, so clone the parameters
- $functionParameters = $parameters;
- unset($functionParameters['description']);
- for ($i = 0; $i < $runs; $i++) {
- foreach ($functions as $function) {
- $start = microtime(TRUE);
- $result = $functionsToCall[$function]->invokeArgs($functionParameters);
- $time = microtime(TRUE) - $start;
- if ($function === 'version1') {
- $baselineTimes[$setName] += $time * 1000;
- }
- if (is_array($result)) {
- $resultObjects[$setName][$function] = array_slice($result, 0, 20, TRUE);
- } else {
- $resultObjects[$setName][$function] = $result;
- }
- $times[$setName][$function] += $time * 1000;
- }
- }
- }
- function findWinner($times) {
- $averagedTimes = array();
- foreach ($times as $setName => $timeData) {
- foreach ($timeData as $functionName => $time) {
- $averagedTimes[$functionName] += $time;
- }
- }
- asort($averagedTimes);
- return $averagedTimes;
- }
- $averagedTimes = findWinner($times);
- /**
- * Format an integer as a time value
- *
- * @param integer $time The value to format
- *
- * @return string
- */
- function printSeconds($time) {
- $prefix = '';
- $suffix = 'μs';
- if ($time < 0) {
- $time = abs($time);
- $prefix = '-';
- }
- if ($time === 0) {
- $suffix = '';
- }
- if ($time >= 1000) {
- $time = $time / 1000;
- $suffix = 'ms';
- }
- if ($time >= 1000) {
- $time = $time / 1000;
- $suffix = ' s';
- }
- if ($time >= 60 && $suffix === ' s') {
- $time = $time / 60;
- $suffix = 'min!';
- }
- return $prefix . sprintf("%.2f {$suffix}", $time);
- }
- ?>
- <html>
- <head>
- <title><?php echo $testName ?> | TYPO3 Tiny 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://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.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">
- <h1 id="top"><?php echo $testName ?></h1>
- <form action="<?php echo $_SERVER['SCRIPT_NAME'] ?>">
- <label for="runs">Run the tests how many times?</label>
- <input name="runs" id="runs" value="<?php echo $runs ?>"/>
- <label for="runs"><a href="#help">Reverse execution order?</a></label>
- <input type="checkbox" name="reverseExecutionOrder" id="reverseExecutionOrder" value="1" <?php echo ($reverseExecutionOrder) ? 'checked="checked"' : '' ?>/>
- <input class="submit" type="submit" value="Go!"/>
- </form>
- <p>Winner using averaged times over all sets: <strong><?php
- $winner = array_slice($averagedTimes, 0, 1);
- echo key($winner) ?></strong> <?php echo printSeconds(current($winner) / count($times)) ?>.</p>
- <?php
- if (count($parameterSets) > 1) {
- echo '<ul>';
- foreach ($averagedTimes as $function => $time) {
- echo '<li><b>' . $function . '</b>: ',
- ' ' . printSeconds($time / count($times)) . '</li>';
- }
- echo '</ul>';
- }
- ?>
- <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>',
- '<p>' , $parameterSets[$setName]['description'] , '</p>',
- '<ul>';
- foreach ($functionData as $function => $time) {
- $identifier = $setName . '-' . $function;
- echo '<li><a style="text-decoration: none" href="#', $identifier, '">',
- ucfirst($function),
- '</a> ',
- ': ',
- sprintf('<span style="min-width: 33px; display: inline-block; text-align: right; font-weight: bold;">%1.2d%%</span> ', $time * 100 / $baselineTimes[$setName]),
- sprintf('<span style="min-width: 50px; display: inline-block; text-align: right; margin: 0 10px;">%s</span> ', printSeconds($time)),
- $descriptions[$function],
- '</li>';
- }
- echo '</ul><h3>Parameters</h3><ul>';
- foreach ($parameterSets[$setName] as $key => $value) {
- if ($key !== 'description') {
- if (is_array($value)) {
- echo '<li>' , $key , ':', '</li>'; var_dump($value);
- } else {
- echo '<li>' . $key . ' = "' . (string) $value . '"</li>';
- }
- } else {
- $setDescriptions[] = $value;
- }
- }
- echo '</ul>';
- }
- ?>
- <script>
- /**
- * Format an integer as a time value
- *
- * @param {String} time The value to format in microseconds.
- * @param {Number} decimals The amount of decimals
- *
- * @return string
- */
- function printSeconds(time, decimals) {
- decimals = typeof decimals !== 'undefined' ? decimals : 2;
- var prefix = '',
- suffix = 'μs';
- if (time < 0) {
- time = Math.abs(time);
- prefix = '-';
- }
- if (time == 0) {
- suffix = '';
- }
- if (time >= 1000) {
- time = time / 1000;
- suffix = 'ms';
- }
- if (time >= 1000) {
- time = time / 1000;
- suffix = ' s';
- }
- if (time >= 60 && suffix == ' s') {
- time = time / 60;
- suffix = 'min!';
- }
- return prefix + Highcharts.numberFormat(time, decimals) + ' ' + suffix;
- }
- var baseLineTimes = [<?php echo implode(',', $baselineTimes) ?>];
- var descriptions = ['<?php echo implode("','", array_map('addslashes', $descriptions)) ?>'];
- var setDescriptions = ['<?php echo implode("','", array_map('addslashes', $setDescriptions)) ?>'];
- 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 printSeconds(this.value);
- }
- }
- },
- 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/>' +
- setDescriptions[this.series.index] + '<br/>' +
- printSeconds(this.point.y) +
- ' (' + 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);
- ?>
- ]
- });
- $('#showSourceLink').on('click', function(e) {
- e.preventDefault();
- $.ajax({
- url: '<?php echo $_SERVER['SCRIPT_NAME'] ?>?source=1',
- cache: false
- })
- .done(function(html) {
- $('.loading').hide();
- $('#sourceCode').html(html);
- $('html, body').animate({
- scrollTop: $("#source").offset().top
- }, {
- duration: 2000,
- easing: 'easeOutBounce'
- });
- });
- });
- $('.top').on('click', function(e) {
- e.preventDefault();
- $('html, body').animate({
- scrollTop: $("#top").offset().top
- }, {
- duration: 2000,
- easing: 'easeOutBounce'
- });
- });
- });
- </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') {
- if (is_array($value)) {
- echo '<li>' , $key , ':', '</li>'; var_dump($value);
- } else {
- echo '<li>' . $key . ' = "' . (string) $value . '"</li>';
- }
- }
- }
- echo '</ul>';
- foreach ($functionData as $function => $time) {
- echo '<h4 id="', $setName . '-' . $function, '">', ucfirst($function), '</h4>',
- '<p>', $descriptions[$function], '</p>';
- var_dump($resultObjects[$setName][$function]);
- echo '<a href="#top" class="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>';
- }
- }
- ?>
- <li><a href="#help">Help</a></li>
- <li><a href="#source">Source Code</a></li>
- </ul>
- </div>
- <div id="help">
- <h2>Help</h2>
- <h3>Execution Order</h3>
- <p>In some cases, the second function (non baseline) always runs faster than the baseline. Even when switching the code around. This toggle enables you to reverse the running order to check for this behaviour. Your winning function should still win whatever the execution order. If that is not the case, then this test has failed to determine what code runs faster.</p>
- <a href="#top" class="top" style="color: #ddd; text-decoration: none;">^ top</a>
- </div>
- <div id="source">
- <h2>Source Code</h2>
- <pre id="sourceCode"><a href="#source" id="showSourceLink">Show the sourcecode of this file.</a></pre>
- <a href="#top" class="top" style="color: #ddd; text-decoration: none;">^ top</a>
- </div>
- <div id="logo" style="position: absolute; top: 60px; left: 60px;"><img alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHYAAAAiCAYAAACKuC3wAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJ
- bWFnZVJlYWR5ccllPAAABzJJREFUeNrsWwlsFUUYHh4VkEMEKaACHmAV0KTENFxaiIqK1sQDNYIS
- UTQRDSlH1DSIBqSgAQVijGIEjdYDREXEIEo4LSiKgFgUMKCAIK1KFQoUSv1++y/v7zCzZ1/bkP2T
- Lztv59jd+Wb+a/c1qKysVLGcftLAb8PKfNUQh8sYRxrkqUWGNpNweBhoI06XAiXALuBnoBBYgv77
- 4umvA2JBEtVlATcA13K5KVf3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9m0Cb0Gbs
- rKrnpqU8Tk5Xwu9dgThKllPKcDUHxOTC0+u6K2qZVPqP7UbGwRD9i0T/Uh/tK0T7qL7DMvaKSRVv
- VcmUIanqNWz2+rDjZLPVs7jtfpVMh/6pqjJP/Z1o5j8BBgADhL1q2hRfzwAAAABJRU5ErkJggg==" /></div>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement