MediaWiki master
benchmarkTidy.php
Go to the documentation of this file.
1<?php
9
10// @codeCoverageIgnoreStart
11require_once __DIR__ . '/../includes/Benchmarker.php';
12// @codeCoverageIgnoreEnd
13
15 public function __construct() {
16 parent::__construct();
17 $this->addOption( 'file', 'Path to file containing the input text', false, true );
18 }
19
20 public function execute() {
21 $file = $this->getOption( 'file', __DIR__ . '/data/tidy/australia-untidy.html.gz' );
22 $html = $this->loadFile( $file );
23 if ( $html === false ) {
24 $this->fatalError( "Unable to open input file" );
25 }
26
27 $this->benchmark( $html );
28 }
29
30 private function benchmark( string $html ) {
31 $services = $this->getServiceContainer();
32 $contLang = $services->getContentLanguage();
33 $tidy = $services->getTidy();
34 $times = [];
35 $innerCount = 10;
36 $outerCount = 10;
37 for ( $j = 1; $j <= $outerCount; $j++ ) {
38 $t = microtime( true );
39 for ( $i = 0; $i < $innerCount; $i++ ) {
40 $tidy->tidy( $html );
41 print $contLang->formatSize( memory_get_usage( true ) ) . "\n";
42 }
43 $t = ( ( microtime( true ) - $t ) / $innerCount ) * 1000;
44 $times[] = $t;
45 print "Run $j: $t\n";
46 }
47 print "\n";
48
49 sort( $times, SORT_NUMERIC );
50 $n = $outerCount;
51 $min = $times[0];
52 $max = end( $times );
53 if ( $n % 2 ) {
54 // @phan-suppress-next-line PhanTypeMismatchDimFetch
55 $median = $times[ ( $n - 1 ) / 2 ];
56 } else {
57 $median = ( $times[$n / 2] + $times[$n / 2 - 1] ) / 2;
58 }
59 $mean = array_sum( $times ) / $n;
60
61 print "Minimum: $min ms\n";
62 print "Median: $median ms\n";
63 print "Mean: $mean ms\n";
64 print "Maximum: $max ms\n";
65 print "Memory usage: " . $contLang->formatSize( memory_get_usage( true ) ) . "\n";
66 print "Peak memory usage: " .
67 $contLang->formatSize( memory_get_peak_usage( true ) ) . "\n";
68 }
69}
70
71// @codeCoverageIgnoreStart
72$maintClass = BenchmarkTidy::class;
73require_once RUN_MAINTENANCE_IF_MAIN;
74// @codeCoverageIgnoreEnd
$maintClass
__construct()
Default constructor.
execute()
Do the actual work.
Base class for benchmark scripts.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
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.
getServiceContainer()
Returns the main service container.