MediaWiki  REL1_24
UtfNormalBench.php
Go to the documentation of this file.
00001 <?php
00027 if ( PHP_SAPI != 'cli' ) {
00028     die( "Run me from the command line please.\n" );
00029 }
00030 
00031 if ( isset( $_SERVER['argv'] ) && in_array( '--icu', $_SERVER['argv'] ) ) {
00032     dl( 'php_utfnormal.so' );
00033 }
00034 
00035 require_once 'UtfNormalDefines.php';
00036 require_once 'UtfNormalUtil.php';
00037 require_once 'UtfNormal.php';
00038 
00039 define( 'BENCH_CYCLES', 5 );
00040 
00041 $testfiles = array(
00042     'testdata/washington.txt' => 'English text',
00043     'testdata/berlin.txt' => 'German text',
00044     'testdata/bulgakov.txt' => 'Russian text',
00045     'testdata/tokyo.txt' => 'Japanese text',
00046     'testdata/young.txt' => 'Korean text'
00047 );
00048 $normalizer = new UtfNormal;
00049 UtfNormal::loadData();
00050 foreach ( $testfiles as $file => $desc ) {
00051     benchmarkTest( $normalizer, $file, $desc );
00052 }
00053 
00054 # -------
00055 
00056 function benchmarkTest( &$u, $filename, $desc ) {
00057     print "Testing $filename ($desc)...\n";
00058     $data = file_get_contents( $filename );
00059     $forms = array(
00060 #       'placebo',
00061         'cleanUp',
00062         'toNFC',
00063 #       'toNFKC',
00064 #       'toNFD', 'toNFKD',
00065         'NFC',
00066 #       'NFKC',
00067 #       'NFD', 'NFKD',
00068         array( 'fastDecompose', 'fastCombiningSort', 'fastCompose' ),
00069 #       'quickIsNFC', 'quickIsNFCVerify',
00070     );
00071 
00072     foreach ( $forms as $form ) {
00073         if ( is_array( $form ) ) {
00074             $str = $data;
00075             foreach ( $form as $step ) {
00076                 $str = benchmarkForm( $u, $str, $step );
00077             }
00078         } else {
00079             benchmarkForm( $u, $data, $form );
00080         }
00081     }
00082 }
00083 
00084 function benchmarkForm( &$u, &$data, $form ) {
00085     #$start = microtime( true );
00086     for ( $i = 0; $i < BENCH_CYCLES; $i++ ) {
00087         $start = microtime( true );
00088         $out = $u->$form( $data, UtfNormal::$utfCanonicalDecomp );
00089         $deltas[] = ( microtime( true ) - $start );
00090     }
00091     #$delta = (microtime( true ) - $start) / BENCH_CYCLES;
00092     sort( $deltas );
00093     $delta = $deltas[0]; # Take shortest time
00094 
00095     $rate = intval( strlen( $data ) / $delta );
00096     $same = ( 0 == strcmp( $data, $out ) );
00097 
00098     printf( " %20s %6.1fms %12s bytes/s (%s)\n",
00099         $form,
00100         $delta * 1000.0,
00101         number_format( $rate ),
00102         ( $same ? 'no change' : 'changed' ) );
00103 
00104     return $out;
00105 }