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