Add some sub-functions back to the benchmark
[lhc/web/wiklou.git] / includes / normal / UtfNormalBench.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21 * Approximate benchmark for some basic operations.
22 *
23 * @package UtfNormal
24 * @access private
25 */
26
27 /** */
28 if( isset( $_SERVER['argv'] ) && in_array( '--icu', $_SERVER['argv'] ) ) {
29 dl( 'php_utfnormal.so' );
30 }
31
32 require_once 'UtfNormalUtil.php';
33 require_once 'UtfNormal.php';
34
35 define( 'BENCH_CYCLES', 3 );
36
37 if( php_sapi_name() != 'cli' ) {
38 die( "Run me from the command line please.\n" );
39 }
40
41 $testfiles = array(
42 'testdata/washington.txt' => 'English text',
43 'testdata/berlin.txt' => 'German text',
44 'testdata/tokyo.txt' => 'Japanese text',
45 'testdata/sociology.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( 'placebo',
59 'cleanUp',
60 'toNFC',
61 # 'toNFKC',
62 # 'toNFD', 'toNFKD',
63 'NFC',
64 # 'NFKC',
65 # 'NFD', 'NFKD',
66 'fastDecompose', 'fastCombiningSort', 'fastCompose',
67 'quickIsNFC', 'quickIsNFCVerify',
68 );
69 foreach( $forms as $form ) {
70 benchmarkForm( $u, $data, $form );
71 }
72 }
73
74 function benchTime(){
75 $st = explode( ' ', microtime() );
76 return (float)$st[0] + (float)$st[1];
77 }
78
79 function benchmarkForm( &$u, &$data, $form ) {
80 global $utfCanonicalDecomp;
81 $start = benchTime();
82 for( $i = 0; $i < BENCH_CYCLES; $i++ ) {
83 $out = $u->$form( $data, $utfCanonicalDecomp );
84 }
85 $delta = (benchTime() - $start) / BENCH_CYCLES;
86 $rate = IntVal( strlen( $data ) / $delta );
87 $same = (0 == strcmp( $data, $out ) );
88
89 printf( " %20s %1.4fs %8d bytes/s (%s)\n", $form, $delta, $rate, ($same ? 'no change' : 'changed' ) );
90 }
91
92 ?>