Skip to content

Instantly share code, notes, and snippets.

@mukeshpanchal27
Created November 6, 2023 09:24
Show Gist options
  • Save mukeshpanchal27/8a087f67a90e0526c703ec8e26514e0a to your computer and use it in GitHub Desktop.
Save mukeshpanchal27/8a087f67a90e0526c703ec8e26514e0a to your computer and use it in GitHub Desktop.
Profile a modified version of `get_query_template` to see if we can speed it up.

Benchmark for get_query_template

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

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

Average time spent for 10 x 18 iterations.

TT3 ( Block theme )

get_query_template() - 0.04080924987793 new_get_query_template() - 0.039115571975708 Change: -4.2%
get_query_template() - 0.036437177658081 new_get_query_template() - 0.033733105659485 Change: -7.4%
get_query_template() - 0.03502893447876 new_get_query_template() - 0.033513689041138 Change: -4.3%
get_query_template() - 0.035555481910706 new_get_query_template() - 0.033641672134399 Change: -5.4%
get_query_template() - 0.036332750320435 new_get_query_template() - 0.034693217277527 Change: -4.5%
get_query_template() - 0.035776114463806 new_get_query_template() - 0.034015727043152 Change: -4.9%
get_query_template() - 0.035825991630554 new_get_query_template() - 0.03460590839386 Change: -3.4%
get_query_template() - 0.036041331291199 new_get_query_template() - 0.033895254135132 Change: -6%
get_query_template() - 0.035417985916138 new_get_query_template() - 0.033571100234985 Change: -5.2%
get_query_template() - 0.03657169342041 new_get_query_template() - 0.034809374809265 Change: -4.8%

TT1 ( Classic theme )

get_query_template() - 0.001415491104126 new_get_query_template() - 0.0013654470443726 Change: -3.5%
get_query_template() - 0.0014647960662842 new_get_query_template() - 0.0013648748397827 Change: -6.8%
get_query_template() - 0.0014342069625854 new_get_query_template() - 0.0013604879379272 Change: -5.1%
get_query_template() - 0.0014017581939697 new_get_query_template() - 0.0013597965240479 Change: -3%
get_query_template() - 0.0014153242111206 new_get_query_template() - 0.0013453006744385 Change: -4.9%
get_query_template() - 0.0014492034912109 new_get_query_template() - 0.0013694286346436 Change: -5.5%
get_query_template() - 0.0013886690139771 new_get_query_template() - 0.0013503074645996 Change: -2.8%
get_query_template() - 0.0015052080154419 new_get_query_template() - 0.0014845132827759 Change: -1.4%
get_query_template() - 0.0013949394226074 new_get_query_template() - 0.0013380289077759 Change: -4.1%
get_query_template() - 0.0014493227005005 new_get_query_template() - 0.0014261722564697 Change: -1.6%
<?php
// Bootstrapping.
define('WP_USE_THEMES', true);
/** Loads the WordPress Environment and Template */
require ('wp-load.php');
// Setup.
$urls = [
'404',
'archive',
'attachment',
'author',
'category',
'date',
'embed',
'frontpage',
'home',
'index',
'page',
'paged',
'privacypolicy',
'search',
'single',
'singular',
'tag',
'taxonomy',
];
$repetitions = 10;
$iterations = 19;
$stringcount = count( $urls );
function microtime_float() {
list( $usec, $sec ) = explode( ' ', microtime() );
return ( (float) $usec + (float) $sec );
}
// Refactoring functions.
function new_get_query_template( $type, $templates = array() ) {
$type = preg_replace( '|[^a-z0-9-]+|', '', $type );
if ( empty( $templates ) ) {
$templates = array( "{$type}.php" );
}
/**
* Filters the list of template filenames that are searched for when retrieving a template to use.
*
* The dynamic portion of the hook name, `$type`, refers to the filename -- minus the file
* extension and any non-alphanumeric characters delimiting words -- of the file to load.
* The last element in the array should always be the fallback template for this query type.
*
* Possible hook names include:
*
* - `404_template_hierarchy`
* - `archive_template_hierarchy`
* - `attachment_template_hierarchy`
* - `author_template_hierarchy`
* - `category_template_hierarchy`
* - `date_template_hierarchy`
* - `embed_template_hierarchy`
* - `frontpage_template_hierarchy`
* - `home_template_hierarchy`
* - `index_template_hierarchy`
* - `page_template_hierarchy`
* - `paged_template_hierarchy`
* - `privacypolicy_template_hierarchy`
* - `search_template_hierarchy`
* - `single_template_hierarchy`
* - `singular_template_hierarchy`
* - `tag_template_hierarchy`
* - `taxonomy_template_hierarchy`
*
* @since 4.7.0
*
* @param string[] $templates A list of template candidates, in descending order of priority.
*/
$templates = apply_filters( "{$type}_template_hierarchy", $templates );
$block_template = locate_block_template( '', $type, $templates );
if ( $block_template ) {
return $block_template;
}
$template = locate_template( $templates );
/**
* Filters the path of the queried template by type.
*
* The dynamic portion of the hook name, `$type`, refers to the filename -- minus the file
* extension and any non-alphanumeric characters delimiting words -- of the file to load.
* This hook also applies to various types of files loaded as part of the Template Hierarchy.
*
* Possible hook names include:
*
* - `404_template`
* - `archive_template`
* - `attachment_template`
* - `author_template`
* - `category_template`
* - `date_template`
* - `embed_template`
* - `frontpage_template`
* - `home_template`
* - `index_template`
* - `page_template`
* - `paged_template`
* - `privacypolicy_template`
* - `search_template`
* - `single_template`
* - `singular_template`
* - `tag_template`
* - `taxonomy_template`
*
* @since 1.5.0
* @since 4.8.0 The `$type` and `$templates` parameters were added.
*
* @param string $template Path to the template. See locate_template().
* @param string $type Sanitized filename without extension.
* @param string[] $templates A list of template candidates, in descending order of priority.
*/
return apply_filters( "{$type}_template", $template, $type, $templates );
}
// 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 = get_query_template( $url );
}
$time_end = microtime_float();
$get_query_template_time[ $repetition ] = $time_end - $time_start;
$time_start = microtime_float();
for ( $index = 0; $index < $iterations; $index ++ ) {
$url = $urls[ $index % $stringcount ];
$result = new_get_query_template( $url );
}
$time_end = microtime_float();
$new_get_query_template_time[ $repetition ] = $time_end - $time_start;
}
$get_query_template_time_avg = array_sum( $get_query_template_time ) / $repetitions;
$new_get_query_template_time_avg = array_sum( $new_get_query_template_time ) / $repetitions;
$percentage = round( $new_get_query_template_time_avg / $get_query_template_time_avg * 100, 1 ) - 100;
printf( "Average time spent for {$repetitions} x {$iterations} iterations:\n get_query_template() - {$get_query_template_time_avg}\nnew_get_query_template() - {$new_get_query_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