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.
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.
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
22 require_once __DIR__
. '/Benchmarker.php';
25 * Maintenance script that benchmarks Sanitizer methods.
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 );
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 ),
46 $method = $this->getOption( 'method' );
49 if ( !$method ||
$method === 'validateEmail' ) {
50 $benches['Sanitizer::validateEmail (valid)'] = function () {
51 Sanitizer
::validateEmail( 'user@example.org' );
53 $benches['Sanitizer::validateEmail (invalid)'] = function () {
54 Sanitizer
::validateEmail( 'username@example! org' );
57 if ( !$method ||
$method === 'encodeAttribute' ) {
58 $benches['Sanitizer::encodeAttribute (simple)'] = function () {
59 Sanitizer
::encodeAttribute( 'simple' );
61 $benches['Sanitizer::encodeAttribute (special)'] = function () {
62 Sanitizer
::encodeAttribute( ":'\"\n https://example" );
65 if ( !$method ||
$method === 'safeEncodeAttribute' ) {
66 $benches['Sanitizer::safeEncodeAttribute (simple)'] = function () {
67 Sanitizer
::safeEncodeAttribute( 'simple' );
69 $benches['Sanitizer::safeEncodeAttribute (special)'] = function () {
70 Sanitizer
::safeEncodeAttribute( ":'\"\n https://example" );
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 );
79 $benches["Sanitizer::removeHTMLtags (input: $lg)"] = function () use ( $textWithHtmlLg ) {
80 Sanitizer
::removeHTMLtags( $textWithHtmlLg );
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 );
89 $benches["Sanitizer::stripAllTags (input: $lg)"] = function () use ( $textWithHtmlLg ) {
90 Sanitizer
::stripAllTags( $textWithHtmlLg );
94 $this->bench( $benches );
98 $maintClass = BenchmarkSanitizer
::class;
99 require_once RUN_MAINTENANCE_IF_MAIN
;