MediaWiki  1.34.0
benchmarkSanitizer.php
Go to the documentation of this file.
1 <?php
22 require_once __DIR__ . '/Benchmarker.php';
23 
30  public function __construct() {
31  parent::__construct();
32  $this->addDescription( 'Benchmark for Sanitizer methods.' );
33  $this->addOption( 'method', 'One of "validateEmail", "encodeAttribute", '
34  . '"safeEncodeAttribute", "removeHTMLtags", or "stripAllTags". '
35  . 'Default: (All)', false, true );
36  }
37 
38  public function execute() {
39  $textWithHtmlSm = 'Before <wrap><in>and</in> another <unclose> <in>word</in></wrap>.';
40  $textWithHtmlLg = str_repeat(
41  // 28K (28 chars * 1000)
42  wfRandomString( 3 ) . ' <tag>' . wfRandomString( 5 ) . '</tag> ' . wfRandomString( 7 ),
43  1000
44  );
45 
46  $method = $this->getOption( 'method' );
47  $benches = [];
48 
49  if ( !$method || $method === 'validateEmail' ) {
50  $benches['Sanitizer::validateEmail (valid)'] = function () {
51  Sanitizer::validateEmail( 'user@example.org' );
52  };
53  $benches['Sanitizer::validateEmail (invalid)'] = function () {
54  Sanitizer::validateEmail( 'username@example! org' );
55  };
56  }
57  if ( !$method || $method === 'encodeAttribute' ) {
58  $benches['Sanitizer::encodeAttribute (simple)'] = function () {
59  Sanitizer::encodeAttribute( 'simple' );
60  };
61  $benches['Sanitizer::encodeAttribute (special)'] = function () {
62  Sanitizer::encodeAttribute( ":'\"\n https://example" );
63  };
64  }
65  if ( !$method || $method === 'safeEncodeAttribute' ) {
66  $benches['Sanitizer::safeEncodeAttribute (simple)'] = function () {
67  Sanitizer::safeEncodeAttribute( 'simple' );
68  };
69  $benches['Sanitizer::safeEncodeAttribute (special)'] = function () {
70  Sanitizer::safeEncodeAttribute( ":'\"\n https://example" );
71  };
72  }
73  if ( !$method || $method === 'removeHTMLtags' ) {
74  $sm = strlen( $textWithHtmlSm );
75  $lg = round( strlen( $textWithHtmlLg ) / 1000 ) . 'K';
76  $benches["Sanitizer::removeHTMLtags (input: $sm)"] = function () use ( $textWithHtmlSm ) {
77  Sanitizer::removeHTMLtags( $textWithHtmlSm );
78  };
79  $benches["Sanitizer::removeHTMLtags (input: $lg)"] = function () use ( $textWithHtmlLg ) {
80  Sanitizer::removeHTMLtags( $textWithHtmlLg );
81  };
82  }
83  if ( !$method || $method === 'stripAllTags' ) {
84  $sm = strlen( $textWithHtmlSm );
85  $lg = round( strlen( $textWithHtmlLg ) / 1000 ) . 'K';
86  $benches["Sanitizer::stripAllTags (input: $sm)"] = function () use ( $textWithHtmlSm ) {
87  Sanitizer::stripAllTags( $textWithHtmlSm );
88  };
89  $benches["Sanitizer::stripAllTags (input: $lg)"] = function () use ( $textWithHtmlLg ) {
90  Sanitizer::stripAllTags( $textWithHtmlLg );
91  };
92  }
93 
94  $this->bench( $benches );
95  }
96 }
97 
98 $maintClass = BenchmarkSanitizer::class;
99 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
Benchmarker\bench
bench(array $benchs)
Definition: Benchmarker.php:50
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
BenchmarkSanitizer\execute
execute()
Do the actual work.
Definition: benchmarkSanitizer.php:38
Benchmarker
Base class for benchmark scripts.
Definition: Benchmarker.php:40
$maintClass
$maintClass
Definition: benchmarkSanitizer.php:98
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:302
BenchmarkSanitizer
Maintenance script that benchmarks Sanitizer methods.
Definition: benchmarkSanitizer.php:29
BenchmarkSanitizer\__construct
__construct()
Default constructor.
Definition: benchmarkSanitizer.php:30
wfRandomString
wfRandomString( $length=32)
Get a random string containing a number of pseudo-random hex characters.
Definition: GlobalFunctions.php:274