I'd like to measure the PHP page generation time for my Wordpress sites, preferrably without any plugin.

Which PHP files would you edit to do this?

I was thinking about adding this in index.php:

$generation_time_start = microtime(true);

and in the footer.php (add in very small text font with CSS)

$generation_time_end = microtime(true);
echo ($generation_time_end - $generation_time_start)*1000;

Is this a reliable way to measure the page generation time?

How would you do it?

  • 1
    Why the objection to using a plugin - that's the great thing about Open Source, you can learn a lot about coding by reading how other people resolve things - I would suggest you read over the code in the Query Monitor plugin to load how to do what you want to do.
    – Q Studio
    Commented Jan 28, 2021 at 9:27
  • @QStudio I use a few plugins for important tasks, which is great. But I try to keep the number of plugins the lowest as possible for small tasks like this, because, eventually, you have lots of problems like "This plugin is no more updated / not compatible with this new version of Wordpress" and I'd like to avoid this on long term. So if possible, I'd prefer a 100% PHP solution in a few lines of code.
    – Basj
    Commented Jan 28, 2021 at 9:30
  • Sure, it's good to be selective before you use a plugin - you can usually tell which are active and will continue to be used by looking at latest update date, github activity and size of the community they have - as I mentioned, this plugin does what you need, just read over the code - github.com/johnbillion/query-monitor
    – Q Studio
    Commented Jan 28, 2021 at 9:42
  • Ok I'll look into this @QStudio. Side-question: can I define custom global PHP variables in index.php (like $generation_time_start), and keep them in all future PHP files which are called, such as footer.php?
    – Basj
    Commented Jan 28, 2021 at 10:17
  • You can, or just use a class and properties - define then at the first hook, retrieve them on the last, such as "shutdown"
    – Q Studio
    Commented Jan 28, 2021 at 10:23


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.