3 use MediaWiki\MediaWikiServices
;
5 require __DIR__
. '/Benchmarker.php';
7 class BenchmarkTidy
extends Benchmarker
{
8 public function __construct() {
10 $this->addOption( 'file', 'Path to file containing the input text', false, true );
11 $this->addOption( 'driver', 'The Tidy driver name, or false to use the configured instance',
13 $this->addOption( 'tidy-config', 'JSON encoded value for the tidy configuration array',
17 public function execute() {
18 $file = $this->getOption( 'file', __DIR__
. '/tidy/australia-untidy.html.gz' );
19 $html = $this->loadFile( $file );
20 if ( $html === false ) {
21 $this->fatalError( "Unable to open input file" );
23 if ( $this->hasOption( 'driver' ) ||
$this->hasOption( 'tidy-config' ) ) {
24 $config = json_decode( $this->getOption( 'tidy-config', '{}' ), true );
25 if ( !is_array( $config ) ) {
26 $this->fatalError( "Invalid JSON tidy config" );
28 $config +
= [ 'driver' => $this->getOption( 'driver', 'RemexHtml' ) ];
29 $driver = MWTidy
::factory( $config );
31 $driver = MWTidy
::singleton();
33 $this->fatalError( "Tidy disabled or not installed" );
37 $this->benchmark( $driver, $html );
40 private function benchmark( $driver, $html ) {
41 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
45 for ( $j = 1; $j <= $outerCount; $j++
) {
46 $t = microtime( true );
47 for ( $i = 0; $i < $innerCount; $i++
) {
48 $driver->tidy( $html );
49 print $contLang->formatSize( memory_get_usage( true ) ) . "\n";
51 $t = ( ( microtime( true ) - $t ) / $innerCount ) * 1000;
57 sort( $times, SORT_NUMERIC
);
62 $median = $times[ ( $n - 1 ) / 2 ];
64 $median = ( $times[$n / 2] +
$times[$n / 2 - 1] ) / 2;
66 $mean = array_sum( $times ) / $n;
68 print "Minimum: $min ms\n";
69 print "Median: $median ms\n";
70 print "Mean: $mean ms\n";
71 print "Maximum: $max ms\n";
72 print "Memory usage: " . $contLang->formatSize( memory_get_usage( true ) ) . "\n";
73 print "Peak memory usage: " .
74 $contLang->formatSize( memory_get_peak_usage( true ) ) . "\n";
78 $maintClass = BenchmarkTidy
::class;
79 require RUN_MAINTENANCE_IF_MAIN
;