Timing WordPress Requests
Sometimes when doing performance testing, you just want to do a quick test without setting up a full benchmarking test suite. WordPress and PHP offer a few tools for us to use.
WP Timers
WP has 2 little known functions available for us: timer_start
and timer_stop
.
timer_start
(source) just sets a global variable $timestart
to PHP’s microtime
(docs). microtime
just returns the current Unix timestamp with microseconds, and in WP’s case, as a float
.
timer_stop
(source) also calls microtime
and sets it to the global $timeend
. It then returns $timeend - $timestart
to get the total time (in seconds) elapsed between the 2 calls.
You can wrap any code you want between these two functions to get the time elapsed. But WP conveniently includes timer_start
early on in their bootstrapping process in wp-settings.php
(source). This means that you can just drop a timer_stop()
call into your theme to get the total elapsed time of the request up until that point.
Example in your functions.php
:
$time_elapsed = timer_stop();
Or if you just want to echo the time, pass a boolean as the first parameter:
timer_stop(true);
Note that although WP always calls timer_start
which sets the global variable $timestart
, calling it again yourself will overwrite $timestart
to the microtime when you called it.
PHP Request Time
If you want to get a more precise measure of how long an entire request takes, you shouldn’t rely on timer_start
and timer_stop
. The main reason is that although WP tries to call timer_start
early on, it’s still not early enough. Take a look at wp-settings again and notice how much is happening before the timer starts. And wp-settings.php
isn’t even the first WP file loaded either.
You’re probably familiar with superglobal $_SERVER
(docs). $_SERVER['REQUEST_TIME']
is actually the timestamp of the start of the request. Available since PHP 5.1.0, this is an integer. $_SERVER['REQUEST_TIME_FLOAT']
is available since PHP 5.4.0 and is the same timestamp but as a float
.
This timestamp gives us a much more accurate time to work with as the beginning of the request. You can use it like this:
$time_elapsed = microtime() - $_SERVER['REQUEST_TIME']; // PHP 5.1.0
$time_elapsed = microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']; // PHP 5.4.0
Total Request Time
If you want to know the total elapsed time (spent in PHP) of a request, you could try and do the above with that calculation done as the last thing in your page/theme. But that can actually be a little messy.
It would probably involve placing that code as the last thing in your footer template for example. Fortunately there’s a foolproof way to know your code will run at the end of the request and keep your code in functions.php
(or anywhere other than a template).
PHP provides a way to register a function to be run when PHP shuts down. The function is conveniently called register_shutdown_function
(docs) and it looks like this:
function time_elapsed() {
$time_elapsed = microtime() - $_SERVER['REQUEST_TIME']; // PHP 5.1.0
echo $time_elapsed, PHP_EOL;
}
register_shutdown_function('time_elapsed');
WordPress actually has a shutdown
action (docs) that you can hook into as well:
function time_elapsed() {
$time_elapsed = microtime() - $_SERVER['REQUEST_TIME'];
echo $time_elapsed, PHP_EOL;
}
add_action('shutdown', 'time_elapsed');
With either of these methods, you’ll get as close as you can can get to the total request time.
Happy timing.