mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-12-06 20:58:55 +00:00
Shortcodes/Formatting: Add PCRE Performance Testing
* Move pattern from `wptexturize()` into a separate function. * Move pattern from `wp_html_split()` into a separate function. * Beautify code for `wp_html_split()`. * Remove unnecessary instances of `/s` modifier in patterns that don't use dots. * Add `tests/phpunit/data/formatting/whole-posts.php` for testing larger strings. * Add function `benchmark_pcre_backtracking()`. * Add tests for `wp_html_split()`. * Add tests for `wptexturize()`. * Add tests for `get_shortcode_regex()`. Props miqrogroove. Fixes #34121. git-svn-id: https://develop.svn.wordpress.org/trunk@34761 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -390,3 +390,59 @@ class wpdb_exposed_methods_for_testing extends wpdb {
|
||||
return call_user_func_array( array( $this, $name ), $arguments );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine approximate backtrack count when running PCRE.
|
||||
*
|
||||
* @return int The backtrack count.
|
||||
*/
|
||||
function benchmark_pcre_backtracking( $pattern, $subject, $strategy ) {
|
||||
$saved_config = ini_get( 'pcre.backtrack_limit' );
|
||||
|
||||
// Attempt to prevent PHP crashes. Adjust these lower when needed.
|
||||
if ( version_compare( phpversion(), '5.4.8', '>' ) ) {
|
||||
$limit = 1000000;
|
||||
} else {
|
||||
$limit = 20000; // 20,000 is a reasonable upper limit, but see also https://core.trac.wordpress.org/ticket/29557#comment:10
|
||||
}
|
||||
|
||||
// Start with small numbers, so if a crash is encountered at higher numbers we can still debug the problem.
|
||||
for( $i = 4; $i <= $limit; $i *= 2 ) {
|
||||
|
||||
ini_set( 'pcre.backtrack_limit', $i );
|
||||
|
||||
switch( $strategy ) {
|
||||
case 'split':
|
||||
preg_split( $pattern, $subject );
|
||||
break;
|
||||
case 'match':
|
||||
preg_match( $pattern, $subject );
|
||||
break;
|
||||
case 'match_all':
|
||||
preg_match_all( $pattern, $subject );
|
||||
break;
|
||||
}
|
||||
|
||||
ini_set( 'pcre.backtrack_limit', $saved_config );
|
||||
|
||||
switch( preg_last_error() ) {
|
||||
case PREG_NO_ERROR:
|
||||
return $i;
|
||||
case PREG_BACKTRACK_LIMIT_ERROR:
|
||||
continue;
|
||||
case PREG_RECURSION_LIMIT_ERROR:
|
||||
trigger_error('PCRE recursion limit encountered before backtrack limit.');
|
||||
break;
|
||||
case PREG_BAD_UTF8_ERROR:
|
||||
trigger_error('UTF-8 error during PCRE benchmark.');
|
||||
break;
|
||||
case PREG_INTERNAL_ERROR:
|
||||
trigger_error('Internal error during PCRE benchmark.');
|
||||
break;
|
||||
default:
|
||||
trigger_error('Unexpected error during PCRE benchmark.');
|
||||
}
|
||||
}
|
||||
|
||||
return $i;
|
||||
}
|
||||
@@ -2048,4 +2048,29 @@ String with a number followed by a single quote !q1!Expendables 3!q1! vestibulum
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Automated performance testing of the main regex.
|
||||
*
|
||||
* @dataProvider data_whole_posts
|
||||
*/
|
||||
function test_pcre_performance( $input ) {
|
||||
global $shortcode_tags;
|
||||
|
||||
// With Shortcodes Disabled
|
||||
$regex = _get_wptexturize_split_regex( );
|
||||
$result = benchmark_pcre_backtracking( $regex, $input, 'split' );
|
||||
$this->assertLessThan( 200, $result );
|
||||
|
||||
// With Shortcodes Enabled
|
||||
$shortcode_regex = _get_wptexturize_shortcode_regex( array_keys( $shortcode_tags ) );
|
||||
$regex = _get_wptexturize_split_regex( $shortcode_regex );
|
||||
$result = benchmark_pcre_backtracking( $regex, $input, 'split' );
|
||||
return $this->assertLessThan( 200, $result );
|
||||
}
|
||||
|
||||
function data_whole_posts() {
|
||||
require_once( DIR_TESTDATA . '/formatting/whole-posts.php' );
|
||||
return data_whole_posts();
|
||||
}
|
||||
}
|
||||
@@ -616,4 +616,20 @@ EOF;
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Automated performance testing of the main regex.
|
||||
*
|
||||
* @dataProvider data_whole_posts
|
||||
*/
|
||||
function test_pcre_performance( $input ) {
|
||||
$regex = '/' . get_shortcode_regex() . '/';
|
||||
$result = benchmark_pcre_backtracking( $regex, $input, 'match_all' );
|
||||
return $this->assertLessThan( 200, $result );
|
||||
}
|
||||
|
||||
function data_whole_posts() {
|
||||
require_once( DIR_TESTDATA . '/formatting/whole-posts.php' );
|
||||
return data_whole_posts();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user