$ composer run-script test
> phpbench run Bench.php --report=default
PHPBench (1.2.10) running benchmarks... #standwithukraine
with PHP version 7.4.33, xdebug ❌, opcache ❌
\Bench
benchTrunk..............................I4 - Mo7.625ms (±2.07%)
benchPatch..............................I4 - Mo3.284ms (±3.08%)
Subjects: 2, Assertions: 0, Failures: 0, Errors: 0
+------+-----------+------------+-----+------+------------+-------------+--------------+----------------+
| iter | benchmark | subject | set | revs | mem_peak | time_avg | comp_z_value | comp_deviation |
+------+-----------+------------+-----+------+------------+-------------+--------------+----------------+
| 0 | Bench | benchTrunk | | 100 | 4,242,960b | 7,900.850μs | +1.00σ | +2.08% |
| 1 | Bench | benchTrunk | | 100 | 4,242,960b | 7,966.200μs | +1.41σ | +2.92% |
| 2 | Bench | benchTrunk | | 100 | 4,242,960b | 7,639.260μs | -0.63σ | -1.30% |
| 3 | Bench | benchTrunk | | 100 | 4,242,960b | 7,595.520μs | -0.90σ | -1.87% |
| 4 | Bench | benchTrunk | | 100 | 4,242,960b | 7,598.570μs | -0.88σ | -1.83% |
| 0 | Bench | benchPatch | | 100 | 4,242,960b | 3,169.200μs | -1.17σ | -3.61% |
| 1 | Bench | benchPatch | | 100 | 4,242,960b | 3,184.240μs | -1.02σ | -3.16% |
| 2 | Bench | benchPatch | | 100 | 4,242,960b | 3,441.970μs | +1.52σ | +4.68% |
| 3 | Bench | benchPatch | | 100 | 4,242,960b | 3,336.390μs | +0.48σ | +1.47% |
| 4 | Bench | benchPatch | | 100 | 4,242,960b | 3,308.470μs | +0.20σ | +0.62% |
+------+-----------+------------+-----+------+------------+-------------+--------------+----------------+
Last active
April 3, 2023 22:46
-
-
Save westonruter/7f916bee3f45ef9307235daefb603e7c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Patch { | |
/** | |
* Makes URLs relative to the WordPress installation. | |
* | |
* @since 5.9.0 | |
* @access private | |
* | |
* @param string $css The CSS to make URLs relative to the WordPress installation. | |
* @param string $stylesheet_url The URL to the stylesheet. | |
* | |
* @return string The CSS with URLs made relative to the WordPress installation. | |
*/ | |
function _wp_normalize_relative_css_links( $css, $stylesheet_url ) { | |
return preg_replace_callback( | |
'#(url\s*\(\s*[\'"]?\s*)([^\'"\)]+)#', | |
static function ( $matches ) use ( $stylesheet_url ) { | |
list( , $prefix, $url ) = $matches; | |
if ( ! ( | |
str_starts_with( $url, 'http:' ) | |
|| | |
str_starts_with( $url, 'https:' ) | |
|| | |
str_starts_with( $url, '//' ) | |
|| | |
str_starts_with( $url, '#' ) | |
|| | |
str_starts_with( $url, 'data:' ) | |
) ) { | |
// Build the absolute URL. | |
$absolute_url = dirname( $stylesheet_url ) . '/' . $url; | |
$absolute_url = str_replace( '/./', '/', $absolute_url ); | |
// Convert to URL related to the site root. | |
$url = wp_make_link_relative( $absolute_url ); | |
} | |
return $prefix . $url; | |
}, | |
$css | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Trunk { | |
/** | |
* Makes URLs relative to the WordPress installation. | |
* | |
* @since 5.9.0 | |
* @access private | |
* | |
* @param string $css The CSS to make URLs relative to the WordPress installation. | |
* @param string $stylesheet_url The URL to the stylesheet. | |
* | |
* @return string The CSS with URLs made relative to the WordPress installation. | |
*/ | |
function _wp_normalize_relative_css_links( $css, $stylesheet_url ) { | |
$has_src_results = preg_match_all( '#url\s*\(\s*[\'"]?\s*([^\'"\)]+)#', $css, $src_results ); | |
if ( $has_src_results ) { | |
// Loop through the URLs to find relative ones. | |
foreach ( $src_results[1] as $src_index => $src_result ) { | |
// Skip if this is an absolute URL. | |
if ( 0 === strpos( $src_result, 'http' ) || 0 === strpos( $src_result, '//' ) ) { | |
continue; | |
} | |
// Skip if the URL is an HTML ID. | |
if ( str_starts_with( $src_result, '#' ) ) { | |
continue; | |
} | |
// Skip if the URL is a data URI. | |
if ( str_starts_with( $src_result, 'data:' ) ) { | |
continue; | |
} | |
// Build the absolute URL. | |
$absolute_url = dirname( $stylesheet_url ) . '/' . $src_result; | |
$absolute_url = str_replace( '/./', '/', $absolute_url ); | |
// Convert to URL related to the site root. | |
$relative_url = wp_make_link_relative( $absolute_url ); | |
// Replace the URL in the CSS. | |
$css = str_replace( | |
$src_results[0][ $src_index ], | |
str_replace( $src_result, $relative_url, $src_results[0][ $src_index ] ), | |
$css | |
); | |
} | |
} | |
return $css; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__ . '/functions.php'; | |
require_once __DIR__ . '/_wp_normalize_relative_css_links.trunk.php'; | |
require_once __DIR__ . '/_wp_normalize_relative_css_links.patch.php'; | |
/** | |
* @BeforeMethods("setUp") | |
* @Revs(100) | |
* @Iterations(5) | |
*/ | |
class Bench { | |
public $files = []; | |
public function setUp() { | |
$directory = new RecursiveDirectoryIterator( 'wp-content' ); | |
$iterator = new RecursiveIteratorIterator( $directory ); | |
$this->files = iterator_to_array( new RegexIterator( $iterator, '/^.+\.css$/' ) ); | |
} | |
/** | |
* Test _wp_normalize_relative_css_links() in trunk. | |
*/ | |
function benchTrunk() { | |
foreach ( $this->files as $file ) { | |
$css = file_get_contents( $file ); | |
$url = "https://example.com/$file"; | |
\Trunk\_wp_normalize_relative_css_links( $css, $file ); | |
} | |
} | |
/** | |
* Test patched version of _wp_normalize_relative_css_links(). | |
*/ | |
function benchPatch() { | |
foreach ( $this->files as $file ) { | |
$css = file_get_contents( $file ); | |
$url = "https://example.com/$file"; | |
\Patch\_wp_normalize_relative_css_links( $css, $file ); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "westonruter/phpbench-wp-normalize-relative-css-links", | |
"description": "Benchmarking performance of _wp_normalize_relative_css_links() in WordPress core", | |
"repositories":[ | |
{ | |
"type":"composer", | |
"url":"https://wpackagist.org", | |
"only": [ | |
"wpackagist-plugin/*", | |
"wpackagist-theme/*" | |
] | |
} | |
], | |
"license": "GPL2", | |
"authors": [ | |
{ | |
"name": "Weston Ruter", | |
"email": "westonruter@google.com" | |
} | |
], | |
"require": { | |
"phpbench/phpbench": "^1.2", | |
"wpackagist-theme/twentytwentytwo": "^1.4", | |
"wpackagist-plugin/gutenberg": "^15.4", | |
"wpackagist-theme/twentytwentythree": "^1.1", | |
"wpackagist-theme/twentytwentyone": "^1.8", | |
"wpackagist-theme/twentytwenty": "^2.2", | |
"wpackagist-theme/twentynineteen": "^2.5", | |
"wpackagist-theme/twentyseventeen": "^3.2", | |
"wpackagist-theme/twentysixteen": "^2.9", | |
"wpackagist-theme/twentyfifteen": "^3.4", | |
"wpackagist-theme/twentyfourteen": "^3.6", | |
"wpackagist-theme/twentythirteen": "^3.8", | |
"wpackagist-theme/twentytwelve": "^3.9", | |
"wpackagist-theme/twentyeleven": "^4.3", | |
"wpackagist-theme/twentyten": "^3.8" | |
}, | |
"scripts": { | |
"test": "phpbench run Bench.php --report=default" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Converts full URL paths to absolute paths. | |
* | |
* Removes the http or https protocols and the domain. Keeps the path '/' at the | |
* beginning, so it isn't a true relative link, but from the web root base. | |
* | |
* @link https://github.com/WordPress/wordpress-develop/blob/b98b5e59d72a68bc31f99b866fa26f7769d16e97/src/wp-includes/formatting.php#L4717-L4731 | |
* | |
* @since 2.1.0 | |
* @since 4.1.0 Support was added for relative URLs. | |
* | |
* @param string $link Full URL path. | |
* @return string Absolute path. | |
*/ | |
function wp_make_link_relative( $link ) { | |
return preg_replace( '|^(https?:)?//[^/]+(/?.*)|i', '$2', $link ); | |
} | |
if ( ! function_exists( 'str_starts_with' ) ) { | |
/** | |
* Polyfill for `str_starts_with()` function added in PHP 8.0. | |
* | |
* Performs a case-sensitive check indicating if | |
* the haystack begins with needle. | |
* | |
* @link https://github.com/WordPress/wordpress-develop/blob/b98b5e59d72a68bc31f99b866fa26f7769d16e97/src/wp-includes/compat.php#LL445-L465C2 | |
* | |
* @since 5.9.0 | |
* | |
* @param string $haystack The string to search in. | |
* @param string $needle The substring to search for in the `$haystack`. | |
* @return bool True if `$haystack` starts with `$needle`, otherwise false. | |
*/ | |
function str_starts_with( $haystack, $needle ) { | |
if ( '' === $needle ) { | |
return true; | |
} | |
return 0 === strpos( $haystack, $needle ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment