Profile key performance metrics for an admin-ajax request

If you like my WordPress work, check out my new plugin, Bylines. Thanks!

Using the eval-file command in wp profile, you can profile key performance metrics for an admin-ajax request by mocking, executing, and evaluating the request with a one-off file. These key performance metrics include execution time, query count, cache hit/miss ratio, and more.

For example, to mock the oembed_cache admin-ajax callback in WordPress core, you’d create an oembed-cache.php file like this:

<?php
// Load the wp_ajax_oembed_cache() function
require_once dirname( __FILE__ ) . '/wp-admin/includes/ajax-actions.php';

// Mock data expected in the $_GET request
$_GET = array(
	'post'   => 1,
);

// Ensure WordPress doesn't halt script execution
define( 'DOING_AJAX', true );
add_filter( 'wp_die_ajax_handler', function(){
	return '__return_false';
});

// Finally, call the function we're profiling
wp_ajax_oembed_cache();

Then, profile oembed-cache.php by running wp profile eval-file oembed-cache.php:

$ wp profile eval-file oembed-cache.php
+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
| time    | query_time | query_count | cache_ratio | cache_hits | cache_misses | request_time | request_count |
+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
| 0.0214s | 0s         | 0           | 100%        | 1          | 0            | 0s           | 0             |
+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+