Merge "Revert "selenium: add new message banner test to user spec""
[lhc/web/wiklou.git] / maintenance / benchmarks / benchmarkSanitizer.php
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 require_once __DIR__ . '/Benchmarker.php';
23
24 /**
25 * Maintenance script that benchmarks Sanitizer methods.
26 *
27 * @ingroup Benchmark
28 */
29 class BenchmarkSanitizer extends Benchmarker {
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;