- wp-before-template: Time it takes from WordPress core's timer_start() call until the end of template_redirect action
- wp-template: Time it takes from end of template_redirect action to start of shutdown
- wp-total: Sum of both of the above metrics
Last active
February 1, 2023 06:33
-
-
Save mukeshpanchal27/429d5f16b5bb9125ef298d830ff3e395 to your computer and use it in GitHub Desktop.
Mini MU plugin for Server-Timing metric
This file contains hidden or 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( | |
'template_redirect', | |
function() { | |
ob_start(); | |
global $server_timing_values, $timestart; | |
if ( ! is_array( $server_timing_values ) ) { | |
$server_timing_values = array(); | |
} | |
$server_timing_values['before-template'] = microtime( true ) - $timestart; | |
add_action( | |
'shutdown', | |
function() { | |
global $server_timing_values, $timestart; | |
if ( ! is_array( $server_timing_values ) ) { | |
$server_timing_values = array(); | |
} | |
$server_timing_values['template'] = microtime( true ) - $timestart; | |
$server_timing_values['total'] = $server_timing_values['before-template'] + $server_timing_values['template']; | |
$output = ob_get_clean(); | |
$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; | |
}, | |
-9999 | |
); | |
}, | |
-9999 | |
); |
This file contains hidden or 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( | |
'template_redirect', | |
function() { | |
ob_start(); | |
global $server_timing_values, $timestart; | |
if ( ! is_array( $server_timing_values ) ) { | |
$server_timing_values = array(); | |
} | |
$server_timing_values['before-template'] = microtime( true ) - $timestart; | |
$output = ob_get_clean(); | |
$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; | |
}, | |
-9999 | |
); | |
add_action( | |
'template_redirect', | |
function() { | |
ob_start(); | |
add_action( | |
'shutdown', | |
function() { | |
global $server_timing_values, $timestart; | |
if ( ! is_array( $server_timing_values ) ) { | |
$server_timing_values = array(); | |
} | |
$server_timing_values['template'] = microtime( true ) - $timestart; | |
$server_timing_values['total'] = $server_timing_values['before-template'] + $server_timing_values['template']; | |
$output = ob_get_clean(); | |
$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; | |
}, | |
-9999 | |
); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment