Samuel Elh Blog

WordPress, PHP, Python and JavaScript tutorials and snippets

How to measure WordPress load time and execution time of scripts

In this tutorial, we will be measuring the WordPress load time and execution time of everything WordPress loads into our user end-point.

This can be handy when you are about optimizing your site speed and load time, for instance test out the WordPress installation load time before and after installing certain WordPress plugin or plugins or themes or so.

Before you proceed:

Please note that we are doing this process for the sake of testing WordPress and getting stats from it and it requires a small modifications to a main WordPress core file (index.php) which you should totally backup and restore back the backup once the tests are done. And, we are doing this in a local installation (e.g Using XAMPP, WAMP, MAMP etc..). Do not test this online unless you are pretty sure about everything you do.

WordPress load time: The syntax

PHP is blocking, that way processes our code line by line, and will not jump to the next callbacks until the first ones are done. I don’t know how to explain that (I just know that in Node.js it runs in a non-blocking IO and mutli-threaded event looping asynchronous mode)..  This way, we will record the current time with time() for seconds or even microtime() for milliseconds (You know which one you need for your testing criteria). And once WordPress is loaded, we record the current time again and calculate the difference between this time integer and the past one which we previously recorded before the WordPress installation is being initiated.

$starting_time = microtime(1);

/* Load WordPress now */

print_r( microtime(1) - $starting_time ); // the difference

 

So I was looking into the WordPress hooks BUT using those hooks means you also need to wait for WordPress to be initiated. Hence I went straight to the index.php file located in the root folder. and that is where I made the tests ( I was only testing out some messaging plugin performance before and after caching, that is why I thought it would be a good thing to share here )

Looking into the index file:
How to measure WordPress load time and execution time of scriptsGreat so everything makes sense now.

Counting WordPress load time:

Just follow along, add this code before the require function

$starting_time = microtime(1);

And this after the require function (at the end of the file)

print_r(
  sprintf( "WordPress took %s seconds to load (%s milliseconds)", date( "i:s", $diff = microtime(1) - $starting_time ), $diff )
);

 

Now save, and go ahead to your WordPress front-end, wait for it to load, and then scroll to the maximum bottom of the page view-port and there you will see some text..

Yep;

WordPress took 00:05 seconds to load (5.6693241596222 milliseconds)

That is for me. How about you?

I am done:

That is awesome. Please consider updating the index.php file straight away after you are done. Update it with your backup or from a fresh installation

I hope that helps a little optimizing your site and analyzing the page load time and finding out which plugin or theme was making affecting your site load speed. You could do the same thing in the admin endpoint, just think a little how to get that done, which is a pretty simple process indeed.

Digital Ocean

Cheap Cloud SSD Hosting

Get a VPS now starting at $5/m, fast and perfect for WordPress and PHP applications

Sign Up with $10 Credit

2 Comments

  1. Hi!

    what could it be reasonable metrics for a regular installation?

  2. Another way to do this is with the “timer_stop()” function, apparently. But that and “get_num_queries()” seem to be quite good into give you a insight of how much WordPress uses in each load.

    That without touching the index.php file… but I have not tested them so can’t say if there is any deviation on the results or if they are precise enough.

Leave a Reply

Your email address will not be published.

*

© 2017 Samuel Elh - Powered by WordPress, DigitalOcean & NameCheap

Theme by Anders NorenUp ↑

Subscribe to our mailing list

Sign up to receive updates about WordPress, free and premium plugins and themes in general and tips and tricks

* indicates required