Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active April 3, 2023 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westonruter/7f916bee3f45ef9307235daefb603e7c to your computer and use it in GitHub Desktop.
Save westonruter/7f916bee3f45ef9307235daefb603e7c to your computer and use it in GitHub Desktop.
$ 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%         |
+------+-----------+------------+-----+------+------------+-------------+--------------+----------------+
<?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 );
}
}
}
{
"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"
}
}
<?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