Memory stress test for UtfNormal issue re bug 28146
[lhc/web/wiklou.git] / includes / normal / UtfNormalMemStress.php
1 <?php
2 /**
3 * Approximate benchmark for some basic operations.
4 * Runs large chunks of text through cleanup with a lowish memory limit,
5 * to test regression on mem usage (bug 28146)
6 *
7 * Copyright © 2004-2011 Brion Vibber <brion@wikimedia.org>
8 * http://www.mediawiki.org/
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 *
25 * @file
26 * @ingroup UtfNormal
27 */
28
29 if( isset( $_SERVER['argv'] ) && in_array( '--icu', $_SERVER['argv'] ) ) {
30 dl( 'php_utfnormal.so' );
31 }
32
33 require_once 'UtfNormalUtil.php';
34 require_once 'UtfNormal.php';
35
36 define( 'BENCH_CYCLES', 1 );
37 define( 'BIGSIZE', 1024 * 1024 * 10); // 10m
38 ini_set('memory_limit', BIGSIZE + 120 * 1024 * 1024);
39
40 if( php_sapi_name() != 'cli' ) {
41 die( "Run me from the command line please.\n" );
42 }
43
44 $testfiles = array(
45 'testdata/washington.txt' => 'English text',
46 'testdata/berlin.txt' => 'German text',
47 'testdata/bulgakov.txt' => 'Russian text',
48 'testdata/tokyo.txt' => 'Japanese text',
49 'testdata/young.txt' => 'Korean text'
50 );
51 $normalizer = new UtfNormal;
52 UtfNormal::loadData();
53 foreach( $testfiles as $file => $desc ) {
54 benchmarkTest( $normalizer, $file, $desc );
55 }
56
57 # -------
58
59 function benchmarkTest( &$u, $filename, $desc ) {
60 print "Testing $filename ($desc)...\n";
61 $data = file_get_contents( $filename );
62 $all = $data;
63 while (strlen($all) < BIGSIZE) {
64 $all .= $all;
65 }
66 $data = $all;
67 echo "Data is " . strlen($data) . " bytes.\n";
68 $forms = array(
69 'quickIsNFCVerify',
70 'cleanUp',
71 );
72 foreach( $forms as $form ) {
73 if( is_array( $form ) ) {
74 $str = $data;
75 foreach( $form as $step ) {
76 $str = benchmarkForm( $u, $str, $step );
77 }
78 } else {
79 benchmarkForm( $u, $data, $form );
80 }
81 }
82 }
83
84 function benchTime(){
85 $st = explode( ' ', microtime() );
86 return (float)$st[0] + (float)$st[1];
87 }
88
89 function benchmarkForm( &$u, &$data, $form ) {
90 #$start = benchTime();
91 for( $i = 0; $i < BENCH_CYCLES; $i++ ) {
92 $start = benchTime();
93 $out = $u->$form( $data, UtfNormal::$utfCanonicalDecomp );
94 $deltas[] = (benchTime() - $start);
95 }
96 #$delta = (benchTime() - $start) / BENCH_CYCLES;
97 sort( $deltas );
98 $delta = $deltas[0]; # Take shortest time
99
100 $rate = intval( strlen( $data ) / $delta );
101 $same = (0 == strcmp( $data, $out ) );
102
103 printf( " %20s %6.1fms %12s bytes/s (%s)\n",
104 $form,
105 $delta*1000.0,
106 number_format( $rate ),
107 ($same ? 'no change' : 'changed' ) );
108 return $out;
109 }