MediaWiki  master
benchmarkTidy.php
Go to the documentation of this file.
1 <?php
23 
24 require __DIR__ . '/../includes/Benchmarker.php';
25 
26 class BenchmarkTidy extends Benchmarker {
27  public function __construct() {
28  parent::__construct();
29  $this->addOption( 'file', 'Path to file containing the input text', false, true );
30  $this->addOption( 'driver', 'The Tidy driver name, or false to use the configured instance',
31  false, true );
32  $this->addOption( 'tidy-config', 'JSON encoded value for the tidy configuration array',
33  false, true );
34  }
35 
36  public function execute() {
37  $file = $this->getOption( 'file', __DIR__ . '/tidy/australia-untidy.html.gz' );
38  $html = $this->loadFile( $file );
39  if ( $html === false ) {
40  $this->fatalError( "Unable to open input file" );
41  }
42  if ( $this->hasOption( 'driver' ) || $this->hasOption( 'tidy-config' ) ) {
43  $config = json_decode( $this->getOption( 'tidy-config', '{}' ), true );
44  if ( !is_array( $config ) ) {
45  $this->fatalError( "Invalid JSON tidy config" );
46  }
47  $config += [ 'driver' => $this->getOption( 'driver', 'RemexHtml' ) ];
48  $driver = MWTidy::factory( $config );
49  } else {
50  $driver = MWTidy::singleton();
51  if ( !$driver ) {
52  $this->fatalError( "Tidy disabled or not installed" );
53  }
54  }
55 
56  $this->benchmark( $driver, $html );
57  }
58 
59  private function benchmark( $driver, $html ) {
60  $contLang = MediaWikiServices::getInstance()->getContentLanguage();
61  $times = [];
62  $innerCount = 10;
63  $outerCount = 10;
64  for ( $j = 1; $j <= $outerCount; $j++ ) {
65  $t = microtime( true );
66  for ( $i = 0; $i < $innerCount; $i++ ) {
67  $driver->tidy( $html );
68  print $contLang->formatSize( memory_get_usage( true ) ) . "\n";
69  }
70  $t = ( ( microtime( true ) - $t ) / $innerCount ) * 1000;
71  $times[] = $t;
72  print "Run $j: $t\n";
73  }
74  print "\n";
75 
76  sort( $times, SORT_NUMERIC );
77  $n = $outerCount;
78  $min = $times[0];
79  $max = end( $times );
80  if ( $n % 2 ) {
81  // @phan-suppress-next-line PhanTypeMismatchDimFetch
82  $median = $times[ ( $n - 1 ) / 2 ];
83  } else {
84  $median = ( $times[$n / 2] + $times[$n / 2 - 1] ) / 2;
85  }
86  $mean = array_sum( $times ) / $n;
87 
88  print "Minimum: $min ms\n";
89  print "Median: $median ms\n";
90  print "Mean: $mean ms\n";
91  print "Maximum: $max ms\n";
92  print "Memory usage: " . $contLang->formatSize( memory_get_usage( true ) ) . "\n";
93  print "Peak memory usage: " .
94  $contLang->formatSize( memory_get_peak_usage( true ) ) . "\n";
95  }
96 }
97 
98 $maintClass = BenchmarkTidy::class;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
BenchmarkTidy\execute
execute()
Do the actual work.
Definition: benchmarkTidy.php:36
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:152
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:487
BenchmarkTidy
Definition: benchmarkTidy.php:26
$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:75
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:245
BenchmarkTidy\__construct
__construct()
Default constructor.
Definition: benchmarkTidy.php:27
Benchmarker
Base class for benchmark scripts.
Definition: Benchmarker.php:41
BenchmarkTidy\benchmark
benchmark( $driver, $html)
Definition: benchmarkTidy.php:59
Maintenance\$config
Config $config
Accessible via getConfig()
Definition: Maintenance.php:149
$maintClass
$maintClass
Definition: benchmarkTidy.php:98
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:281
MWTidy\singleton
static singleton()
Definition: MWTidy.php:59
Benchmarker\loadFile
loadFile( $file)
Definition: Benchmarker.php:183
$t
$t
Definition: testCompression.php:74
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:266