MediaWiki  1.33.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  $median = $times[ ( $n - 1 ) / 2 ];
63  } else {
64  $median = ( $times[$n / 2] + $times[$n / 2 - 1] ) / 2;
65  }
66  $mean = array_sum( $times ) / $n;
67 
68  print "Minimum: $min ms\n";
69  print "Median: $median ms\n";
70  print "Mean: $mean ms\n";
71  print "Maximum: $max ms\n";
72  print "Memory usage: " . $contLang->formatSize( memory_get_usage( true ) ) . "\n";
73  print "Peak memory usage: " .
74  $contLang->formatSize( memory_get_peak_usage( true ) ) . "\n";
75  }
76 }
77 
BenchmarkTidy\execute
execute()
Do the actual work.
Definition: benchmarkTidy.php:17
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Definition: router.php:42
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:485
BenchmarkTidy
Definition: benchmarkTidy.php:7
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$html
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition: hooks.txt:1985
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:248
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
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:148
$maintClass
$maintClass
Definition: benchmarkTidy.php:78
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:283
MWTidy\singleton
static singleton()
Definition: MWTidy.php:61
Benchmarker\loadFile
loadFile( $file)
Definition: Benchmarker.php:182
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
$t
$t
Definition: testCompression.php:69
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:269