From: Tim Starling Date: Thu, 23 Mar 2017 01:51:43 +0000 (+1100) Subject: Add benchmarkTidy.php, to benchmark tidy drivers X-Git-Tag: 1.31.0-rc.0~3439^2 X-Git-Url: http://git.cyclocoop.org/%22%20.%20generer_url_ecrire%28%22auteur_infos%22%2C%22id_auteur=%24connect_id_auteur%22%29%20.%20%22?a=commitdiff_plain;h=448be2ed3e3d705696f1bade68c55a4fbc1b56bf;p=lhc%2Fweb%2Fwiklou.git Add benchmarkTidy.php, to benchmark tidy drivers Plus representative input file Change-Id: I254793fc55c57a98c07ae1e4c27e6005965c9a20 --- diff --git a/autoload.php b/autoload.php index ba5b77478e..27c27c5692 100644 --- a/autoload.php +++ b/autoload.php @@ -192,6 +192,7 @@ $wgAutoloadLocalClasses = [ 'BenchmarkHooks' => __DIR__ . '/maintenance/benchmarks/benchmarkHooks.php', 'BenchmarkParse' => __DIR__ . '/maintenance/benchmarks/benchmarkParse.php', 'BenchmarkPurge' => __DIR__ . '/maintenance/benchmarks/benchmarkPurge.php', + 'BenchmarkTidy' => __DIR__ . '/maintenance/benchmarks/benchmarkTidy.php', 'Benchmarker' => __DIR__ . '/maintenance/benchmarks/Benchmarker.php', 'BitmapHandler' => __DIR__ . '/includes/media/Bitmap.php', 'BitmapHandler_ClientOnly' => __DIR__ . '/includes/media/Bitmap_ClientOnly.php', diff --git a/includes/parser/MWTidy.php b/includes/parser/MWTidy.php index 01bf2d0d65..ffc884eb2d 100644 --- a/includes/parser/MWTidy.php +++ b/includes/parser/MWTidy.php @@ -83,7 +83,7 @@ class MWTidy { /** * @return bool|\MediaWiki\Tidy\TidyDriverBase */ - protected static function singleton() { + public static function singleton() { global $wgUseTidy, $wgTidyInternal, $wgTidyConf, $wgDebugTidy, $wgTidyConfig, $wgTidyBin, $wgTidyOpts; diff --git a/maintenance/benchmarks/README b/maintenance/benchmarks/README index c021abd293..27da9defd1 100644 --- a/maintenance/benchmarks/README +++ b/maintenance/benchmarks/README @@ -5,3 +5,6 @@ To get somehow accurate result, you might want to bound the PHP process to a specific CPU with `taskset` and raise its priority with `nice`. Example: $ taskset 1 nice -n-10 php bench_wfIsWindows.php + +australia-untidy.html.gz contains representative input text for +benchmarkTidy.php. It needs to be decompressed before use. diff --git a/maintenance/benchmarks/australia-untidy.html.gz b/maintenance/benchmarks/australia-untidy.html.gz new file mode 100644 index 0000000000..148481da6f Binary files /dev/null and b/maintenance/benchmarks/australia-untidy.html.gz differ diff --git a/maintenance/benchmarks/benchmarkTidy.php b/maintenance/benchmarks/benchmarkTidy.php new file mode 100644 index 0000000000..14791745fe --- /dev/null +++ b/maintenance/benchmarks/benchmarkTidy.php @@ -0,0 +1,78 @@ +addOption( 'file', 'A filename which contains the input text', true, true ); + $this->addOption( 'driver', 'The Tidy driver name, or false to use the configured instance', + false, true ); + $this->addOption( 'tidy-config', 'JSON encoded value for the tidy configuration array', + false, true ); + } + + public function execute() { + $html = file_get_contents( $this->getOption( 'file' ) ); + if ( $html === false ) { + $this->error( "Unable to open input file", 1 ); + } + if ( $this->hasOption( 'driver' ) || $this->hasOption( 'tidy-config' ) ) { + $config = json_decode( $this->getOption( 'tidy-config', '{}' ), true ); + if ( !is_array( $config ) ) { + $this->error( "Invalid JSON tidy config", 1 ); + } + $config += [ 'driver' => $this->getOption( 'driver', 'RemexHtml' ) ]; + $driver = MWTidy::factory( $config ); + } else { + $driver = MWTidy::singleton(); + if ( !$driver ) { + $this->error( "Tidy disabled or not installed", 1 ); + } + } + + $this->benchmark( $driver, $html ); + } + + private function benchmark( $driver, $html ) { + global $wgContLang; + + $times = []; + $innerCount = 10; + $outerCount = 10; + for ( $j = 1; $j <= $outerCount; $j++ ) { + $t = microtime( true ); + for ( $i = 0; $i < $innerCount; $i++ ) { + $driver->tidy( $html ); + print $wgContLang->formatSize( memory_get_usage( true ) ) . "\n"; + } + $t = ( ( microtime( true ) - $t ) / $innerCount ) * 1000; + $times[] = $t; + print "Run $j: $t\n"; + } + print "\n"; + + sort( $times, SORT_NUMERIC ); + $n = $outerCount; + $min = $times[0]; + $max = end( $times ); + if ( $n % 2 ) { + $median = $times[ ( $n - 1 ) / 2 ]; + } else { + $median = ( $times[$n / 2] + $times[$n / 2 - 1] ) / 2; + } + $mean = array_sum( $times ) / $n; + + print "Minimum: $min ms\n"; + print "Median: $median ms\n"; + print "Mean: $mean ms\n"; + print "Maximum: $max ms\n"; + print "Memory usage: " . + $wgContLang->formatSize( memory_get_usage( true ) ) . "\n"; + print "Peak memory usage: " . + $wgContLang->formatSize( memory_get_peak_usage( true ) ) . "\n"; + } +} + +$maintClass = 'BenchmarkTidy'; +require RUN_MAINTENANCE_IF_MAIN;