MediaWiki  master
benchmarkTidy.php
Go to the documentation of this file.
1 <?php
23 
24 require_once __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  }
31 
32  public function execute() {
33  $file = $this->getOption( 'file', __DIR__ . '/data/tidy/australia-untidy.html.gz' );
34  $html = $this->loadFile( $file );
35  if ( $html === false ) {
36  $this->fatalError( "Unable to open input file" );
37  }
38 
39  $this->benchmark( $html );
40  }
41 
42  private function benchmark( $html ) {
43  $services = MediaWikiServices::getInstance();
44  $contLang = $services->getContentLanguage();
45  $tidy = $services->getTidy();
46  $times = [];
47  $innerCount = 10;
48  $outerCount = 10;
49  for ( $j = 1; $j <= $outerCount; $j++ ) {
50  $t = microtime( true );
51  for ( $i = 0; $i < $innerCount; $i++ ) {
52  $tidy->tidy( $html );
53  print $contLang->formatSize( memory_get_usage( true ) ) . "\n";
54  }
55  $t = ( ( microtime( true ) - $t ) / $innerCount ) * 1000;
56  $times[] = $t;
57  print "Run $j: $t\n";
58  }
59  print "\n";
60 
61  sort( $times, SORT_NUMERIC );
62  $n = $outerCount;
63  $min = $times[0];
64  $max = end( $times );
65  if ( $n % 2 ) {
66  // @phan-suppress-next-line PhanTypeMismatchDimFetch
67  $median = $times[ ( $n - 1 ) / 2 ];
68  } else {
69  $median = ( $times[$n / 2] + $times[$n / 2 - 1] ) / 2;
70  }
71  $mean = array_sum( $times ) / $n;
72 
73  print "Minimum: $min ms\n";
74  print "Median: $median ms\n";
75  print "Mean: $mean ms\n";
76  print "Maximum: $max ms\n";
77  print "Memory usage: " . $contLang->formatSize( memory_get_usage( true ) ) . "\n";
78  print "Peak memory usage: " .
79  $contLang->formatSize( memory_get_peak_usage( true ) ) . "\n";
80  }
81 }
82 
83 $maintClass = BenchmarkTidy::class;
84 require_once RUN_MAINTENANCE_IF_MAIN;
$maintClass
__construct()
Default constructor.
execute()
Do the actual work.
Base class for benchmark scripts.
Definition: Benchmarker.php:40
loadFile( $file)
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Service locator for MediaWiki core services.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42