Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a1fd89c
Implement logic to measure specific hook execution time with Server-T…
Jul 18, 2023
1c21d08
Merge branch 'trunk' into add/server-timing-hook-ui
Jul 21, 2023
ec9708b
Fix newly exposed lint violations.
Jul 21, 2023
6a1205f
Use more appropriate workaround when PHP_INT_MIN is not defined.
Jul 21, 2023
08d79c3
Merge branch 'trunk' into add/server-timing-hook-ui
Aug 3, 2023
694a6d1
Reuse logic to measure actions and filters, and provide inline docume…
Aug 3, 2023
6b5d12a
Clarify how the hooks are measured in WP Admin UI.
Aug 3, 2023
61f5ccc
Ensure that no duplicate hooks can be provided.
Aug 3, 2023
585911d
Add tests for server timing setting registration.
Aug 3, 2023
6db1e85
Add tests for server timing admin UI.
Aug 3, 2023
a8d141f
Remove XHTML syntax from WP Admin forms.
Aug 7, 2023
400c0a1
Modify hook measuring logic to use all hook.
Aug 7, 2023
c5fe384
Replace sanitize_key for hook name sanitization with custom regex to …
Aug 7, 2023
447f078
Have hook callbacks self-remove to only measure first occurrence.
Aug 7, 2023
c48d206
Remove novalidate attribute.
Aug 7, 2023
b85959e
Loosen hook name sanitization to rely on sanitize_text_field(), while…
Aug 7, 2023
124a7a6
Update test to cover whitespace in hookname.
Aug 7, 2023
7f99021
Use allowlist for Server-Timing option keys.
Aug 7, 2023
05d849e
Fix tests.
Aug 7, 2023
fd41db1
Add note on hook limitation unless output buffering is enabled.
Aug 7, 2023
f484a7d
Use placeholders for non-translatable strings.
Aug 8, 2023
7c94b9e
Add missing early return on top of procedural file.
Aug 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Reuse logic to measure actions and filters, and provide inline docume…
…ntation for better understanding.
  • Loading branch information
Felix Arntz
Felix Arntz committed Aug 3, 2023
commit 694a6d1d4e3c968102ddce960ebbb252f468ed37
99 changes: 49 additions & 50 deletions server-timing/defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,63 +223,62 @@ static function( $acc, $query ) {
function perflab_register_additional_server_timing_metrics_from_setting() {
$options = (array) get_option( PERFLAB_SERVER_TIMING_SETTING, array() );

if ( isset( $options['benchmarking_actions'] ) ) {
foreach ( $options['benchmarking_actions'] as $action ) {
$metric_slug = 'action-' . $action;

$measure_callback = static function( $metric ) use ( $action ) {
$metric->measure_before();
add_action( $action, array( $metric, 'measure_after' ), PHP_INT_MAX, 0 );
};

add_action(
$action,
static function() use ( $metric_slug, $measure_callback ) {
perflab_server_timing_register_metric(
$metric_slug,
array(
'measure_callback' => $measure_callback,
'access_cap' => 'exist',
)
);
/*
* This closure measures performance of a hook (action or filter), as follows:
*
* 1. Add a hook callback at the minimum (i.e. earliest) priority possible.
* 2. In that callback, register the metric for the hook, with a prefix of either "action" or "filter".
* 3. Provide a measuring callback which captures the time span between beginning to end of the hook:
* 1. Capture the current time immediately, i.e. at the earliest hook priority.
* 2. Add another hook callback at the maximum (i.e. latest) priority possible.
* 3. In that callback, capture the current time, leading the Server-Timing API to calculate the difference.
*
* Parameters to this closure are the hook name, what type of hook it is (either "action" or "filter", used as a
* prefix for the registered metric), and the callback function to add the hook (either "add_action" or
* "add_filter").
*/
$measure_hook = static function( $hook_name, $hook_type, $add_hook_func ) {
$metric_slug = "{$hook_type}-{$hook_name}";
$measure_callback = static function( $metric ) use ( $hook_name, $add_hook_func ) {
$metric->measure_before();
call_user_func(
$add_hook_func,
$hook_name,
static function( $passthrough = null ) use ( $metric ) {
$metric->measure_after();
return $passthrough;
},
// phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_minFound
defined( 'PHP_INT_MIN' ) ? PHP_INT_MIN : ~PHP_INT_MAX
PHP_INT_MAX
);
};

call_user_func(
$add_hook_func,
$hook_name,
static function( $passthrough = null ) use ( $metric_slug, $measure_callback ) {
perflab_server_timing_register_metric(
$metric_slug,
array(
'measure_callback' => $measure_callback,
'access_cap' => 'exist',
)
);
return $passthrough;
},
// phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_minFound
defined( 'PHP_INT_MIN' ) ? PHP_INT_MIN : ~PHP_INT_MAX
Comment thread
felixarntz marked this conversation as resolved.
Outdated
);
};

if ( isset( $options['benchmarking_actions'] ) ) {
foreach ( $options['benchmarking_actions'] as $action ) {
$measure_hook( $action, 'action', 'add_action' );
}
}

if ( isset( $options['benchmarking_filters'] ) ) {
foreach ( $options['benchmarking_filters'] as $filter ) {
$metric_slug = 'filter-' . $filter;

$measure_callback = static function( $metric ) use ( $filter ) {
$metric->measure_before();
add_filter(
$filter,
static function( $passthrough ) use ( $metric ) {
$metric->measure_after();
return $passthrough;
},
PHP_INT_MAX
);
};

add_filter(
$filter,
static function( $passthrough ) use ( $metric_slug, $measure_callback ) {
perflab_server_timing_register_metric(
$metric_slug,
array(
'measure_callback' => $measure_callback,
'access_cap' => 'exist',
)
);
return $passthrough;
},
// phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_minFound
defined( 'PHP_INT_MIN' ) ? PHP_INT_MIN : ~PHP_INT_MAX
);
$measure_hook( $filter, 'filter', 'add_filter' );
}
}
}
Expand Down