MediaWiki  1.29.2
benchmarkTidy.php
Go to the documentation of this file.
1 <?php
2 
3 require __DIR__ . '/../Maintenance.php';
4 
5 class BenchmarkTidy extends Maintenance {
6  public function __construct() {
7  parent::__construct();
8  $this->addOption( 'file', 'A filename which contains the input text', true, true );
9  $this->addOption( 'driver', 'The Tidy driver name, or false to use the configured instance',
10  false, true );
11  $this->addOption( 'tidy-config', 'JSON encoded value for the tidy configuration array',
12  false, true );
13  }
14 
15  public function execute() {
16  $html = file_get_contents( $this->getOption( 'file' ) );
17  if ( $html === false ) {
18  $this->error( "Unable to open input file", 1 );
19  }
20  if ( $this->hasOption( 'driver' ) || $this->hasOption( 'tidy-config' ) ) {
21  $config = json_decode( $this->getOption( 'tidy-config', '{}' ), true );
22  if ( !is_array( $config ) ) {
23  $this->error( "Invalid JSON tidy config", 1 );
24  }
25  $config += [ 'driver' => $this->getOption( 'driver', 'RemexHtml' ) ];
26  $driver = MWTidy::factory( $config );
27  } else {
28  $driver = MWTidy::singleton();
29  if ( !$driver ) {
30  $this->error( "Tidy disabled or not installed", 1 );
31  }
32  }
33 
34  $this->benchmark( $driver, $html );
35  }
36 
37  private function benchmark( $driver, $html ) {
39 
40  $times = [];
41  $innerCount = 10;
42  $outerCount = 10;
43  for ( $j = 1; $j <= $outerCount; $j++ ) {
44  $t = microtime( true );
45  for ( $i = 0; $i < $innerCount; $i++ ) {
46  $driver->tidy( $html );
47  print $wgContLang->formatSize( memory_get_usage( true ) ) . "\n";
48  }
49  $t = ( ( microtime( true ) - $t ) / $innerCount ) * 1000;
50  $times[] = $t;
51  print "Run $j: $t\n";
52  }
53  print "\n";
54 
55  sort( $times, SORT_NUMERIC );
56  $n = $outerCount;
57  $min = $times[0];
58  $max = end( $times );
59  if ( $n % 2 ) {
60  $median = $times[ ( $n - 1 ) / 2 ];
61  } else {
62  $median = ( $times[$n / 2] + $times[$n / 2 - 1] ) / 2;
63  }
64  $mean = array_sum( $times ) / $n;
65 
66  print "Minimum: $min ms\n";
67  print "Median: $median ms\n";
68  print "Mean: $mean ms\n";
69  print "Maximum: $max ms\n";
70  print "Memory usage: " .
71  $wgContLang->formatSize( memory_get_usage( true ) ) . "\n";
72  print "Peak memory usage: " .
73  $wgContLang->formatSize( memory_get_peak_usage( true ) ) . "\n";
74  }
75 }
76 
77 $maintClass = 'BenchmarkTidy';
BenchmarkTidy\execute
execute()
Do the actual work.
Definition: benchmarkTidy.php:15
BenchmarkTidy
Definition: benchmarkTidy.php:5
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
MWTidy\factory
static factory(array $config)
Create a new Tidy driver object from configuration.
Definition: MWTidy.php:124
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
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:1956
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:215
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
BenchmarkTidy\__construct
__construct()
Default constructor.
Definition: benchmarkTidy.php:6
BenchmarkTidy\benchmark
benchmark( $driver, $html)
Definition: benchmarkTidy.php:37
Maintenance\$config
Config $config
Accessible via getConfig()
Definition: Maintenance.php:130
$maintClass
$maintClass
Definition: benchmarkTidy.php:77
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:250
MWTidy\singleton
static singleton()
Definition: MWTidy.php:86
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:392
$t
$t
Definition: testCompression.php:67
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:236
$wgContLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the content language as $wgContLang
Definition: design.txt:56