Skip to content

Instantly share code, notes, and snippets.

@felixarntz
Last active November 24, 2022 00:29
Show Gist options
  • Select an option

  • Save felixarntz/43fb19e6491a1bc65a7e930dace17c47 to your computer and use it in GitHub Desktop.

Select an option

Save felixarntz/43fb19e6491a1bc65a7e930dace17c47 to your computer and use it in GitHub Desktop.

Revisions

  1. felixarntz revised this gist Nov 24, 2022. 1 changed file with 0 additions and 31 deletions.
    31 changes: 0 additions & 31 deletions perflab-server-timing-examples.php
    Original file line number Diff line number Diff line change
    @@ -97,37 +97,6 @@ function( $passthrough ) use ( $metric ) {
    },
    PHP_INT_MAX
    );

    // Measure the query to autoload options.
    // While the code here is correct, in practice this will not work from within a plugin as the alloptions query
    // already runs before. It would need to be registered early in a drop-in like object-cache.php to work.
    add_filter(
    'query',
    function( $query ) {
    global $wpdb;
    if ( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" !== $query ) {
    return $query;
    }
    perflab_server_timing_register_metric(
    'load-alloptions-query',
    array(
    'measure_callback' => function( $metric ) {
    $metric->measure_before();
    add_filter(
    'pre_cache_alloptions',
    function( $passthrough ) use ( $metric ) {
    $metric->measure_after();
    return $passthrough;
    },
    PHP_INT_MIN
    );
    },
    'access_cap' => 'exist',
    )
    );
    return $query;
    }
    );
    },
    0
    );
  2. felixarntz created this gist Oct 31, 2022.
    133 changes: 133 additions & 0 deletions perflab-server-timing-examples.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,133 @@
    <?php
    add_action(
    'plugins_loaded',
    function() {
    // Utility function that returns a measuring callback for an action hook,
    // which should be registered as a metric within the action as early as possible
    // (see PHP_INT_MIN usage below).
    $get_measure_action_callback = function( $action_hook ) {
    return function( $metric ) use ( $action_hook ) {
    $metric->measure_before();
    add_action( $action_hook, array( $metric, 'measure_after' ), PHP_INT_MAX );
    };
    };

    // Measure execution time of various critical WordPress action hooks.
    $actions_to_measure = array(
    'plugins-loaded' => 'plugins_loaded',
    'after-setup-theme' => 'after_setup_theme',
    'init' => 'init',
    'loaded' => 'wp_loaded',
    'template-redirect' => 'template_redirect',
    );
    if ( perflab_server_timing_use_output_buffer() ) {
    $actions_to_measure['head'] = 'wp_head';
    $actions_to_measure['footer'] = 'wp_footer';
    }

    foreach ( $actions_to_measure as $metric_slug => $action_hook ) {
    $measure_action_callback = $get_measure_action_callback( $action_hook );

    // If action is already running, at least measure the remainder.
    if ( doing_action( $action_hook ) ) {
    perflab_server_timing_register_metric(
    $metric_slug,
    array(
    'measure_callback' => $measure_action_callback,
    'access_cap' => 'exist',
    )
    );
    } else {
    add_action(
    $action_hook,
    function() use ( $metric_slug, $measure_action_callback ) {
    perflab_server_timing_register_metric(
    $metric_slug,
    array(
    'measure_callback' => $measure_action_callback,
    'access_cap' => 'exist',
    )
    );
    },
    PHP_INT_MIN
    );
    }
    }

    // Measure the time it takes to parse the request (including, but not exclusively, the 'parse_request' action).
    add_filter(
    'do_parse_request',
    function( $passthrough ) {
    perflab_server_timing_register_metric(
    'parse-request',
    array(
    'measure_callback' => function( $metric ) {
    $metric->measure_before();
    add_action( 'parse_request', array( $metric, 'measure_after' ), PHP_INT_MAX );
    },
    'access_cap' => 'exist',
    )
    );
    return $passthrough;
    },
    PHP_INT_MAX
    );

    // Measure the time it takes to locate the template.
    add_action(
    'template_redirect',
    function() {
    perflab_server_timing_register_metric(
    'locate-template',
    array(
    'measure_callback' => function( $metric ) {
    $metric->measure_before();
    add_filter(
    'template_include',
    function( $passthrough ) use ( $metric ) {
    $metric->measure_after();
    return $passthrough;
    },
    PHP_INT_MIN
    );
    },
    'access_cap' => 'exist',
    )
    );
    },
    PHP_INT_MAX
    );

    // Measure the query to autoload options.
    // While the code here is correct, in practice this will not work from within a plugin as the alloptions query
    // already runs before. It would need to be registered early in a drop-in like object-cache.php to work.
    add_filter(
    'query',
    function( $query ) {
    global $wpdb;
    if ( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" !== $query ) {
    return $query;
    }
    perflab_server_timing_register_metric(
    'load-alloptions-query',
    array(
    'measure_callback' => function( $metric ) {
    $metric->measure_before();
    add_filter(
    'pre_cache_alloptions',
    function( $passthrough ) use ( $metric ) {
    $metric->measure_after();
    return $passthrough;
    },
    PHP_INT_MIN
    );
    },
    'access_cap' => 'exist',
    )
    );
    return $query;
    }
    );
    },
    0
    );