MediaWiki  1.34.0
benchmarkTidy.php
Go to the documentation of this file.
1 <?php
2 
4 
5 require __DIR__ . '/Benchmarker.php';
6 
7 class BenchmarkTidy extends Benchmarker {
8  public function __construct() {
9  parent::__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',
12  false, true );
13  $this->addOption( 'tidy-config', 'JSON encoded value for the tidy configuration array',
14  false, true );
15  }
16 
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" );
22  }
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" );
27  }
28  $config += [ 'driver' => $this->getOption( 'driver', 'RemexHtml' ) ];
29  $driver = MWTidy::factory( $config );
30  } else {
31  $driver = MWTidy::singleton();
32  if ( !$driver ) {
33  $this->fatalError( "Tidy disabled or not installed" );
34  }
35  }
36 
37  $this->benchmark( $driver, $html );
38  }
39 
40  private function benchmark( $driver, $html ) {
41  $contLang = MediaWikiServices::getInstance()->getContentLanguage();
42  $times = [];
43  $innerCount = 10;
44  $outerCount = 10;
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";
50  }
51  $t = ( ( microtime( true ) - $t ) / $innerCount ) * 1000;
52  $times[] = $t;
53  print "Run $j: $t\n";
54  }
55  print "\n";
56 
57  sort( $times, SORT_NUMERIC );
58  $n = $outerCount;
59  $min = $times[0];
60  $max = end( $times );
61  if ( $n % 2 ) {
62  // @phan-suppress-next-line PhanTypeMismatchDimFetch
63  $median = $times[ ( $n - 1 ) / 2 ];
64  } else {
65  $median = ( $times[$n / 2] + $times[$n / 2 - 1] ) / 2;
66  }
67  $mean = array_sum( $times ) / $n;
68 
69  print "Minimum: $min ms\n";
70  print "Median: $median ms\n";
71  print "Mean: $mean ms\n";
72  print "Maximum: $max ms\n";
73  print "Memory usage: " . $contLang->formatSize( memory_get_usage( true ) ) . "\n";
74  print "Peak memory usage: " .
75  $contLang->formatSize( memory_get_peak_usage( true ) ) . "\n";
76  }
77 }
78 
79 $maintClass = BenchmarkTidy::class;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
BenchmarkTidy\execute
execute()
Do the actual work.
Definition: benchmarkTidy.php:17
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:504
BenchmarkTidy
Definition: benchmarkTidy.php:7
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
MWTidy\factory
static factory(array $config=null)
Create a new Tidy driver object from configuration.
Definition: MWTidy.php:76
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
$t
$t
Definition: make-normalization-table.php:143
BenchmarkTidy\__construct
__construct()
Default constructor.
Definition: benchmarkTidy.php:8
Benchmarker
Base class for benchmark scripts.
Definition: Benchmarker.php:40
BenchmarkTidy\benchmark
benchmark( $driver, $html)
Definition: benchmarkTidy.php:40
Maintenance\$config
Config $config
Accessible via getConfig()
Definition: Maintenance.php:170
$maintClass
$maintClass
Definition: benchmarkTidy.php:79
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:302
MWTidy\singleton
static singleton()
Definition: MWTidy.php:61
Benchmarker\loadFile
loadFile( $file)
Definition: Benchmarker.php:182
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:288