3 require __DIR__
. '/../Maintenance.php';
5 class BenchmarkTidy
extends Maintenance
{
6 public function __construct() {
8 $this->addOption( 'file', 'A filename which contains the input text', true, true );
9 $this->addOption( 'driver', 'The Tidy driver name, or false to use the configured instance',
11 $this->addOption( 'tidy-config', 'JSON encoded value for the tidy configuration array',
15 public function execute() {
16 $html = file_get_contents( $this->getOption( 'file' ) );
17 if ( $html === false ) {
18 $this->fatalError( "Unable to open input file" );
20 if ( $this->hasOption( 'driver' ) ||
$this->hasOption( 'tidy-config' ) ) {
21 $config = json_decode( $this->getOption( 'tidy-config', '{}' ), true );
22 if ( !is_array( $config ) ) {
23 $this->fatalError( "Invalid JSON tidy config" );
25 $config +
= [ 'driver' => $this->getOption( 'driver', 'RemexHtml' ) ];
26 $driver = MWTidy
::factory( $config );
28 $driver = MWTidy
::singleton();
30 $this->fatalError( "Tidy disabled or not installed" );
34 $this->benchmark( $driver, $html );
37 private function benchmark( $driver, $html ) {
43 for ( $j = 1; $j <= $outerCount; $j++
) {
44 $t = microtime( true );
45 for ( $i = 0; $i < $innerCount; $i++
) {
46 $driver->tidy( $html );
47 print $wgContLang->formatSize( memory_get_usage( true ) ) . "\n";
49 $t = ( ( microtime( true ) - $t ) / $innerCount ) * 1000;
55 sort( $times, SORT_NUMERIC
);
60 $median = $times[ ( $n - 1 ) / 2 ];
62 $median = ( $times[$n / 2] +
$times[$n / 2 - 1] ) / 2;
64 $mean = array_sum( $times ) / $n;
66 print "Minimum: $min ms\n";
67 print "Median: $median ms\n";
68 print "Mean: $mean ms\n";
69 print "Maximum: $max ms\n";
70 print "Memory usage: " .
71 $wgContLang->formatSize( memory_get_usage( true ) ) . "\n";
72 print "Peak memory usage: " .
73 $wgContLang->formatSize( memory_get_peak_usage( true ) ) . "\n";
77 $maintClass = 'BenchmarkTidy';
78 require RUN_MAINTENANCE_IF_MAIN
;