Last active
November 24, 2022 00:29
Some example use-cases for the Server Timing API in the Performance Lab plugin (see https://github.com/WordPress/performance/pull/553). A previous version for an older implementation of the API can be found at https://gist.github.com/felixarntz/c343a33242b6527ad3f446c8f7f4fec2.
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 | |
add_action( | |
'plugins_loaded', | |
function() { | |
// Utility function that returns a measuring callback for an action hook, | |
// which should be registered as a metric within the action as early as possible | |
// (see PHP_INT_MIN usage below). | |
$get_measure_action_callback = function( $action_hook ) { | |
return function( $metric ) use ( $action_hook ) { | |
$metric->measure_before(); | |
add_action( $action_hook, array( $metric, 'measure_after' ), PHP_INT_MAX ); | |
}; | |
}; | |
// Measure execution time of various critical WordPress action hooks. | |
$actions_to_measure = array( | |
'plugins-loaded' => 'plugins_loaded', | |
'after-setup-theme' => 'after_setup_theme', | |
'init' => 'init', | |
'loaded' => 'wp_loaded', | |
'template-redirect' => 'template_redirect', | |
); | |
if ( perflab_server_timing_use_output_buffer() ) { | |
$actions_to_measure['head'] = 'wp_head'; | |
$actions_to_measure['footer'] = 'wp_footer'; | |
} | |
foreach ( $actions_to_measure as $metric_slug => $action_hook ) { | |
$measure_action_callback = $get_measure_action_callback( $action_hook ); | |
// If action is already running, at least measure the remainder. | |
if ( doing_action( $action_hook ) ) { | |
perflab_server_timing_register_metric( | |
$metric_slug, | |
array( | |
'measure_callback' => $measure_action_callback, | |
'access_cap' => 'exist', | |
) | |
); | |
} else { | |
add_action( | |
$action_hook, | |
function() use ( $metric_slug, $measure_action_callback ) { | |
perflab_server_timing_register_metric( | |
$metric_slug, | |
array( | |
'measure_callback' => $measure_action_callback, | |
'access_cap' => 'exist', | |
) | |
); | |
}, | |
PHP_INT_MIN | |
); | |
} | |
} | |
// Measure the time it takes to parse the request (including, but not exclusively, the 'parse_request' action). | |
add_filter( | |
'do_parse_request', | |
function( $passthrough ) { | |
perflab_server_timing_register_metric( | |
'parse-request', | |
array( | |
'measure_callback' => function( $metric ) { | |
$metric->measure_before(); | |
add_action( 'parse_request', array( $metric, 'measure_after' ), PHP_INT_MAX ); | |
}, | |
'access_cap' => 'exist', | |
) | |
); | |
return $passthrough; | |
}, | |
PHP_INT_MAX | |
); | |
// Measure the time it takes to locate the template. | |
add_action( | |
'template_redirect', | |
function() { | |
perflab_server_timing_register_metric( | |
'locate-template', | |
array( | |
'measure_callback' => function( $metric ) { | |
$metric->measure_before(); | |
add_filter( | |
'template_include', | |
function( $passthrough ) use ( $metric ) { | |
$metric->measure_after(); | |
return $passthrough; | |
}, | |
PHP_INT_MIN | |
); | |
}, | |
'access_cap' => 'exist', | |
) | |
); | |
}, | |
PHP_INT_MAX | |
); | |
}, | |
0 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment