Revert r84706, r84709 (UtfNormal rewrite). Clearly more work needed to get these...
[lhc/web/wiklou.git] / includes / normal / UtfNormalBench.php
1 <?php
2 /**
3 * Approximate benchmark for some basic operations.
4 *
5 * Copyright © 2004 Brion Vibber <brion@pobox.com>
6 * http://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 if( isset( $_SERVER['argv'] ) && in_array( '--icu', $_SERVER['argv'] ) ) {
28 dl( 'php_utfnormal.so' );
29 }
30
31 require_once 'UtfNormalUtil.php';
32 require_once 'UtfNormal.php';
33
34 define( 'BENCH_CYCLES', 5 );
35
36 if( php_sapi_name() != 'cli' ) {
37 die( "Run me from the command line please.\n" );
38 }
39
40 $testfiles = array(
41 'testdata/washington.txt' => 'English text',
42 'testdata/berlin.txt' => 'German text',
43 'testdata/bulgakov.txt' => 'Russian text',
44 'testdata/tokyo.txt' => 'Japanese text',
45 'testdata/young.txt' => 'Korean text'
46 );
47 $normalizer = new UtfNormal;
48 UtfNormal::loadData();
49 foreach( $testfiles as $file => $desc ) {
50 benchmarkTest( $normalizer, $file, $desc );
51 }
52
53 # -------
54
55 function benchmarkTest( &$u, $filename, $desc ) {
56 print "Testing $filename ($desc)...\n";
57 $data = file_get_contents( $filename );
58 $forms = array(
59 # 'placebo',
60 'cleanUp',
61 'toNFC',
62 # 'toNFKC',
63 # 'toNFD', 'toNFKD',
64 'NFC',
65 # 'NFKC',
66 # 'NFD', 'NFKD',
67 array( 'fastDecompose', 'fastCombiningSort', 'fastCompose' ),
68 # 'quickIsNFC', 'quickIsNFCVerify',
69 );
70 foreach( $forms as $form ) {
71 if( is_array( $form ) ) {
72 $str = $data;
73 foreach( $form as $step ) {
74 $str = benchmarkForm( $u, $str, $step );
75 }
76 } else {
77 benchmarkForm( $u, $data, $form );
78 }
79 }
80 }
81
82 function benchTime(){
83 $st = explode( ' ', microtime() );
84 return (float)$st[0] + (float)$st[1];
85 }
86
87 function benchmarkForm( &$u, &$data, $form ) {
88 #$start = benchTime();
89 for( $i = 0; $i < BENCH_CYCLES; $i++ ) {
90 $start = benchTime();
91 $out = $u->$form( $data, UtfNormal::$utfCanonicalDecomp );
92 $deltas[] = (benchTime() - $start);
93 }
94 #$delta = (benchTime() - $start) / BENCH_CYCLES;
95 sort( $deltas );
96 $delta = $deltas[0]; # Take shortest time
97
98 $rate = intval( strlen( $data ) / $delta );
99 $same = (0 == strcmp( $data, $out ) );
100
101 printf( " %20s %6.1fms %12s bytes/s (%s)\n",
102 $form,
103 $delta*1000.0,
104 number_format( $rate ),
105 ($same ? 'no change' : 'changed' ) );
106 return $out;
107 }