Make WordPress Core


Ignore:
Timestamp:
10/02/2015 04:25:40 AM (11 years ago)
Author:
wonderboymusic
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/utils.php

    r34655 r34761  
    391391        }
    392392}
     393
     394/**
     395 * Determine approximate backtrack count when running PCRE.
     396 *
     397 * @return int The backtrack count.
     398 */
     399function benchmark_pcre_backtracking( $pattern, $subject, $strategy ) {
     400        $saved_config = ini_get( 'pcre.backtrack_limit' );
     401       
     402        // Attempt to prevent PHP crashes.  Adjust these lower when needed.
     403        if ( version_compare( phpversion(), '5.4.8', '>' ) ) {
     404                $limit = 1000000;
     405        } else {
     406                $limit = 20000;  // 20,000 is a reasonable upper limit, but see also https://core.trac.wordpress.org/ticket/29557#comment:10
     407        }
     408
     409        // Start with small numbers, so if a crash is encountered at higher numbers we can still debug the problem.
     410        for( $i = 4; $i <= $limit; $i *= 2 ) {
     411
     412                ini_set( 'pcre.backtrack_limit', $i );
     413               
     414                switch( $strategy ) {
     415                case 'split':
     416                        preg_split( $pattern, $subject );
     417                        break;
     418                case 'match':
     419                        preg_match( $pattern, $subject );
     420                        break;
     421                case 'match_all':
     422                        preg_match_all( $pattern, $subject );
     423                        break;
     424                }
     425
     426                ini_set( 'pcre.backtrack_limit', $saved_config );
     427
     428                switch( preg_last_error() ) {
     429                case PREG_NO_ERROR:
     430                        return $i;
     431                case PREG_BACKTRACK_LIMIT_ERROR:
     432                        continue;
     433                case PREG_RECURSION_LIMIT_ERROR:
     434                        trigger_error('PCRE recursion limit encountered before backtrack limit.');
     435                        break;
     436                case PREG_BAD_UTF8_ERROR:
     437                        trigger_error('UTF-8 error during PCRE benchmark.');
     438                        break;
     439                case PREG_INTERNAL_ERROR:
     440                        trigger_error('Internal error during PCRE benchmark.');
     441                        break;
     442                default:
     443                        trigger_error('Unexpected error during PCRE benchmark.');       
     444                }
     445        }
     446
     447        return $i;
     448}
Note: See TracChangeset for help on using the changeset viewer.