MediaWiki  master
benchmarkTidy.php
Go to the documentation of this file.
1 <?php
22 require_once __DIR__ . '/../includes/Benchmarker.php';
23 
24 class BenchmarkTidy extends Benchmarker {
25  public function __construct() {
26  parent::__construct();
27  $this->addOption( 'file', 'Path to file containing the input text', false, true );
28  }
29 
30  public function execute() {
31  $file = $this->getOption( 'file', __DIR__ . '/data/tidy/australia-untidy.html.gz' );
32  $html = $this->loadFile( $file );
33  if ( $html === false ) {
34  $this->fatalError( "Unable to open input file" );
35  }
36 
37  $this->benchmark( $html );
38  }
39 
40  private function benchmark( $html ) {
41  $services = $this->getServiceContainer();
42  $contLang = $services->getContentLanguage();
43  $tidy = $services->getTidy();
44  $times = [];
45  $innerCount = 10;
46  $outerCount = 10;
47  for ( $j = 1; $j <= $outerCount; $j++ ) {
48  $t = microtime( true );
49  for ( $i = 0; $i < $innerCount; $i++ ) {
50  $tidy->tidy( $html );
51  print $contLang->formatSize( memory_get_usage( true ) ) . "\n";
52  }
53  $t = ( ( microtime( true ) - $t ) / $innerCount ) * 1000;
54  $times[] = $t;
55  print "Run $j: $t\n";
56  }
57  print "\n";
58 
59  sort( $times, SORT_NUMERIC );
60  $n = $outerCount;
61  $min = $times[0];
62  $max = end( $times );
63  if ( $n % 2 ) {
64  // @phan-suppress-next-line PhanTypeMismatchDimFetch
65  $median = $times[ ( $n - 1 ) / 2 ];
66  } else {
67  $median = ( $times[$n / 2] + $times[$n / 2 - 1] ) / 2;
68  }
69  $mean = array_sum( $times ) / $n;
70 
71  print "Minimum: $min ms\n";
72  print "Median: $median ms\n";
73  print "Mean: $mean ms\n";
74  print "Maximum: $max ms\n";
75  print "Memory usage: " . $contLang->formatSize( memory_get_usage( true ) ) . "\n";
76  print "Peak memory usage: " .
77  $contLang->formatSize( memory_get_peak_usage( true ) ) . "\n";
78  }
79 }
80 
81 $maintClass = BenchmarkTidy::class;
82 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)
getServiceContainer()
Returns the main service container.
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.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42