Skip to content

Instantly share code, notes, and snippets.

@felixarntz
Last active February 23, 2024 11:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felixarntz/de5c697a1a16c2b892634b70216eb6c7 to your computer and use it in GitHub Desktop.
Save felixarntz/de5c697a1a16c2b892634b70216eb6c7 to your computer and use it in GitHub Desktop.
Mini WordPress core server performance test suite

Mini WordPress core server performance test suite

Purpose

This quick "mini test suite" allows you to easily collect server-side performance metrics for WordPress core development. It does so by utilizing the Server-Timing header.

Usage

  1. Paste the code from main.php into pretty much anywhere in WordPress core, e.g. into wp-includes/default-filters.php.
  2. Use code like the one in example.com around any code or a specific function that you want to record metrics for.
    • All timing metrics should be float, in seconds. They will automatically be converted to milliseconds for output.
    • Other metrics (e.g. function or method call counts) should be integer.
  3. You can test that the metrics show up correctly by loading WordPress in the browser and using performance.getEntries()[0].serverTiming in the Developer Console.
  4. Use the GoogleChromeLabs/wpp-research repository, specifically its benchmark-url command, to trigger a number of requests against the URL, and return the median Server-Timing values for all requests made. For example like this (for 10 requests, and output as CSV): npm run research -- benchmark-url -u http://localhost:8889 -n 10 -o csv
  5. You can copy the CSV output into a spreadsheet to easily share it with other people.
<?php
function demo_func() {
// Add this to the top of code to record performance timing for.
global $server_timing_values;
if ( ! is_array( $server_timing_values ) ) {
$server_timing_values = array();
}
$start_time = microtime( true );
// Here should be the code of the function itself.
sleep( 1 );
// This is an example of how to add a Server-Timing metric for the total execution time _and_ call count of the function.
if ( ! isset( $server_timing_values['demo-func-time'] ) ) {
$server_timing_values['demo-func-time'] = 0.0;
}
if ( ! isset( $server_timing_values['demo-func-count'] ) ) {
$server_timing_values['demo-func-count'] = 0;
}
$server_timing_values['demo-func-time'] += microtime( true ) - $start_time;
$server_timing_values['demo-func-count']++;
<?php
// Add this pretty much anywhere in WordPress core, e.g. into the `wp-includes/default-filters.php` file.
add_action(
'template_redirect',
function() {
ob_start();
add_action(
'shutdown',
function() {
global $server_timing_values, $timestart;
if ( ! is_array( $server_timing_values ) ) {
$server_timing_values = array();
}
$server_timing_values['total-time'] = microtime( true ) - $timestart;
$output = ob_get_clean();
$header_values = array();
foreach ( $server_timing_values as $slug => $value ) {
if ( is_float( $value ) ) {
$value = round( $value * 1000.0, 2 );
}
$header_values[] = sprintf( 'wp-%1$s;dur=%2$s', $slug, $value );
}
header( 'Server-Timing: ' . implode( ', ', $header_values ) );
echo $output;
},
-9999
);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment