Debian Codegraph @

maintenance/benchmarks/benchmarkTidy.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Benchmark
20 */
21
22 // @codeCoverageIgnoreStart
23 require_once __DIR__ . '/../includes/Benchmarker.php';
24 // @codeCoverageIgnoreEnd
25
26 class 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;
85 require_once RUN_MAINTENANCE_IF_MAIN;
86 // @codeCoverageIgnoreEnd