Merge "resources: Strip '$' and 'mw' from file closures"
[lhc/web/wiklou.git] / maintenance / benchmarks / benchmarkTidy.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
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
78 $maintClass = BenchmarkTidy::class;
79 require RUN_MAINTENANCE_IF_MAIN;