@@ -223,63 +223,62 @@ static function( $acc, $query ) {
223223function perflab_register_additional_server_timing_metrics_from_setting () {
224224 $ options = (array ) get_option ( PERFLAB_SERVER_TIMING_SETTING , array () );
225225
226- if ( isset ( $ options ['benchmarking_actions ' ] ) ) {
227- foreach ( $ options ['benchmarking_actions ' ] as $ action ) {
228- $ metric_slug = 'action- ' . $ action ;
229-
230- $ measure_callback = static function ( $ metric ) use ( $ action ) {
231- $ metric ->measure_before ();
232- add_action ( $ action , array ( $ metric , 'measure_after ' ), PHP_INT_MAX , 0 );
233- };
234-
235- add_action (
236- $ action ,
237- static function () use ( $ metric_slug , $ measure_callback ) {
238- perflab_server_timing_register_metric (
239- $ metric_slug ,
240- array (
241- 'measure_callback ' => $ measure_callback ,
242- 'access_cap ' => 'exist ' ,
243- )
244- );
226+ /*
227+ * This closure measures performance of a hook (action or filter), as follows:
228+ *
229+ * 1. Add a hook callback at the minimum (i.e. earliest) priority possible.
230+ * 2. In that callback, register the metric for the hook, with a prefix of either "action" or "filter".
231+ * 3. Provide a measuring callback which captures the time span between beginning to end of the hook:
232+ * 1. Capture the current time immediately, i.e. at the earliest hook priority.
233+ * 2. Add another hook callback at the maximum (i.e. latest) priority possible.
234+ * 3. In that callback, capture the current time, leading the Server-Timing API to calculate the difference.
235+ *
236+ * Parameters to this closure are the hook name, what type of hook it is (either "action" or "filter", used as a
237+ * prefix for the registered metric), and the callback function to add the hook (either "add_action" or
238+ * "add_filter").
239+ */
240+ $ measure_hook = static function ( $ hook_name , $ hook_type , $ add_hook_func ) {
241+ $ metric_slug = "{$ hook_type }- {$ hook_name }" ;
242+ $ measure_callback = static function ( $ metric ) use ( $ hook_name , $ add_hook_func ) {
243+ $ metric ->measure_before ();
244+ call_user_func (
245+ $ add_hook_func ,
246+ $ hook_name ,
247+ static function ( $ passthrough = null ) use ( $ metric ) {
248+ $ metric ->measure_after ();
249+ return $ passthrough ;
245250 },
246- // phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_minFound
247- defined ( 'PHP_INT_MIN ' ) ? PHP_INT_MIN : ~PHP_INT_MAX
251+ PHP_INT_MAX
248252 );
253+ };
254+
255+ call_user_func (
256+ $ add_hook_func ,
257+ $ hook_name ,
258+ static function ( $ passthrough = null ) use ( $ metric_slug , $ measure_callback ) {
259+ perflab_server_timing_register_metric (
260+ $ metric_slug ,
261+ array (
262+ 'measure_callback ' => $ measure_callback ,
263+ 'access_cap ' => 'exist ' ,
264+ )
265+ );
266+ return $ passthrough ;
267+ },
268+ // phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_minFound
269+ defined ( 'PHP_INT_MIN ' ) ? PHP_INT_MIN : ~PHP_INT_MAX
270+ );
271+ };
272+
273+ if ( isset ( $ options ['benchmarking_actions ' ] ) ) {
274+ foreach ( $ options ['benchmarking_actions ' ] as $ action ) {
275+ $ measure_hook ( $ action , 'action ' , 'add_action ' );
249276 }
250277 }
251278
252279 if ( isset ( $ options ['benchmarking_filters ' ] ) ) {
253280 foreach ( $ options ['benchmarking_filters ' ] as $ filter ) {
254- $ metric_slug = 'filter- ' . $ filter ;
255-
256- $ measure_callback = static function ( $ metric ) use ( $ filter ) {
257- $ metric ->measure_before ();
258- add_filter (
259- $ filter ,
260- static function ( $ passthrough ) use ( $ metric ) {
261- $ metric ->measure_after ();
262- return $ passthrough ;
263- },
264- PHP_INT_MAX
265- );
266- };
267-
268- add_filter (
269- $ filter ,
270- static function ( $ passthrough ) use ( $ metric_slug , $ measure_callback ) {
271- perflab_server_timing_register_metric (
272- $ metric_slug ,
273- array (
274- 'measure_callback ' => $ measure_callback ,
275- 'access_cap ' => 'exist ' ,
276- )
277- );
278- return $ passthrough ;
279- },
280- // phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_minFound
281- defined ( 'PHP_INT_MIN ' ) ? PHP_INT_MIN : ~PHP_INT_MAX
282- );
281+ $ measure_hook ( $ filter , 'filter ' , 'add_filter ' );
283282 }
284283 }
285284}
0 commit comments