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