]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - maintenance/benchmarks/benchmarkTidy.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / maintenance / benchmarks / benchmarkTidy.php
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 ) {
38                 global $wgContLang;
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';
78 require RUN_MAINTENANCE_IF_MAIN;