3 * Approximate benchmark for some basic operations.
5 * Copyright © 2004 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
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.
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.
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
27 use UtfNormal\Validator;
29 if ( PHP_SAPI != 'cli' ) {
30 die( "Run me from the command line please.\n" );
33 require_once dirname( __DIR__ ) . '/vendor/autoload.php';
35 define( 'BENCH_CYCLES', 5 );
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'
44 $normalizer = new Validator;
45 Validator::loadData();
46 foreach ( $testfiles as $file => $desc ) {
47 benchmarkTest( $normalizer, $file, $desc );
52 function benchmarkTest( &$u, $filename, $desc ) {
53 print "Testing $filename ($desc)...\n";
54 $data = file_get_contents( $filename );
64 array( 'fastDecompose', 'fastCombiningSort', 'fastCompose' ),
65 # 'quickIsNFC', 'quickIsNFCVerify',
68 foreach ( $forms as $form ) {
69 if ( is_array( $form ) ) {
71 foreach ( $form as $step ) {
72 $str = benchmarkForm( $u, $str, $step );
75 benchmarkForm( $u, $data, $form );
80 function benchmarkForm( &$u, &$data, $form ) {
81 # $start = microtime( true );
82 for ( $i = 0; $i < BENCH_CYCLES; $i++ ) {
83 $start = microtime( true );
84 $out = $u->$form( $data, Validator::$utfCanonicalDecomp );
85 $deltas[] = ( microtime( true ) - $start );
87 # $delta = (microtime( true ) - $start) / BENCH_CYCLES;
89 $delta = $deltas[0]; # Take shortest time
91 $rate = intval( strlen( $data ) / $delta );
92 $same = ( 0 == strcmp( $data, $out ) );
94 printf( " %20s %6.1fms %12s bytes/s (%s)\n",
97 number_format( $rate ),
98 ( $same ? 'no change' : 'changed' ) );