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