MediaWiki REL1_35
benchmarkTidy.php
Go to the documentation of this file.
1<?php
23
24require __DIR__ . '/../includes/Benchmarker.php';
25
27 public function __construct() {
28 parent::__construct();
29 $this->addOption( 'file', 'Path to file containing the input text', false, true );
30 $this->addOption( 'driver', 'The Tidy driver name, or false to use the configured instance',
31 false, true );
32 $this->addOption( 'tidy-config', 'JSON encoded value for the tidy configuration array',
33 false, true );
34 }
35
36 public function execute() {
37 $file = $this->getOption( 'file', __DIR__ . '/tidy/australia-untidy.html.gz' );
38 $html = $this->loadFile( $file );
39 if ( $html === false ) {
40 $this->fatalError( "Unable to open input file" );
41 }
42 if ( $this->hasOption( 'driver' ) || $this->hasOption( 'tidy-config' ) ) {
43 $config = json_decode( $this->getOption( 'tidy-config', '{}' ), true );
44 if ( !is_array( $config ) ) {
45 $this->fatalError( "Invalid JSON tidy config" );
46 }
47 $config += [ 'driver' => $this->getOption( 'driver', 'RemexHtml' ) ];
48 $driver = MWTidy::factory( $config );
49 } else {
50 $driver = MWTidy::singleton();
51 if ( !$driver ) {
52 $this->fatalError( "Tidy disabled or not installed" );
53 }
54 }
55
56 $this->benchmark( $driver, $html );
57 }
58
59 private function benchmark( $driver, $html ) {
60 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
61 $times = [];
62 $innerCount = 10;
63 $outerCount = 10;
64 for ( $j = 1; $j <= $outerCount; $j++ ) {
65 $t = microtime( true );
66 for ( $i = 0; $i < $innerCount; $i++ ) {
67 $driver->tidy( $html );
68 print $contLang->formatSize( memory_get_usage( true ) ) . "\n";
69 }
70 $t = ( ( microtime( true ) - $t ) / $innerCount ) * 1000;
71 $times[] = $t;
72 print "Run $j: $t\n";
73 }
74 print "\n";
75
76 sort( $times, SORT_NUMERIC );
77 $n = $outerCount;
78 $min = $times[0];
79 $max = end( $times );
80 if ( $n % 2 ) {
81 // @phan-suppress-next-line PhanTypeMismatchDimFetch
82 $median = $times[ ( $n - 1 ) / 2 ];
83 } else {
84 $median = ( $times[$n / 2] + $times[$n / 2 - 1] ) / 2;
85 }
86 $mean = array_sum( $times ) / $n;
87
88 print "Minimum: $min ms\n";
89 print "Median: $median ms\n";
90 print "Mean: $mean ms\n";
91 print "Maximum: $max ms\n";
92 print "Memory usage: " . $contLang->formatSize( memory_get_usage( true ) ) . "\n";
93 print "Peak memory usage: " .
94 $contLang->formatSize( memory_get_peak_usage( true ) ) . "\n";
95 }
96}
97
98$maintClass = BenchmarkTidy::class;
const RUN_MAINTENANCE_IF_MAIN
$maintClass
benchmark( $driver, $html)
__construct()
Default constructor.
execute()
Do the actual work.
Base class for benchmark scripts.
loadFile( $file)
static singleton()
Definition MWTidy.php:59
static factory(array $config=null)
Create a new Tidy driver object from configuration.
Definition MWTidy.php:75
hasOption( $name)
Checks to see if a particular option was set.
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.
Config $config
Accessible via getConfig()
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
MediaWikiServices is the service locator for the application scope of MediaWiki.
while(( $__line=Maintenance::readconsole()) !==false) print
Definition eval.php:64
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42