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 <[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 = 'substr() vs substr_replace()';
- $runs = 100;
- // Parameter Sets
- $parameterSets = array(
- 'set1' => array (
- 'string' => 'some.KeyValue',
- 'key' => 'some',
- 'description' => 'Regular use',
- )
- );
- /*****************************************************************************
- * Tests:
- ****************************************************************************/
- /**
- * Baseline
- */
- $descriptions['version1'] = 'substr()';
- function version1($string, $key) {
- return substr($string, strlen($key));
- }
- $descriptions['version2'] = 'substr_replace()';
- function version2($string, $key) {
- return substr_replace($string, '', 0, strlen($key));
- }
- /*****************************************************************************
- * Helper functions needed for test:
- ****************************************************************************/
- /*****************************************************************************
- * 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;
- // Description is used later on, so clone the parameters
- $functionParameters = $parameters;
- unset($functionParameters['description']);
- foreach ($functions['user'] as $function) {
- if (strpos($function, 'version', 0) === 0) {
- $result = array();
- $start = getmicrotime();
- for ($i = 0; $i < $runs; $i++) {
- $functionToCall = new ReflectionFunction($function);
- $result = $functionToCall->invokeArgs($functionParameters);
- }
- $end = getmicrotime();
- $time = $end - $start;
- if ($baselineRun) {
- $baselineTimes[$setName] = $time;
- $baselineRun = FALSE;
- }
- $xAxis[] = $function;
- if (is_array($result)) {
- $resultObjects[$setName][$function] = array_slice($result, 0, 20, TRUE);
- } else {
- $resultObjects[$setName][$function] = $result;
- }
- $times[$setName][$function] = sprintf('%1.3f', $time);
- $i++;
- }
- }
- }
- function findWinner($times) {
- $cumulativeTimes = array();
- foreach ($times as $setName => $timeData) {
- foreach ($timeData as $functionName => $time) {
- if (isset($cumulativeTimes[$functionName])) {
- $cumulativeTimes['overall'][$functionName] += $time;
- $cumulativeTimes[$setName][$functionName] += $time;
- } else {
- $cumulativeTimes['overall'][$functionName] = $time;
- $cumulativeTimes[$setName][$functionName] = $time;
- }
- }
- }
- $cumulativeTimes = array_filter($cumulativeTimes, 'asort');
- return $cumulativeTimes;
- }
- $cumulativeTimes = (findWinner($times));
- ?>
- <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://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 overall winning function is <strong><?php echo key(array_slice($cumulativeTimes['overall'], 0, 1)) ?></strong>.</p>
- <?php
- if (count($parameterSets) > 1) {
- echo '<ul>';
- foreach ($cumulativeTimes as $set => $functions) {
- if ($set === 'overall') { continue; }
- echo '<li><b>' . $set . '</b>: ' . key(array_slice($functions, 0, 1)) . '</li>';
- }
- echo '</ul>';
- }
- ?>
- <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') {
- if (is_array($value)) {
- echo '<li>' . $key . ':'. '</li>';
- echo var_dump($value);
- } else {
- echo '<li>' . $key . ' = "' . (string) $value . '"</li>';
- }
- } else {
- $setDescriptions[] = $value;
- }
- }
- 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>
- /**
- * 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);
- ?>
- ]
- });
- });
- </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>';
- 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