Debian Codegraph @

vendor/wikimedia/utfnormal/scripts/memstress.php

raw
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 <[email protected]>
8 * https://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 use UtfNormal\Validator;
30
31 if ( PHP_SAPI != 'cli' ) {
32 die( "Run me from the command line please.\n" );
33 }
34
35 require_once dirname( __DIR__ ) . '/vendor/autoload.php';
36
37 define( 'BENCH_CYCLES', 1 );
38 // 10 M
39 define( 'BIGSIZE', 1024 * 1024 * 10 );
40 ini_set( 'memory_limit', BIGSIZE + 120 * 1024 * 1024 );
41
42 $testfiles = [
43 'testdata/washington.txt' => 'English text',
44 'testdata/berlin.txt' => 'German text',
45 'testdata/bulgakov.txt' => 'Russian text',
46 'testdata/tokyo.txt' => 'Japanese text',
47 'testdata/young.txt' => 'Korean text'
48 ];
49 $normalizer = new Validator;
50 Validator::loadData();
51 foreach ( $testfiles as $file => $desc ) {
52 benchmarkTest( $normalizer, $file, $desc );
53 }
54
55 /**
56 * @param Validator &$u
57 * @param string $filename
58 * @param string $desc
59 */
60 function benchmarkTest( &$u, $filename, $desc ) {
61 print "Testing $filename ($desc)...\n";
62 $data = file_get_contents( $filename );
63 $all = $data;
64 while ( strlen( $all ) < BIGSIZE ) {
65 $all .= $all;
66 }
67 $data = $all;
68 echo "Data is " . strlen( $data ) . " bytes.\n";
69 $forms = [
70 'quickIsNFCVerify',
71 'cleanUp',
72 ];
73
74 foreach ( $forms as $form ) {
75 if ( is_array( $form ) ) {
76 $str = $data;
77 foreach ( $form as $step ) {
78 $str = benchmarkForm( $u, $str, $step );
79 }
80 } else {
81 benchmarkForm( $u, $data, $form );
82 }
83 }
84 }
85
86 /**
87 * @param Validator &$u
88 * @param string &$data
89 * @param string $form
90 * @return string
91 */
92 function benchmarkForm( &$u, &$data, $form ) {
93 # $start = microtime( true );
94 for ( $i = 0; $i < BENCH_CYCLES; $i++ ) {
95 $start = microtime( true );
96 $out = $u->$form( $data, Validator::$utfCanonicalDecomp );
97 $deltas[] = ( microtime( true ) - $start );
98 }
99 # $delta = (microtime( true ) - $start) / BENCH_CYCLES;
100 sort( $deltas );
101 # Take shortest time
102 $delta = $deltas[0];
103
104 $rate = intval( strlen( $data ) / $delta );
105 $same = ( strcmp( $data, $out ) == 0 );
106
107 printf( " %20s %6.1fms %12s bytes/s (%s)\n",
108 $form,
109 $delta * 1000.0,
110 number_format( $rate ),
111 ( $same ? 'no change' : 'changed' ) );
112
113 return $out;
114 }