]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - vendor/wikimedia/utfnormal/scripts/benchmark.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / vendor / wikimedia / utfnormal / scripts / benchmark.php
1 <?php
2 /**
3  * Approximate benchmark for some basic operations.
4  *
5  * Copyright © 2004 Brion Vibber <brion@pobox.com>
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 = array(
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
52 function benchmarkTest( &$u, $filename, $desc ) {
53         print "Testing $filename ($desc)...\n";
54         $data = file_get_contents( $filename );
55         $forms = array(
56 #               'placebo',
57                 'cleanUp',
58                 'toNFC',
59 #               'toNFKC',
60 #               'toNFD', 'toNFKD',
61                 'NFC',
62 #               'NFKC',
63 #               'NFD', 'NFKD',
64                 array( 'fastDecompose', 'fastCombiningSort', 'fastCompose' ),
65 #               'quickIsNFC', 'quickIsNFCVerify',
66         );
67
68         foreach ( $forms as $form ) {
69                 if ( is_array( $form ) ) {
70                         $str = $data;
71                         foreach ( $form as $step ) {
72                                 $str = benchmarkForm( $u, $str, $step );
73                         }
74                 } else {
75                         benchmarkForm( $u, $data, $form );
76                 }
77         }
78 }
79
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 );
86         }
87         # $delta = (microtime( true ) - $start) / BENCH_CYCLES;
88         sort( $deltas );
89         $delta = $deltas[0]; # Take shortest time
90
91         $rate = intval( strlen( $data ) / $delta );
92         $same = ( 0 == strcmp( $data, $out ) );
93
94         printf( " %20s %6.1fms %12s bytes/s (%s)\n",
95                 $form,
96                 $delta * 1000.0,
97                 number_format( $rate ),
98                 ( $same ? 'no change' : 'changed' ) );
99
100         return $out;
101 }