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