| 1 |
<?php |
| 2 |
/** |
| 3 |
* Approximate benchmark for some basic operations. |
| 4 |
* |
| 5 |
* Copyright © 2004 Brion Vibber <[email protected]> |
| 6 |
* https://www.mediawiki.org/ |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation; either version 2 of the License, or |
| 11 |
* (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License along |
| 19 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 20 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 |
* http://www.gnu.org/copyleft/gpl.html |
| 22 |
* |
| 23 |
* @file |
| 24 |
* @ingroup UtfNormal |
| 25 |
*/ |
| 26 |
|
| 27 |
use UtfNormal\Validator; |
| 28 |
|
| 29 |
if ( PHP_SAPI != 'cli' ) { |
| 30 |
die( "Run me from the command line please.\n" ); |
| 31 |
} |
| 32 |
|
| 33 |
require_once dirname( __DIR__ ) . '/vendor/autoload.php'; |
| 34 |
|
| 35 |
define( 'BENCH_CYCLES', 5 ); |
| 36 |
|
| 37 |
$testfiles = [ |
| 38 |
__DIR__ . '/testdata/washington.txt' => 'English text', |
| 39 |
__DIR__ . '/testdata/berlin.txt' => 'German text', |
| 40 |
__DIR__ . '/testdata/bulgakov.txt' => 'Russian text', |
| 41 |
__DIR__ . '/testdata/tokyo.txt' => 'Japanese text', |
| 42 |
__DIR__ . '/testdata/young.txt' => 'Korean text' |
| 43 |
]; |
| 44 |
$normalizer = new Validator; |
| 45 |
Validator::loadData(); |
| 46 |
foreach ( $testfiles as $file => $desc ) { |
| 47 |
benchmarkTest( $normalizer, $file, $desc ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* @param Validator &$u |
| 52 |
* @param string $filename |
| 53 |
* @param string $desc |
| 54 |
*/ |
| 55 |
function benchmarkTest( &$u, $filename, $desc ) { |
| 56 |
print "Testing $filename ($desc)...\n"; |
| 57 |
$data = file_get_contents( $filename ); |
| 58 |
$forms = [ |
| 59 |
# 'placebo', |
| 60 |
'cleanUp', |
| 61 |
'toNFC', |
| 62 |
# 'toNFKC', |
| 63 |
# 'toNFD', 'toNFKD', |
| 64 |
'NFC', |
| 65 |
# 'NFKC', |
| 66 |
# 'NFD', 'NFKD', |
| 67 |
[ 'fastDecompose', 'fastCombiningSort', 'fastCompose' ], |
| 68 |
# 'quickIsNFC', 'quickIsNFCVerify', |
| 69 |
]; |
| 70 |
|
| 71 |
foreach ( $forms as $form ) { |
| 72 |
if ( is_array( $form ) ) { |
| 73 |
$str = $data; |
| 74 |
foreach ( $form as $step ) { |
| 75 |
$str = benchmarkForm( $u, $str, $step ); |
| 76 |
} |
| 77 |
} else { |
| 78 |
benchmarkForm( $u, $data, $form ); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @param Validator &$u |
| 85 |
* @param string &$data |
| 86 |
* @param string $form |
| 87 |
* @return string |
| 88 |
*/ |
| 89 |
function benchmarkForm( &$u, &$data, $form ) { |
| 90 |
# $start = microtime( true ); |
| 91 |
for ( $i = 0; $i < BENCH_CYCLES; $i++ ) { |
| 92 |
$start = microtime( true ); |
| 93 |
$out = $u->$form( $data, Validator::$utfCanonicalDecomp ); |
| 94 |
$deltas[] = ( microtime( true ) - $start ); |
| 95 |
} |
| 96 |
# $delta = (microtime( true ) - $start) / BENCH_CYCLES; |
| 97 |
sort( $deltas ); |
| 98 |
# Take shortest time |
| 99 |
$delta = $deltas[0]; |
| 100 |
|
| 101 |
$rate = intval( strlen( $data ) / $delta ); |
| 102 |
$same = ( strcmp( $data, $out ) == 0 ); |
| 103 |
|
| 104 |
printf( " %20s %6.1fms %12s bytes/s (%s)\n", |
| 105 |
$form, |
| 106 |
$delta * 1000.0, |
| 107 |
number_format( $rate ), |
| 108 |
( $same ? 'no change' : 'changed' ) ); |
| 109 |
|
| 110 |
return $out; |
| 111 |
} |