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 = 'isFirstPartOfStr()';
- $runs = 5000;
- // Parameter Sets
- $parameterSets = array(
- 'set1' => array (
- 'description' => 'regular string, not first part',
- 'needle' => 'www',
- 'haystack' => '/var/www/htdocs/'
- ),
- 'set2' => array (
- 'description' => 'regular string, first part',
- 'needle' => 'www',
- 'haystack' => 'www/htdocs/something'
- ),
- 'set3' => array (
- 'description' => 'empty needle (note that this will of cousre fail v2 and v3)',
- 'needle' => '',
- 'haystack' => 'www/htdocs/something'
- )
- );
- /*****************************************************************************
- * Tests:
- ****************************************************************************/
- $descriptions['version1'] = 'Baseline GeneralUtility::isFirstPartOfStr';
- function version1($needle, $haystack) {
- return isFirstPartOfStr($haystack, $needle);
- }
- $descriptions['version2'] = 'Using substr(), known needle length';
- function version2($needle, $haystack) {
- return substr($haystack, 0, 3) === $needle;
- }
- $descriptions['version3'] = 'Using substr(), known needle length, early return';
- function version3($needle, $haystack) {
- return substr($haystack, 0, 3) === $needle;
- }
- $descriptions['version4'] = 'Using substr(), unknown needle length';
- function version4($needle, $haystack) {
- return substr($haystack, 0, strlen($needle)) === $needle;
- }
- $descriptions['version5'] = 'Using substr(), unknown needle length, early return';
- function version5($needle, $haystack) {
- return substr($haystack, 0, strlen($needle)) === $needle;
- }
- $descriptions['version6'] = 'Using strpos(), known needle length, early return';
- function version6($needle, $haystack) {
- return $needle !== '' && strpos($haystack, $needle) === 0;
- }
- /*****************************************************************************
- * Helper functions needed for test:
- ****************************************************************************/
- /**
- * Returns TRUE if the first part of $str matches the string $partStr
- *
- * @param string $str Full string to check
- * @param string $partStr Reference string which must be found as the "first part" of the full string
- * @return boolean TRUE if $partStr was found to be equal to the first part of $str
- */
- function isFirstPartOfStr($str, $partStr) {
- return $partStr != '' && strpos((string) $str, (string) $partStr, 0) === 0;
- }
- /*****************************************************************************
- * 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['needle'], $parameters['haystack']);
- }
- $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>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 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>
- 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 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/>' +
- setDescriptions[this.series.index] + '<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>';
- 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
Advertisement