Skip to content

Instantly share code, notes, and snippets.

@mukeshpanchal27
Last active November 30, 2022 15:21
Show Gist options
  • Save mukeshpanchal27/b10f1360067e4429db1ef406f9e9dc02 to your computer and use it in GitHub Desktop.
Save mukeshpanchal27/b10f1360067e4429db1ef406f9e9dc02 to your computer and use it in GitHub Desktop.
Profile a modified version of `locate_template` to see if we can speed it up.

Benchmark for locate_template

Profile a modified version of locate_template to see if we can speed it up.

This is used to benchmark the patch introduced for Trac ticket #40731.

Results of the benchmark are found in a shared Google Sheet.

<?php
// Bootstrapping.
define('WP_USE_THEMES', true);
/** Loads the WordPress Environment and Template */
require ('wp-load.php');
// Setup.
$urls = [
[ 'front-page.php' ],
[ 'home.php', 'index.php' ],
[ 'header.php' ],
[ 'template-parts/header/site-header.php' ],
[ 'template-parts/header/site-branding.php' ],
[ 'template-parts/header/site-nav.php' ],
[ 'template-parts/content/content-excerpt.php', 'template-parts/content/content.php' ],
[ 'template-parts/header/excerpt-header.php' ],
[ 'template-parts/excerpt/excerpt.php' ],
[ 'footer.php' ],
[ 'template-parts/footer/footer-widgets.php' ],
];
$urls2 = [
[ 'front-page-2.php' ],
[ 'home-2.php', 'index-2.php' ],
[ 'header-2.php' ],
[ 'template-parts-2/header/site-header.php' ],
[ 'template-parts-2/header/site-branding.php' ],
[ 'template-parts-2/header/site-nav.php' ],
[ 'template-parts-2/content/content-excerpt.php', 'template-parts-2/content/content.php' ],
[ 'template-parts-2/header/excerpt-header.php' ],
[ 'template-parts-2/excerpt/excerpt.php' ],
[ 'footer-2.php' ],
[ 'template-parts-2/footer/footer-widgets.php' ],
];
$repetitions = 10;
$iterations = 11;
$stringcount = count( $urls );
function microtime_float() {
list( $usec, $sec ) = explode( ' ', microtime() );
return ( (float) $usec + (float) $sec );
}
// Refactoring functions.
function new_locate_template( $template_names, $load = false, $require_once = true, $args = array() ) {
$located = '';
static $template_cache = array();
foreach ( (array) $template_names as $template_name ) {
if ( ! $template_name ) {
continue;
}
if ( isset( $template_cache[ $template_name ] ) ) {
if ( '' === $template_cache[ $template_name ] ) {
continue;
}
$located = $template_cache[ $template_name ];
break;
}
if ( file_exists( STYLESHEETPATH . '/' . $template_name ) ) {
$located = $template_cache[$template_name] = STYLESHEETPATH . '/' . $template_name;
break;
} elseif ( file_exists( TEMPLATEPATH . '/' . $template_name ) ) {
$located = $template_cache[$template_name] = TEMPLATEPATH . '/' . $template_name;
break;
} elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
$located = $template_cache[$template_name] = ABSPATH . WPINC . '/theme-compat/' . $template_name;
break;
}
$template_cache[$template_name] = '';
}
if ( $load && '' !== $located ) {
load_template( $located, $require_once, $args );
}
return $located;
}
// Profiling.
for ( $repetition = 0; $repetition <= $repetitions; $repetition ++ ) {
if( $repetition == 0 ) {
continue;
}
$time_start = microtime_float();
for ( $index = 0; $index < $iterations; $index ++ ) {
$url = $urls[ $index % $stringcount ];
$result = locate_template( $url );
}
$time_end = microtime_float();
$locate_template_time[ $repetition ] = $time_end - $time_start;
$time_start = microtime_float();
for ( $index = 0; $index < $iterations; $index ++ ) {
$url = $urls2[ $index % $stringcount ];
$result = new_locate_template( $url );
}
$time_end = microtime_float();
$new_locate_template_time[ $repetition ] = $time_end - $time_start;
}
$locate_template_time_avg = array_sum( $locate_template_time ) / $repetitions;
$new_locate_template_time_avg = array_sum( $new_locate_template_time ) / $repetitions;
$percentage = round( $new_locate_template_time_avg / $locate_template_time_avg * 100, 1 ) - 100;
printf( "Average time spent for ${repetitions} x ${iterations} iterations:\n locate_template() - ${locate_template_time_avg}\nnew_locate_template() - ${new_locate_template_time_avg}\n\nChange: ${percentage}%%\n" );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment