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;
BenchmarkTidy\benchmark
benchmark( $html)
Definition: benchmarkTidy.php:42
BenchmarkTidy\execute
execute()
Do the actual work.
Definition: benchmarkTidy.php:32
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:200
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:489
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
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:249
BenchmarkTidy\__construct
__construct()
Default constructor.
Definition: benchmarkTidy.php:27
Benchmarker
Base class for benchmark scripts.
Definition: Benchmarker.php:40
$maintClass
$maintClass
Definition: benchmarkTidy.php:83
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:286
Benchmarker\loadFile
loadFile( $file)
Definition: Benchmarker.php:202
$t
$t
Definition: testCompression.php:74