-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Performance Tests: Improve collection and reporting (#61450)
- Loading branch information
1 parent
051a72f
commit bbe171a
Showing
8 changed files
with
255 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
add_filter( | ||
'template_include', | ||
static function ( $template ) { | ||
|
||
global $timestart, $wpdb; | ||
|
||
$server_timing_values = array(); | ||
$template_start = microtime( true ); | ||
|
||
$server_timing_values['wpBeforeTemplate'] = $template_start - $timestart; | ||
|
||
ob_start(); | ||
|
||
add_action( | ||
'shutdown', | ||
static function () use ( $server_timing_values, $template_start, $wpdb ) { | ||
$output = ob_get_clean(); | ||
|
||
$server_timing_values['wpTemplate'] = microtime( true ) - $template_start; | ||
|
||
$server_timing_values['wpTotal'] = $server_timing_values['wpBeforeTemplate'] + $server_timing_values['wpTemplate']; | ||
|
||
/* | ||
* 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['wpMemoryUsage'] = memory_get_usage(); | ||
$server_timing_values['wpDbQueries'] = $wpdb->num_queries; | ||
|
||
$header_values = array(); | ||
foreach ( $server_timing_values as $slug => $value ) { | ||
if ( is_float( $value ) ) { | ||
$value = round( $value * 1000.0, 2 ); | ||
} | ||
$header_values[] = sprintf( '%1$s;dur=%2$s', $slug, $value ); | ||
} | ||
header( 'Server-Timing: ' . implode( ', ', $header_values ) ); | ||
|
||
echo $output; | ||
}, | ||
PHP_INT_MIN | ||
); | ||
|
||
return $template; | ||
}, | ||
PHP_INT_MAX | ||
); | ||
|
||
add_action( | ||
'admin_init', | ||
static function () { | ||
global $timestart, $wpdb; | ||
|
||
ob_start(); | ||
|
||
add_action( | ||
'shutdown', | ||
static function () use ( $wpdb, $timestart ) { | ||
$output = ob_get_clean(); | ||
|
||
$server_timing_values = array(); | ||
|
||
$server_timing_values['wpTotal'] = microtime( true ) - $timestart; | ||
|
||
/* | ||
* 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['wpMemoryUsage'] = memory_get_usage(); | ||
$server_timing_values['wpDbQueries'] = $wpdb->num_queries; | ||
|
||
$header_values = array(); | ||
foreach ( $server_timing_values as $slug => $value ) { | ||
if ( is_float( $value ) ) { | ||
$value = round( $value * 1000.0, 2 ); | ||
} | ||
$header_values[] = sprintf( '%1$s;dur=%2$s', $slug, $value ); | ||
} | ||
header( 'Server-Timing: ' . implode( ', ', $header_values ) ); | ||
|
||
echo $output; | ||
}, | ||
PHP_INT_MIN | ||
); | ||
}, | ||
PHP_INT_MAX | ||
); |
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
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
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
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
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