/
githubmirror
/
gutenberg
Обзор
Документация
Войти
/
githubmirror
/
gutenberg
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
trunk
packages/e2e-tests/mu-plugins/server-timing.php
91 строка
3 KB
Weston Ruter
Improve output buffer for sending server-timing header (#72536)
21 окт 2025, 18:46
Не верифицирован
21 окт 2025, 18:46
6d8fd43
Код
Авторство
О чём код?
<?php /* * Note: As of WordPress 6.9, there is a template enhancement output buffer which can be used instead of the following * template_include code which opens its own buffer. It is enabled by default in classic themes, but it can be enabled * by default even for block themes via: * * add_filter( 'wp_should_output_buffer_template_for_enhancement', '__return_true' ); * * Alternatively, instead of using the template_include filter to start output buffering, this can be done via the * wp_before_include_template action which fires after the template_include filter and immediately before the template * is loaded. */ 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( static function ( $output ) use ( $server_timing_values, $template_start, $wpdb ) { $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 ) ); return $output; } ); return $template; }, PHP_INT_MAX ); add_action( 'admin_init', static function () { global $timestart, $wpdb; ob_start( static function ( $output ) use ( $wpdb, $timestart ) { $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 ) ); return $output; } ); }, PHP_INT_MAX );