Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active September 8, 2023 04:40
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/469c9de1ee699c97285e8bc8cc688991 to your computer and use it in GitHub Desktop.
Save westonruter/469c9de1ee699c97285e8bc8cc688991 to your computer and use it in GitHub Desktop.
Script to test changes to themes in https://core.trac.wordpress.org/ticket/59316
<?php
/**
* Benchmark Web Vitals for Themes with Script Loading Strategies.
*
* USAGE:
* 1. Place this script is in the root of the WordPress/wordpress-develop Git repo; make sure environment is started.
* 2. Fetch the trac-59316 branch from https://github.com/WordPress/wordpress-develop/pull/5170.
* 3. Clone the GoogleChromeLabs/wpp-research repo, for example at ../wpp-research and run `npm install`.
* 4. Make sure that each theme has been configured, including the Showcase page for Twenty Eleven.
* 5. Then to invoke this script, do: WPP_RESEARCH_DIR=../wpp-research php benchmark-theme-web-vitals.php
*
* @package BenchmarkThemeWebVitals
* @link https://core.trac.wordpress.org/ticket/59316
* @author Weston Ruter
* @copyright Google, 2023
* @license GPL2
*/
namespace BenchmarkThemeWebVitals;
use Exception;
$themes = array(
'twentyeleven' => '/showcase/', // Page configured to set the showcase page template.
'twentytwelve' => '/sample-page/',
'twentythirteen' => '/sample-page/',
'twentyfourteen' => '/', // With featured content configured on homepage.
'twentyfifteen' => '/sample-page/',
'twentysixteen' => '/sample-page/',
'twentyseventeen' => '/', // Because the homepage has a large header.
'twentynineteen' => '/sample-page/',
'twentytwenty' => '/sample-page/',
'twentytwentyone' => '/sample-page/',
);
$baseline_branch = 'trunk';
$feature_branch = 'trac-59316';
$request_number = getenv( 'REQUEST_NUMBER' ) ? intval( getenv( 'REQUEST_NUMBER' ) ) : 20;
$home_url = 'http://localhost:8889';
$wpp_research_dir = getenv( 'WPP_RESEARCH_DIR' );
if ( ! $wpp_research_dir || ! is_dir( $wpp_research_dir ) ) {
fwrite( STDERR, "Missing WPP_RESEARCH_DIR environment variable.\n" );
exit( 1 );
}
$all_results = array();
foreach ( $themes as $theme => $path ) {
echo "# $theme\n";
system( sprintf( 'npm run env:cli -- theme activate %s', escapeshellarg( $theme ) ) );
$url = $home_url . $path;
try {
echo "Checking {$baseline_branch} branch...\n";
system( sprintf( 'git checkout --quiet %s', escapeshellarg( $baseline_branch ) ) );
$before_result = benchmark_web_vitals( $url, $request_number, $wpp_research_dir );
echo "Checking {$feature_branch} branch...\n";
system( sprintf( 'git checkout --quiet %s', escapeshellarg( $feature_branch ) ) );
$after_result = benchmark_web_vitals( $url, $request_number, $wpp_research_dir );
} catch ( Exception $e ) {
fwrite( STDERR, $e->getMessage() );
exit( 1 );
}
echo "$before_result => $after_result\n";
$all_results[ $theme ] = array( $before_result, $after_result );
}
echo "\n\n";
echo "Theme, Before, After, Diff (ms), Percentage\n";
foreach ( $all_results as $theme => list( $before, $after ) ) {
$diff = $after - $before;
echo join(
', ',
array(
$theme,
sprintf( '%.2f', $before ),
sprintf( '%.2f', $after ),
( $diff > 0 ? '+' : '' ) . sprintf( '%.2f', $diff ),
( $diff > 0 ? '+' : '-' ) . sprintf( '%.2f%%', abs( 100 - ( $after / $before ) * 100 ) ),
)
) . "\n";
}
function benchmark_web_vitals( string $url, int $request_number, string $wpp_research_dir ): float {
$output = shell_exec(
sprintf(
'cd %s && npm run --silent research benchmark-web-vitals -- -u %s -n %s -o csv',
escapeshellarg( $wpp_research_dir ),
escapeshellarg( $url ),
escapeshellarg( $request_number )
)
);
$lines = preg_split( "/\n/", trim( $output ) );
if ( count( $lines ) !== 6 ) {
throw new Exception( STDERR, "Unexpected line count:\n{$output}\n" );
}
if ( 'Success Rate,100%' !== $lines[1] ) {
throw new Exception( STDERR, "Success rate not 100%:\n{$output}\n" );
}
if ( ! preg_match( '/^LCP-TTFB.+?,(\d.+)/', $lines[5], $matches ) ) {
throw new Exception( STDERR, "Cannot find LCP-TTFB:\n{$output}\n" );
}
return floatval( $matches[1] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment