wordpress-develop/tests/performance/wp-content/mu-plugins/server-timing.php
Felix Arntz a3b80f551f Build/Test: Fix incorrect hook being used to separate Server-Timing metrics in performance tests.
The `wp-before-template` and `wp-template` metric intend to separate WordPress core's own bootstrap process from any logic that is part of the eventual template loaded. The appropriate hook for that is the `template_include` filter as that occurs right before the template file is included.

Props mukesh27, joemcgill.
Fixes #58674.


git-svn-id: https://develop.svn.wordpress.org/trunk@56162 602fd350-edb4-49c9-b593-d223f7449a82
2023-07-07 17:39:09 +00:00

46 lines
1018 B
PHP

<?php
add_filter(
'template_include',
static function( $template ) {
global $timestart;
$server_timing_values = array();
$template_start = microtime( true );
$server_timing_values['before-template'] = $template_start - $timestart;
ob_start();
add_action(
'shutdown',
static function() use ( $server_timing_values, $template_start ) {
global $timestart;
$output = ob_get_clean();
$server_timing_values['template'] = microtime( true ) - $template_start;
$server_timing_values['total'] = $server_timing_values['before-template'] + $server_timing_values['template'];
$header_values = array();
foreach ( $server_timing_values as $slug => $value ) {
if ( is_float( $value ) ) {
$value = round( $value * 1000.0, 2 );
}
$header_values[] = sprintf( 'wp-%1$s;dur=%2$s', $slug, $value );
}
header( 'Server-Timing: ' . implode( ', ', $header_values ) );
echo $output;
},
PHP_INT_MIN
);
return $template;
},
PHP_INT_MAX
);