wordpress-develop/tests/performance/wp-content/mu-plugins/server-timing.php
Pascal Birchler 74e0604508 Build/Test Tools: Expand performance test scenarios.
Adds new tests for localized sites as well as the dashboard.
Also amends Server-Timing output to measure memory usage in all scenarios.

Props swissspidy, joemcgill, flixos90, mukesh27, mamaduka.
See #59656.
Fixes #59815.

git-svn-id: https://develop.svn.wordpress.org/trunk@57083 602fd350-edb4-49c9-b593-d223f7449a82
2023-11-08 10:30:21 +00:00

53 lines
1.3 KiB
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'];
/*
* While values passed via Server-Timing are intended to be durations,
* any numeric value can actually be passed.
* This is a nice little trick as it allows to easily get this information in JS.
*/
$server_timing_values['memory-usage'] = memory_get_usage();
$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
);