MediaWiki  REL1_19
UtfNormalMemStress.php
Go to the documentation of this file.
00001 <?php
00029 if( isset( $_SERVER['argv'] ) && in_array( '--icu', $_SERVER['argv'] ) ) {
00030         dl( 'php_utfnormal.so' );
00031 }
00032 
00033 require_once 'UtfNormalDefines.php';
00034 require_once 'UtfNormalUtil.php';
00035 require_once 'UtfNormal.php';
00036 
00037 define( 'BENCH_CYCLES', 1 );
00038 define( 'BIGSIZE', 1024 * 1024 * 10); // 10m
00039 ini_set('memory_limit', BIGSIZE + 120 * 1024 * 1024);
00040 
00041 if( php_sapi_name() != 'cli' ) {
00042         die( "Run me from the command line please.\n" );
00043 }
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         foreach( $forms as $form ) {
00074                 if( is_array( $form ) ) {
00075                         $str = $data;
00076                         foreach( $form as $step ) {
00077                                 $str = benchmarkForm( $u, $str, $step );
00078                         }
00079                 } else {
00080                         benchmarkForm( $u, $data, $form );
00081                 }
00082         }
00083 }
00084 
00085 function benchTime(){
00086         $st = explode( ' ', microtime() );
00087         return (float)$st[0] + (float)$st[1];
00088 }
00089 
00090 function benchmarkForm( &$u, &$data, $form ) {
00091         #$start = benchTime();
00092         for( $i = 0; $i < BENCH_CYCLES; $i++ ) {
00093                 $start = benchTime();
00094                 $out = $u->$form( $data, UtfNormal::$utfCanonicalDecomp );
00095                 $deltas[] = (benchTime() - $start);
00096         }
00097         #$delta = (benchTime() - $start) / BENCH_CYCLES;
00098         sort( $deltas );
00099         $delta = $deltas[0]; # Take shortest time
00100 
00101         $rate = intval( strlen( $data ) / $delta );
00102         $same = (0 == strcmp( $data, $out ) );
00103 
00104         printf( " %20s %6.1fms %12s bytes/s (%s)\n",
00105                 $form,
00106                 $delta*1000.0,
00107                 number_format( $rate ),
00108                 ($same ? 'no change' : 'changed' ) );
00109         return $out;
00110 }